- Use
bash
built-ins wherever possible. - Try not to pipe (
|
) at all. - Limit usage of external commands
$(cmd)
. - Indent 4 spaces.
- Use snake_case for function and variable names.
- Keep lines below
100
characters long. - Use
[[ ]]
for tests. - Quote EVERYTHING.
For your contribution to be accepted, your changes need to pass ShellCheck.
shellcheck gen
- Don’t use GNU conventions in commands.
- Use POSIX arguments and flags.
- Don’t use
cut
.- Use
bash
's built-in parameter expansion.
- Use
- Don’t use
echo
.- Use
printf "%s\n"
- Use
- Don’t use
bc
. - Don’t use
sed
.- Use
bash
's built-in parameter expansion.
- Use
- Don’t use
cat
.- Use
bash
's built-in syntax (file="$(< /path/to/file.txt)"
).
- Use
- Don’t use
grep "pattern" | awk '{ printf }'
.- Use
awk '/pattern/ { printf }'
- Use
- Don’t use
wc
.- Use
${#var}
or${#arr[@]}
.
- Use
If the test only has one command inside of it; use the compact test
syntax. Otherwise the normal if
/fi
is just fine.
# Bad
if [[ "$var" ]]; then
printf "%s\n" "$var"
fi
# Good
[[ "$var" ]] && printf "%s\n" "$var"
# Also good (Use this for longer lines).
[[ "$var" ]] && \
printf "%s\n" "$var"
Case statements need to be formatted in a specific way.
# Good example (Notice the indentation).
case "$var" in
1) printf "%s\n" 1 ;;
2)
printf "%s\n" "1"
printf "%s\n" "2"
;;
*)
printf "%s\n" "1"
printf "%s\n" "2"
printf "%s\n" "3"
;;
esac