1

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

hostname
whoami
uid
etc...

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]

NOT

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 '17 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 '17 at 15:04
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.