Shell scripts are often criticized for not having an integrated debugger. But this is false!

When programming in bash, there are command line options to see what is being read and then executed in a script.

Example

Let’s say we have the script MyScript.sh:

  #!/bin/sh
touch unFichier
if [ -f ./unFichier ]; then
  rm ./unFichier
fi
  

If we execute it this way:

  /bin/bash -v -x ./MyScript.sh
  

We’ll get output like this:

  #!/bin/sh
touch unFichier
+ touch unFichier
if [ -f ./unFichier ]; then
  rm ./unFichier
fi
+ '['-f ./unFichier ']'
+ rm ./unFichier
  

The normal lines are the lines and blocks that are read, while those with a + in front are the ones being executed.

Last updated 05 Mar 2008, 09:49 +0200. history