How to echo shell commands as they are executed – Dev

The best answers to the question “How to echo shell commands as they are executed” in the category Dev.


In a shell script, how do I echo all shell commands called and expand any variable names?

For example, given the following line:


I would like the script to run the command and display the following

ls /full/path/to/some/dir

The purpose is to save a log of all shell commands called and their arguments. Is there perhaps a better way of generating such a log?


set -x will give you what you want.

Here is an example shell script to demonstrate:

set -x #echo on

ls $PWD

This expands all variables and prints the full commands before output of the command.


+ ls /home/user/
file1.txt file2.txt


set -x or set -o xtrace expands variables and prints a little + sign before the line.

set -v or set -o verbose does not expand the variables before printing.

Use set +x and set +v to turn off the above settings.

On the first line of the script, one can put #!/bin/sh -x (or -v) to have the same effect as set -x (or -v) later in the script.

The above also works with /bin/sh.

See the bash-hackers’ wiki on set attributes, and on debugging.

$ cat shl

ls $DIR

$ bash -x shl 
+ DIR=/tmp/so
+ ls /tmp/so


You can also toggle this for select lines in your script by wrapping them in set -x and set +x, for example,

if [[ ! -e $OUT_FILE ]];
   echo "grabbing $URL"
   set -x
   curl --fail --noproxy $SERV -s -S $URL -o $OUT_FILE
   set +x


I use a function to echo and run the command:

# Function to display commands
exe() { echo "\$ [email protected]" ; "[email protected]" ; }

exe echo hello world

Which outputs

$ echo hello world
hello world

For more complicated commands pipes, etc., you can use eval:

# Function to display commands
exe() { echo "\$ ${@/eval/}" ; "[email protected]" ; }

exe eval "echo 'Hello, World!' | cut -d ' ' -f1"

Which outputs

$  echo 'Hello, World!' | cut -d ' ' -f1