In Python, if a string represents a statement, to execute it we have to use eval
.
In Bash, why is it not the same case?
$ cmd="ls"
$ $cmd
a.out company.png
Python and Bash are entirely different languages. The main purpose of Python is to execute internal statements with the added capability to execute external programs. The main purpose of the shell is to execute external programs, with some control structures and internal commands added. Bash has more internal features than the original shell, but still maintains compatibility.
One of the features of the shell is that you can define variables and have them later expanded in commands. Basically you can think that the shell implicitly uses something like Python's eval
for each statement, although there are of course differences.
There is also an eval
command to the shell that can be useful and dangerous if the normal level of processing and substitution isn't enough and you need an additional level. It evaluates the rest of the line and feeds the result to the normal substitutions the shell performs. Until you have a better understanding and an actual need for it, it's best to forget about this feature.
cmd=( "ls" "|" "cat")
"${cmd[@]}"
doesn't run by itself, but need eval "${cmd[@]}"
. cmd="ls | cat"
also need eval "$cmd"
. So when do I need eval
and when do I not need eval
to run a string or an array of strings as a command?
– Tim
Nov 20 '18 at 21:45
eval
fail to run a command while not using eval
will succeed?
– Tim
Nov 20 '18 at 21:48
eval
if the replacement string contains a shell special symbol, like ;
, &
, |
, $
, and you want it to have its special meaning instead of using it literally. Succeeding without eval
depends on the definition of "succeed". Commands with special characters will behave differently, although you may consider them to succeed in both cases.
– RalfFriedl
Nov 20 '18 at 23:05
eval "${cmd[@]}" > "$FILE"
, no matter how many quotes I add to it, will it always succeed for some ${cmd[@]}
, but fail for some other ${cmd[@]}
? In other ways, can one always construct some ${cmd[@]}
for it to fail?
– Tim
Nov 20 '18 at 23:12
-rwxrwxr-x
or1
orben
? – Romeo Ninov Nov 20 '18 at 19:56eval
does exist in bash, though its use is often discouraged. It does in fact execute a string as if the string was typed on the command line. Seeman bash
. What you're showing is executing something stored in an array, not a string, so I'm not sure what you're asking. – Kevin Kruse Nov 20 '18 at 19:59eval
before every command I typed into my shell, I would switch to a different shell. ;) Bash and Python serve different purposes, so their usage and syntax are different. After redirection, variable expansion, etc. anything on the command prompt is treated as a command. – Kevin Kruse Nov 20 '18 at 20:16