Reputation: 695
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
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
Reputation: 158130
It's pretty simple, you can just do:
comm='pwd'
# just execute
$comm
# fetch output into variable
output=$($comm)
Upvotes: 2
Reputation: 11588
Yes, just surround the sub-command in backticks (the ` character):
comm=`pwd`
Upvotes: 0