HostOnNet Blog

Bash show command with set -x

Looking for Linux Server Admin or WordPress Expert? We can help.

In bash script, some times you need to print out the command that is executed. This is useful when you want to debug some dynamically generated commands. This can be done with set -x statement. set +x will disable this.

Here is a sample bash script

honvnc@hon-vpn:~$ cat 1.sh
#!/bin/bash

set -x
ls -l
set +x
echo "done"
honvnc@hon-vpn:~$ 

Example

honvnc@hon-vpn:~$ ./1.sh 
+ ls -l
total 52
-rwxr-xr-x 1 honvnc honvnc   45 Sep 24 18:29 1.sh
-rw------- 1 honvnc honvnc 1679 Sep 24 17:25 boby
-rw-r--r-- 1 honvnc honvnc  396 Sep 24 17:25 boby.pub
drwxr-xr-x 2 honvnc honvnc 4096 Feb  9  2016 Desktop
drwxr-xr-x 2 honvnc honvnc 4096 Feb  9  2016 Documents
drwxr-xr-x 6 honvnc honvnc 4096 Sep 18 19:37 Downloads
drwxr-xr-x 2 honvnc honvnc 4096 Feb  9  2016 Music
drwxr-xr-x 6 honvnc honvnc 4096 May 20  2016 myApp
drwxr-xr-x 2 honvnc honvnc 4096 Feb  9  2016 Pictures
drwxr-xr-x 2 honvnc honvnc 4096 Feb  9  2016 Public
drwxr-xr-x 4 honvnc honvnc 4096 Aug  1 05:04 site-download
drwxr-xr-x 2 honvnc honvnc 4096 Feb  9  2016 Templates
drwxr-xr-x 2 honvnc honvnc 4096 Feb  9  2016 Videos
+ set +x
done
honvnc@hon-vpn:~$ 

Here it print the commands ls -l instead of the result. Since we used set +x before running

echo "done"

this code is not printed out.

See bash

Posted in Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.