sh -x and sh -v in Linux

By: Dorris  

While programming shell sometimes you need to find the errors (bugs) in shell script and correct the errors (remove errors - debug). For this purpose you can use -v and -x option with sh or bash command to debug the shell script. General syntax is as follows:
Syntax:
sh   option   { shell-script-name }
OR
bash   option   { shell-script-name }
Option can be
-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments. 

Example:

$ cat > dsh1.sh
#
# Script to show debug of shell
#
tot=`expr $1 + $2`
echo $tot

Press ctrl + d to save, and run it as
$ chmod 755 dsh1.sh
$ ./dsh1.sh 4 5

9
$ sh -x dsh1.sh 4 5
#
# Script to show debug of shell
#
tot=`expr $1 + $2`
expr $1 + $2
++ expr 4 + 5
+ tot=9
echo $tot
+ echo 9
9

 

See the above output, -x shows the exact values of variables (or statements are shown on screen with values).

$ sh -v dsh1.sh 4 5

Use -v option to debug complex shell script.




Archived Comments

1. Hi, just wanted to tell you, I loved this post. It was helpful.
Keep on posting!

View Tutorial          By: pearl abyss facebook at 2017-05-15 18:55:17


Most Viewed Articles (in Linux )

Latest Articles (in Linux)

smskannel SMS gateway run in background

Running jar files in background in ssh window

Can't locate ExtUtils/MakeMaker.pm in @INC ...

Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697

make: Nothing to be done for `all'.

bash: svn: command not found

apxs: command not found

bash: make: command not found

How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows

How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows

How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows

How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows

Compiling and Installing software from source in Linux

Installing using Debian apt-get in Linux

How to burn your CD / DVD ISO image using k3b on CentOS

Comment on this tutorial