I'm building an enumeration script that has a list of commands to run such as:


I want to output to the screen the command being run, but due to variable expansion, it gets substituted to the actual command and bash proceeds to execute it.

So the desired effect is to have a line in the script such as:

echo Currently Running: [$cmd]

and have that echo out to the screen:

Currently Running [whoami]


Currently running [root]

How does one escape the command but still get expansion to stdout?

  • 2
    I think we could give better answers if we saw how you were doing it now.
    – Jeff Schaller
    Oct 20, 2017 at 0:09
  • This answer marked works as intended. I was shipping the $cmd to a function that used printf and it was causing it to execute.
    – HashHazard
    Oct 20, 2017 at 15:04

1 Answer 1


Use the variable twice:

tomas@tomas:~$ command=ls
tomas@tomas:~$ echo Currently Running: [$command] && $command
Currently Running: [ls]
cgoban.jar  Documents  Music     Public     Videos
Desktop     Downloads  Pictures  Templates

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .