Best Practices 4 Bash

Best Practices 4 Bash

Explained best practice for Bash programmers

Here are some Bash scripting best practices with examples to write clean and maintainable scripts:

  1. Use functions:
greet(){
  echo "Hello $1!"
}

greet "John"
# Hello John!
  1. Use meaningful variable names:
firstName="John"
lastName="Doe"
  1. Use constants:
MAX_TRIES=3
  1. Use if/then/else over && and ||:
if [[ $age -ge 18 ]]; then
  echo "Eligible to vote"
else
  echo "Not eligible"
fi

Here is how that if/else condition could be written using && and || instead:

[[ $age -ge 18 ]] && echo "Eligible to vote" || echo "Not eligible"

This works because:

  • && is the AND operator

  • || is the OR operator

  • The expressions on either side of && or || are evaluated left to right.

  • If the first expression evaluates to true, the result is the value of the second expression.

  • If the first expression evaluates to false, the result is the value of the first expression.

So in this case:

  • If $age is >= 18, then [[ $age -ge 18 ]] evaluates to true

  • The AND operator && will then evaluate the second expression echo "Eligible to vote"

  • That result is returned

  • If $age is < 18, then [[ $age -ge 18 ]] evaluates to false

  • The OR operator || will then evaluate the first expression, which is false, and return that result

  • The second expression echo "Not eligible" is not evaluated.

While this works, if/then/else is generally considered more readable for conditional logic in scripts.

  1. Add indentation and comments:
# Greet the user
greet $firstName

# Check eligibility 
if [[ $age -ge 18 ]] ; then
   echo "Eligible to vote"
fi
  1. Handle errors:
if [ ! -e "$file" ]; then
  echo "File does not exist!" 
  exit 1  # Exit with error
fi
  1. Add help function:
help() {
  echo "This script greets a user and..."
}

help

Disclaim: This guide was created with AI. Of course, many free resources teach you how to write better Bash code. I have asked for these resources:

• Shell Style Guide by Geir Magnusson Jr - A comprehensive guide with recommendations on style, structure, portability and more. This is considered a de facto standard for Bash style. https://google.github.io/styleguide/shellguide.html

• The Linux Documentation Project's Bash Guide - A thorough tutorial on Bash scripting with sections on best practices.
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/

• ShellCheck - An open source tool to static analyze your shell scripts and detect issues like syntax errors, bugs and unintended behavior.
https://www.shellcheck.net/

• Linuxize - This blog has several good articles on Bash scripting best practices.
https://linuxize.com/

• Real World Bash Scripting - A paid course by Mark Smalley that covers best practices in depth. Highly recommended if you want to take your Bash skills to the next level. https://linuxconfig.org/bash-scripting-tutorial

Hope the links are helpful! Let me know if any of these links do not work properly


Learn and prosper 🖖