user1926550
user1926550

Reputation: 695

BASH - commands string-like

I am wondering if it is possible in BASH to use commands in this way:

lets take command pwd

Lets say I would like to have a variable comm="pwd" and then use it somewhere in program, but when I use it, I get the real pwd command output. Is this even possible?

Upvotes: 3

Views: 273

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185560

Try doing this :

var=$(pwd)

The backquote (`) is used in the old-style command substitution, e.g.

foo=`command`

The

foo=$(command)

syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. See http://mywiki.wooledge.org/BashFAQ/082

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 158130

It's pretty simple, you can just do:

comm='pwd'
# just execute
$comm
# fetch output into variable
output=$($comm)

Upvotes: 2

Stuart M
Stuart M

Reputation: 11588

Yes, just surround the sub-command in backticks (the ` character):

comm=`pwd`

Upvotes: 0

Related Questions