Using the set command

Most of the time, we invoke the debugging mode from the first line of script. This debugging mode will remain active until the last line of code. But many times, we may need to enable debugging for a particular section of script. By using the set command, we can enable and disable debugging at any point in our shell script:

    set -x
    section of script
    set +x

Consider the following script:

[ $str1 = $str2 ] 
echo $? 
Set -x 
[ $str1 != $str2 ] 
echo $? 
[ -z $str1 ] 
echo $? 
Set +x 
[ -n $str2 ] 
echo $? 
Exit 0 

In this case, the debugging will be enabled after the set -x and will be disabled immediately after the set +x.

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.