I would like to store a command to use at a later period in a variable (not the output of the command, but the command itself)
I have a simple script as follows:
command="ls";
echo "Command: $command"; #Output is: Command: ls
b=`$command`;
echo $b; #Output is: public_html REV test... (command worked successfully)
However, when I try something a bit more complicated, it fails. For example, if I make
command="ls | grep -c '^'";
The output is:
Command: ls | grep -c '^'
ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
ls: cannot access '^': No such file or directory
Any idea how I could store such a command (with pipes/multiple commands) in a variable for later use?
Do not use
eval
! It has a major risk of introducing arbitrary code execution.BashFAQ-50 - I'm trying to put a command in a variable, but the complex cases always fail.
Put it in an array and expand all the words with double-quotes
"${arr[@]}"
to not let theIFS
split the words due to Word Splitting.and see the contents of the array inside. The
declare -p
allows you see the contents of the array inside with each command parameter in separate indices. If one such argument contains spaces, quoting inside while adding to the array will prevent it from getting split due to Word-Splitting.and execute the commands as
(or) altogether use a
bash
function to run the command,and call the function as just
POSIX
sh
has no arrays, so the closest you can come is to build up a list of elements in the positional parameters. Here's a POSIXsh
way to run a mail programIts is not necessary to store commands in variables even as you need to use it later. just execute it as per normal. If you store in variable, you would need some kind of
eval
statement or invoke some unnecessary shell process to "execute your variable".Using this method, the command is immediately evaluated and it's return value is stored.
Same with backtick
Using eval in the
$(...)
will not make it evaluated laterUsing eval, it is evaluated when
eval
is usedIn the above example, if you need to run a command with arguments, put them in the string you are storing
For bash scripts this is rarely relevant, but one last note. Be careful with
eval
. Eval only strings you control, never strings coming from an untrusted user or built from untrusted user input.Use eval: