Questions tagged [eval]
74 questions
313
votes
10 answers
What is the "eval" command in bash?
What can you do with the eval command? Why is it useful? Is it some kind of a built-in function in bash? There is no man page for it..
LanceBaynes
- 39,295
- 97
- 250
- 349
136
votes
1 answer
Why eval the output of ssh-agent?
In order to run ssh-agent I have to use:
eval $(ssh-agent)
Why is it necessary to eval the output of ssh-agent? Why can't I just run it?
jx12345
- 1,570
- 2
- 10
- 12
20
votes
4 answers
How can I list all user names and/or home directories?
I want to list all the users’ directories on the machine. Usually, I will do:
ls -l /home
But I use it in a script that will be deployed on others’ machines and maybe on those machines they don't call it home (e.g. myHome).
So I want to generalize…
lakerda
- 355
- 1
- 3
- 7
16
votes
10 answers
What are some useful use-cases for eval in Shell?
You hear it a lot eval is evil, whether it's in Shell/POSIX world, or in other langs like python etc...
But I'm wondering, is it actually useless? or is it there some arcane, non-documented, interesting or just useful use-cases for it?
Would prefer…
Nordine Lotfi
- 2,200
- 12
- 45
12
votes
2 answers
Behaviour of "eval" under "set -e" in conditional expression
Consider the commands
eval false || echo ok
echo also ok
Ordinarily, we'd expect this to execute the false utility and, since the exit status is non-zero, to then execute echo ok and echo also ok.
In all the POSIX-like shells I use (ksh93, zsh,…
Kusalananda
- 320,670
- 36
- 633
- 936
11
votes
2 answers
return value from eval
The bash manual states:
eval [arg ...]
The args are read and concatenated together into a single com-
mand. This command is then read and executed by the shell, and
its exit status is returned as the value of…
Viesturs
- 933
- 3
- 10
- 15
10
votes
3 answers
Bash: Why is eval and shift used in a script that parses command line arguments?
As I was looking this answer https://stackoverflow.com/a/11065196/4706711 in order to figure out on how to use parameters like --something or -s some questions rised regarding the answer's script :
#!/bin/bash
TEMP=`getopt -o ab:c:: --long…
Dimitrios Desyllas
- 979
- 3
- 11
- 27
10
votes
1 answer
How to save an alias of an eval $(other_comand) command
I've been using docker for a while and there's a command I write each time I boot up my docker:
eval $(docker-machine env)
I know eval shouldn't be used unless necessary, but it's mentioned by the following:
docker-machine env outputs environment…
GabLeRoux
- 885
- 9
- 17
9
votes
3 answers
Why and when should eval use be avoided in shell scripts?
Many answers and comments on this site mention that one should generally avoid using eval in shell scripts. Often these comments are addressed to beginners.
I've seen mention of security issues and robustness, but very few examples.
I already…
Wildcard
- 35,316
- 26
- 130
- 258
7
votes
4 answers
bash + read variables & values from file by bash script
I have the following file variable and values
# more file.txt
export worker01="sdg sdh sdi sdj sdk"
export worker02="sdg sdh sdi sdj sdm"
export worker03="sdg sdh sdi sdj sdf"
I perform source in order to read the variable
# source…
yael
- 12,598
- 51
- 169
- 303
6
votes
1 answer
eval vs. pipe through bash
What is the difference between using:
eval 'echo "foo"'
and
echo 'echo "foo"' | bash
is there any?
Alexander Mills
- 9,330
- 19
- 95
- 180
6
votes
2 answers
Why is using eval necessary to pass quoted arguments
I have the following example.
#!/bin/bash
ARGUMENTS="-executors 1 -description \"The Host\" "
# call1
# error: parameter Host" is not allowed
java -jar swarm-client.jar $ARGUMENTS
# call2
# works fine with eval
eval java -jar swarm-client.jar…
Skip
- 319
- 2
- 4
- 14
6
votes
2 answers
Can bash expand a quoted and/or escaped string variable into words?
I have a bash shell variable containing a string formed of multiple words delimited by whitespace. The string can contain escapes, such as escaped whitespace within a word. Words containing whitespace may alternatively be quoted.
A shell variable…
starfry
- 7,302
- 6
- 47
- 69
5
votes
1 answer
File with parentheses/brackets in working directory causes eval error
I encountered a strange bug today, when running a script in a directory containing a directory with parentheses in it, such as a().
Minimal working example
I managed to reduce the bug to the following minimal working example:
Create an empty…
KIAaze
- 767
- 1
- 8
- 18
4
votes
1 answer
How to conditionally redirect the output to files based on variable in bash
I'm trying to use eval command to eval a comment -- I'm not sure if this is the right way to do it. Example:
i=?? (What I want here is either a #, to comment what's after, or blank)
somecommand arg1 arg2 $(eval $i) >> file
So based on the $i value…
Raywando
- 427
- 1
- 4
- 7