- Always indent by four spaces, do not use tab characters.
- Ensure lines do not end with white space characters (enable automatic line trimming in your editor of choice)
- Place the keywords
then / do
on the same line likeif / for / while
and not a separate line. - Always use
$( myCommand )
instead of backticks`` myCommand ``
to execute commands in subshells
- Always use the
local
keyword when declaring variables within functions. - Prefer using lowercase variable names except when exporting them to environment. Otherwise unintentional clashes with existing environment variables, e.g.
$USER
, or$PATH
may happen. - Prefer using the bash builtin double brackets test operator
[[ ... ]]
instead of the single bracket test operator. It supports glob/regex pattern matching and better handles testing of empty variables. See http://mywiki.wooledge.org/BashGuide/Practices#Bash_Tests
- Try to limit the usage of subshells which are expensive in emulated environments such as Cygwin.
- Use
$PWD instead of $ (pwd) to get the current path. - Avoid unnecessary cat, use
command < file
instead ofcat file | command
which is executed in a subshell.
- Use
- Use
#!/usr/bin/env bash
instead of#!/bin/bash
- Use
\033
as escape sequence instead of\e
to support MacOS - Use
sed -E
instead ofsed -r
to enable extended regular expressions on Linux and MacOS. See https://www.gnu.org/software/sed/manual/sed.html#Command_002dLine-Options