Shell script

File location

For builds that involve independent command-line tools, use Make, and follow DataMade’s Making Data Guidelines and Clark Grubb’s Makefile Style Guide. Examples: standard_profile_template

If a repository has scripts to set itself up and/or update itself, follow GitHub’s Scripts to Rule Them All. Examples: deploy, standard_profile_template

bash and sh scripts should use the .sh extension, unless they are in a script/ directory.

Shell options

Start a Bash script with set -euo pipefail. If the script explicitly handles unset variables, omit -u. To see which command failed due to the -e option, add -x.

Reference: The Set Builtin

Code style

Check shell scripts using shellcheck.

Style shell scripts using shfmt: for example, shfmt -w -i 4 -sr (shfmt -f .).

Use:

  • [ ] instead of test

  • [ ] instead of [[ ]], unless required

  • $NAME instead of ${NAME}, unless followed by a word character

  • Subshells to temporarily change directory, for example:

    (
       cd subdir/
       mv x.txt y.txt
    )
    

    Instead of:

    cd subdir/
    mv x.txt y.txt
    cd ..  # AVOID
    

Avoid:

  • set -x in scripts run by continuous integration, because it will expand any secret variables

Reference