Skip to content

zahlman/func2cmd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

func2cmd — simple utilities for writing simple scripts at the command line

Why

When I find myself repeating simple commands or pipelines in Bash I often have the instinct to write a quick function for it. (It's easy enough to repeat a command from history, but editing in filenames etc. gets irritating.)

The problem is, there's a decent chance I'll want to do the same or similar things in another terminal session in the future, and my hastily-defined functions won't persist. Writing them to my ~/.bash_aliases is too much friction, especially since I keep that file set read-only (as a tiny bit of protection against malware, which I'm told usually doesn't check). And it's not really maintainable or shareable.

So I set up a bit of automation so that I can easily save these functions permanently, as separate scripts with a shebang and everything. So not only can I use them later, I can chuck them in their own GitHub repo etc. to show off.

And then I thought, maybe others could benefit from this setup as well, so here you go. I originally wrote the main logic in Python, but converted it to Bash to remove the dependency (and to study/practice with Bash, since I really normally only use it for very simple things).

How

  1. Put func2cmd somewhere writable that's on your path (I use ~/.local/bin) and chmod +x it.

  2. (Recommended) As root, put share-command in /usr/local/bin and chmod +x it. (This currently depends on func2cmd being in ~/.local/bin.)

  3. (Recommended) Add this to your ~/.bashrc, ~/.bash_aliases or similar file, to simplify command-line use:

    make-command () { type "$1" | func2cmd && unset "$1"; }
    

Note that it isn't possible to replace this cleanly with a script; the script will run in an environment that doesn't contain the function being converted (unless you export -f first, which defeats the goal of simplifying use).

What

func2cmd

func2cmd example will:

  1. Parse the output of type example
  2. Ensure that the name refers to a function
  3. Extract the body of that function
  4. Save it as a Bash script in the same folder as func2cmd

share-command

sudo share-command example will:

  1. Determine which user invoked sudo
  2. Look for example in that user's ~/.local/bin
  3. If found, and it wouldn't overwrite anything, copy it (using install) to /usr/local/bin

You may find this especially useful if, like me, you use multiple local user accounts for identity management.

More stuff

The etc/ folder contains a few other interesting things, which you may use or consider as you see fit:

edit-command

A simple wrapper I created using func2cmd so that I can easily open other scripts created by func2cmd in Vim. Your editor, terminal etc. preferences may differ.

bash-cmdline

A utility for "dry-running" commands, to verify exactly what arguments would be passed to a command. Simply replace the command you would otherwise run with bash-cmdline; the output is a set of hex dumps of each received argument (using xxd). I've found this useful when trying to work with tricky filenames (especially those containing spaces and fancy Unicode), especially if tools like xargs are involved. It also lets you test out e.g. using Bash character expansions to supply binary data as arguments.

make-pyp

A wrapper, created using func2cmd, for Shantanu Jain's excellent pyp utility. It uses the --explain option, which outputs a usable Python script (complete with shebang), to add to your collection of commands. This requires a permanent installation of pyp created using either Pipx (pipx install pypyp) or uv (uv tool intall pypyp). (Note that there is a separate PyPI package named pyp; don't use that one.) The shebang for the resulting script is rewritten to use pyp's environment, so that it can use pyp-provided functionality such as pypprint.

python-func2cmd

The original version of func2cmd, written in Python.

About

Simple utilities for writing simple scripts at the command line

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors