Good Shell Scripting Practices

Just some notes on good practices when shell scripting…

  • It is a good idea to comment your code (this is for programming in general).
  • Have a header with name of script, author, modification date and parameters being used.
  • Variable declaration comments should be on the same line.
  • While other comments can be on a separate line.
  • Variables should have short, but descriptive names.
  • Constants (variables that don’t change) should be upper case.
  • Numbers should not be randomly used, they should be linked to constants so they have meaning.
  • It is a good idea to follow GNU guidelines for how utilities should operate; for example:
    • should have one letter options with one dash () and longer options with two dashes ().
    • should have –help and –version option
      • –version prints information about the program, such as name and version.
      • –help a documentation on how to use the program.
  • For commands that take input, output files; input files should be arguments, while output files should given with option -o.
  • Split functionality into small functions.
  • place #!/bin/bash at top of script.
  • Don’t use reserved names such as echo or ls.
  • Don’t use same name for functions and variables.
  • In if statements place arithmetic between (( )) to not be confused with string comparisons.