Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


Debugging

Debugging a bash script (or any coding) can be as exciting or full of headaches as writing the code in the first place.

Luckily there are several excellent resources available to assist you on that excursion.

Bash will tell you right away about some errors or typos:

thisValue = "Forty-two"
echo $thisValue
-bash: thisValue: command not found

Recall from the variables page: no spaces around '-'

First a few simple things you can do right now in your script, depending on what the issue seems to be.

Carefully read over your code word-by-word:

  • check for correct spelling of commands and variables
  • missing / extra brackets or braces in loops, arrays or functions: () {} []
  • missing / extra spaces
  • missing / extra quotes (' " `)
  • have someone else knowledgeable read over your code

    Now try these:

    Those fixes may be all you need. Try them.

    If not here are some deep-dive tools:

    Stopping or Quitting

    At some point you will write a script which seems to take forever to complete - on a Mac you might even get a beachball spinning endlessly on your screen.

    This means you may have something in your code that is not working, maybe trying to do something but can't finish - it is hung up. In these cases you can hit control-. (control + period) on the keyboard which should quit that process and bring you back to your prompt.

    Now you need to debug your script to figure out what's wrong.

    On rare occasions you may have to Force Quit Terminal (or ANY application) that is not reacting to any key press or mouse clicks.

    Click the Apple logo (&⌘) in the top left of your task bar, then choose Force Quit, or press option-command-esc with the keyboard.