Debugging mode – the tracing execution (option -x)

The -x option, short for xtrace or execution trace, tells the shell to echo each command after performing the substitution steps. Thus, we will see the value of variables and commands.

We can trace the execution of the Shell script as follows:

    $ bash -x hello.sh
  

Instead of the previous way, we can modify the shebang line as follows:

#!/bin/bash  -x 

Let's test the earlier debug_01.sh script as follows:

    $ bash -x hello.sh
  

Output:

    $ bash -x debug_02.sh
    + echo Hello student
    Hello student
    + date
    + echo The date is Fri May 1 00:18:52 IST 2015
    The date is Fri May 1 00:18:52 IST 2015
    + echo Your home shell is /bin/bash
    Your home shell is /bin/bash
    + echo Good-bye student
    Good-bye student
  

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.