Improves your Bash scripts

Improves your Bash scripts.


The following shebang

#!/usr/bin/env bashsh-0

gives you:

  • set -e by default:
$ echo hello ; false ; echo world

will only print hello.

  • CMD for showing executed commands in bold on stderr:
$ CMD git status
  • CMD_STR prints the string in bold on stderr and runs it with eval:
$ CMD_STR 'gitk --all &'
  • MSG prints a remark in bold on stderr, formatted as a comment:
$ MSG hello world

prints # hello world.

  • READ_P works like read -p but prints the first parameter using MSG:
$ READ_P 'What is your name? ' your_name
  • QUOTED prints the given arguments in a way that is safe to pass to commands:
$ echo "My arguments are: $(QUOTED "$@")"

You can find a few examples in the examples directory.

Caveats / known limitations

You cannot just

bpkg install bashsh

to install bashsh locally (it is forced to install globally anyway), because if you use

#!/usr/bin/env ./relative/path/to/bashsh-0

the path of the command supplied to env will be interpreted as relative to the working directory, not to the script’s directory.

Published: October 01 2014

blog comments powered by Disqus