Issue
[user@localhost ~]$ declare -p test
declare -- test="eval \\\\\\\\; eval \\\\\\\\;"
[user@localhost ~]$ $test
-bash: \\: command not found
-bash: \: command not found
It seems like the second set of backslashes is being parsed twice as much as the first set. This happens for any number of backslashes I add.
Why?
Solution
It's easier to see the problem with echo
:
$ declare -- test="echo \\\\\\\\; echo \\\\\\\\;"
$ $test
\\\\; echo \\\\;
As you can see it does not run two echo
commands, it runs a single echo
and passes the rest of the string to it. This happens because $test
does not cause shell code to evaluate. It will instead do word splitting into a so-called simple command and invoke the result.
To get the effect you expected, evaluate it as a shell string:
$ eval "$test"
-bash: \: command not found
-bash: \: command not found
To reproduce the result without the variable, where one eval invokes the second one with one after interpreting the backslashes:
$ 'eval' '\\\\;' 'eval' '\\\\;'
-bash: \\: command not found
-bash: \: command not found
$ eval '\\\\; eval \\\\;' # Equivalent but more canonical
-bash: \\: command not found
-bash: \: command not found
Answered By - that other guy Answer Checked By - Senaida (WPSolving Volunteer)