1

The "canonical" example of usefulness of POSIX shell eval built-in is this:

>foo=10
>x=foo
>eval y='$'$x
>echo $y
10

But the POSIX shell standard says:

The eval utility shall construct a command by concatenating arguments together, separating each with a <space> character. The constructed command shall be read and executed by the shell.

Indeed, I can't find any examples of non-trivial (that is, whose results cannot be achieved simpler) usage of eval with such contatenation?

Please show me.

Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
  • 2
    Voting to close as too broad. While many of the links in https://www.google.com/search?q=site%3Astackoverflow.com+shell+eval will be specific to `bash` or some other shell, there will be plenty of examples to amuse and enlighten you. – tripleee Jun 27 '16 at 05:18
  • @tripleee can you explain why you think this is "too broad"? The question is precise and specific. It serves to understand something not clear from the standard, not covered by example in the standard, and I did go over the example myself. – Mark Galeck Jun 27 '16 at 17:27
  • Mainly the "idle curiosity" language from https://stackoverflow.com/help/dont-ask - if you are trying to solve a concrete problem, what is it? If you would like to discuss, maybe post a separate question on [meta]. – tripleee Jun 27 '16 at 17:42
  • @tripleee I see your point, but I am going to respectfully disagree. It is not idle curiosity. I am trying to solve a concrete problem, which I cannot elaborate here, but for which, I have to perfectly understand `eval`. "Why some part of some manual is necessary and could not be omitted" is a very concrete problem to ask. – Mark Galeck Jun 27 '16 at 18:39
  • 1
    I do see from tripleee's comment, that in this link http://stackoverflow.com/questions/11065077/eval-command-in-bash-and-its-typical-uses there is an answer to my question. Because it is buried in there, I will simplify it here so maybe others can benefit: `y=$(eval echo \${$x})` – Mark Galeck Jun 27 '16 at 18:45
  • @tripleee What is curiosity to one person solves a concrete problem for another one. You seem to overlook that when you provide an answer on SO, you don't just provide it for the one person who asked. You provide an answer to everyone with the same question in the future and these people may have concrete problems to solve and don't even have to ask then but can directly find the answer. In over 9 of 10 cases I find answers on SO without even asking a question. – Mecki Jan 06 '23 at 22:14
  • @MarkGaleck `y=$(eval echo \${$x})` could also have been written as `y=$(eval "echo \${$x})"`, so this alone does not explain the need for the existence of the concatenation feature which this question is all about. Aside from the fact that the `echo` is stupid and one can just write `eval "y=\$$x"` which is the same as `eval y='$'$x`, unless the final variable content contains characters that `echo` can and shall interpret (like `\n` becoming a line break, but then you need to provide the `-e` option to `echo`). – Mecki Jan 06 '23 at 22:24

1 Answers1

0

Without that concatenation

cmdOutput=$( eval "$cmd" )

would work but

cmdOutput=$( eval $cmd )

would not if $cmd contains spaces. And while both commands will usually produce the same output, the first one creates a temporary string that is fed as single argument to eval and then destroyed again while the second one simply does not.

However, this can have an impact in certain corner cases, which is why I spoke of "usually" above:

ls_date=$( printf '%s\n%s' ls main.bash )
echo "1: $( eval $ls_date -l )"
echo
echo "2: $( eval "$ls_date -l" )"

Output:

1: -rwxrwxrwx 1 root root 112 Jan  6 23:31 main.bash

main.bash: line 5: main.bash: command not found
2: main.bash

In the first case, main.bash and -l are both treated as arguments to ls. In the second case ls and main.bash are treated as separate commands and -l would be an argument to the second command (yet main.bash was had no x-bit set and also was not in the path).

Last but not least: Syntax parser will try to parse unquoted syntax following eval but they typically won't parse any syntax in quotes except for $-expressions ($var, $(cmd), and $((<math>))).

Mecki
  • 125,244
  • 33
  • 244
  • 253