Good practices for Shell scripts

If we follow certain good practices, then we will face errors. Even if errors are found, these will be easier to debug:

  1. Clear and tidy the script.
  2. Try to properly indent the programming constructs, such as if, for, while, and other similar loops:
    if [ $rate -lt 3 ]
    then
      echo "Sales tax rate is too small."
    fi
  
  1. Do not put multiple commands on the same line by using ;.
  2. Use descriptive variable names, such as salary, instead of sa. In very complex Shell scripts, non-descriptive variable names will make debugging very difficult.
  3. Store the file and directory names in variables instead of typing them again and again. If any change is required in the directory path, then making the change in the variable at one ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.