Echo command, and then run it? (Like make)

mjs picture mjs · Sep 2, 2012 · Viewed 24.5k times · Source

Is there some way to get bash into a sort of verbose mode where, such that, when it's running a shell script, it echoes out the command it's going to run before running it? That is, so that it's possible to see the commands that were run (as well as their output), similar to the output of make?

That is, if running a shell script like

echo "Hello, World"

I would like the following output

echo "Hello, World"
Hello, World

Alternatively, is it possible to write a bash function called echo_and_run that will output a command and then run it?

$ echo_and_run echo "Hello, World"
echo "Hello, World"
Hello, World

Answer

cnicutar picture cnicutar · Sep 2, 2012

You could make your own function to echo commands before calling eval.

Bash also has a debugging feature. Once you set -x bash will display each command before executing it.

cnicutar@shell:~/dir$ set -x
cnicutar@shell:~/dir$ ls
+ ls --color=auto
a  b  c  d  e  f