When to use bash and when to avoid bash
it’s rather simple:
- does it need to glue userland utilities together? use bash.
- does it need to do complex tasks (e.g. database queries)? use something else.
Why? … It consumes a lot of time and is often very difficult to debug in comparison to dynamic programming languages such as python, ruby or even perl. You are simply going to waste valuable time, performance and nerve you could have spent better otherwise.
Style conventions
…
- Thus, scripts should somewhat reflect the following general layout:
#!/usr/bin/env bash # # AUTHORS, LICENSE and DOCUMENTATION # set -eu -o pipefail Readonly Variables Global Variables Import ("source scriptname") of external source code Functions … Main …
Use built in variable expansion instead of sed/awk
…
same thing with string replacement.
# ${VAR/PATTERN/STRING} # single replacement # ${VAR//PATTERN/STRING} # all match replacement # Use ${VAR#PATTERN} ${VAR%PATTERN} ${VAR/PATTERN} for string removal VAR=foofoobar ${VAR/foo/bar} # barfoobar ${VAR//foo/bar} # barbarbar ${VAR//foo} # bar
More examples and uses here: http://wiki.bash-hackers.org/syntax/pe (Internet Archive 2023-04-08)