bash

Best practices for Bash

  1. Use "$x" instead of $x

This will prevent surprises when the variable contains spaces

2. Use $HOME instead of ~

This is because the ~ is not always expanded in certain commands

3. For conditional expressions use double brackets [[ ]]

instead of single brackets [ ]

If statements

if [[ $num -gt 50 ]]; then
	echo "$num is greater than 50"
elif [[ $num -eq 50 ]]; then
	echo "$num is equal to 50
else
	echo "$num is less than 50"
fi  

Debugging

using #/bin/bash -x at top of your script will run your script in your script will enable XTRACE, this will print every line it executes including the values of the variables that get used in these lines

also using set -x anywhere in your script will also enable XTRACE

Resources

If and Else Conditionals in ZSH Script
Decision-making, also called conditionals, is a fundamental building block in programming. ZSH is built on top of Bash. The syntax and functionality are similar for scripts built for Bash or the ZSH shell. In this article, we will discuss how to implement conditionals in our scripts using the ZSH sh…