Consider the following:
#!/bin/tcsh
set thing = 'marker:echo "quoted argument"'
set a = `echo "$thing" | sed 's/\([^:]*\):\(.*\)/\1/'`
set b = `echo "$thing" | sed 's/\([^:]*\):\(.*\)/\2/'`
echo $a
echo $b
$b
echo "quoted argument"
This gives
marker
echo "quoted argument"
"quoted argument"
quoted argument
If $b
is echo "quoted argument"
, why does evaluating $b
give a different result from echo "quoted argument"
?
Since I know tcsh
is awful (but it's what I have to use for work), here is the same problem in Bash:
thing='marker:echo "quoted argument"'
a=`echo "$thing" | sed 's/\(.*\):\([^:]*\)/\1/'`
b=`echo "$thing" | sed 's/\(.*\):\([^:]*\)/\2/'`
echo $a
echo $b
$b
echo "quoted argument"
The output is the same. Note that, were I doing this in Bash, I would certainly use a map. I do not have that luxury :)
. The solution must work in tcsh
.
Desired Output
I would like $b
to behave just as if I typed the command in myself as I see it:
marker
echo "quoted argument"
quoted argument
quoted argument
This is a follow-up question to Accessing array elements with spaces in TCSH.
One thing you must keep in mind with command substitution is that each
pipe
and eachcommand
you string together executes within its own subshell. Each time that happens, the shell process the command or string you provide:In the case of
b
, you are assigning the return fromsed
exactly as it is returned tob
including the quotes. They become part ofb
. Soecho $b
is equivalent toecho '"quoted argument"'
. Whereas, yourecho "quoted argument"
prints the string as the characters contained within the quotes, the shell removing the literal quotes.Sorry for the initial confusion.
Yeah,
eval
is the "solution" here (well the solution is not to have a command in a string in the first place see http://mywiki.wooledge.org/BashFAQ/050 for more).The reason you see the quotes when you run
$b
is because of the order of evaluation of a shell command. The very last thing that the shell does, after all other expansions, is to remote quotes (however it doesn't remove quotes that resulted from any of the expansions).So when you have
b='echo "quoted arguments"'
and run$b
as the command line what happens is that the variable is expanded so you getecho "quoted arguments"
and then that is run as-is.