Bash

Published

October 10, 2024

Unit testing

One option is to use Bach by the amazing Chai Feng.

Safer scripts

#!/usr/bin/env bash

set -euxo pipefail
  • ‘-e’ exit immediately on command fail

  • ‘-u’ treat unset variables as an error and exit immediately

  • ‘-x’ print each command before executing it

  • ‘-o pipefail’ sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status.

see: safer bash scripts

Also, always quote all variables.

List uniq file extensions in a dir

for f in *; do echo ${f##*.}; done | sort | uniq`

Heredoc

$ cat << END | wc -l
one
two
three
END
3