bash
Best practices for Bash
- 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