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

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


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 -xcnicutar@shell:~/dir$ ls+ ls --color=autoa  b  c  d  e  f


To answer the second part of your question, here's a shell function that does what you want:

echo_and_run() { echo "$*" ; "$@" ; }

I use something similar to this:

echo_and_run() { echo "\$ $*" ; "$@" ; }

which prints $ in front of the command (it looks like a shell prompt and makes it clearer that it's a command). I sometimes use this in scripts when I want to show some (but not all) of the commands it's executing.

As others have mentioned, it does lose quotation marks:

$ echo_and_run echo "Hello, world"$ echo Hello, worldHello, world$ 

but I don't think there's any good way to avoid that; the shell strips quotation marks before echo_and_run gets a chance to see them. You could write a script that would check for arguments containing spaces and other shell metacharacters and add quotation marks as needed (which still wouldn't necessarily match the quotation marks you actually typed).


It's possible to use bash's printf in conjunction with the %q format specifier to escape the arguments so that spaces are preserved:

function echo_and_run {  echo "$" "$@"  eval $(printf '%q ' "$@") < /dev/tty}