Why does the following script work as expected (prints hello
)
#!/bin/bash
foo=$(bash -c 'echo hello')
echo $foo
while this script:
#!/bin/bash
cmd="bash -c 'echo hello'"
foo=$($cmd)
echo $foo
gives the following error:
hello': -c: line 0: unexpected EOF while looking for matching `''
hello': -c: line 1: syntax error: unexpected end of file
In,
cmd="bash -c 'echo hello'"
$cmd
You're not running the bash -c 'echo hello'
command, you're running the $cmd
simple command.
That unquoted $cmd
means invoking the split+glob operator. Here, with the default value of $IFS
, the content of $cmd
is split into bash
, -c
, 'echo
and hello'
. So, you're running bash
with those 4 arguments, it's as if you had typed:
bash -c "'echo" "hello'"
And that 'echo
code has a missing closing quote (the hello'
argument goes into the $0
of that inline script).
If you want to evaluate the content of $cmd
as shell code, it's
eval "$cmd"
So:
cmd="bash -c 'echo hello'"
foo=$(eval "$cmd")
echo "$foo"
Though you could also use your split+glob operator differently:
cmd='bash,-c,echo hello'
IFS=, # split on comma
set -f # disable glob
foo=$($cmd)
echo "$foo"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments