Skip to content

Files

Latest commit

 

History

History

shell

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Shell

ShellCheck

ShellCheck can catch many bug risks in shell scripts: we encourage running it against all scripts. There is also a Code Climate engine available wrapping ShellCheck: we recommend enabling this engine on repos that contain significant shell scripts.

Compatability

/bin/sh should be preferred to /bin/bash for compatability unless Bash-only features are really needed. Be aware that many modern systems actually alias /bin/bash to /bin/sh, so if you use Bash-isms accidentally you may not realize it. If you want to be sure your shell is sh-compliant, you may want to install the [dash] shell & use that to test your scripts.

By the same token, using POSIX flags & functionality that will work between most flavors of *nix is preferred when possible.

Error reporting

All scripts should call set -e to ensure the script will exit immediately if any intermediate command errors.

Variable and function names

Names of variables and functions should be snake_cased. Variable names should not be UPPER_CASED unless being exported to the ENV.

Exit codes

The general principle, of course, is that 0 indicates success and 1 indicates a general error. More complex scripts may want to define specific codes for different kinds of errors: please refer to the Bash Scripting Guide & sysexits for reserved/defined codes. One common one we use in many scripts is 64 to indicate incorrect arguments passed to a script.

Calling binaries

Long-form flags (e.g. ls --all instead of ls -a) are preferred: code is read more often than it's written. Long flags help keep scripts easy to understand & maintain, and written scripts don't have the same need for brevity that typing at an interactive terminal does.

String interpolation for output

Interpolating variables within strings passed to echo can have unexpected results: printf is preferred.

# Good

printf "Hello %s\n" "$name"

# Bad

echo "Hello $name"

# This is fine: no variable involved

echo "Hello world."

Resources