Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH - commands string-like

Tags:

bash

command

pwd

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?

like image 321
user1926550 Avatar asked Feb 12 '26 08:02

user1926550


2 Answers

It's pretty simple, you can just do:

comm='pwd'
# just execute
$comm
# fetch output into variable
output=$($comm)
like image 196
hek2mgl Avatar answered Feb 13 '26 21:02

hek2mgl


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

like image 32
Gilles Quenot Avatar answered Feb 13 '26 20:02

Gilles Quenot