Questions tagged [eval]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
202
votes
7answers
523k views

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..
75
votes
1answer
21k views

Why eval the output of ssh-agent?

In order to run ssh-agent I have to use eval $(ssh-agent) Why do I have to eval the output of ssh-agent? Why isn't it designed so that I can just run it? Note: Backticks (`) removed as they are ...
16
votes
4answers
25k views

How can I list all user names and/or home directories?

I want to list all the users dirs 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 that machines they don't call it home (...
9
votes
2answers
239 views

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. ...
7
votes
1answer
4k views

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 ...
6
votes
1answer
2k views

eval vs. pipe through bash

What is the difference between using: eval 'echo "foo"' and echo 'echo "foo"' | bash is there any?
6
votes
2answers
12k views

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 ...
6
votes
4answers
25k views

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 ...
5
votes
3answers
6k views

Why and when should eval use be avoided in shell scripts? [duplicate]

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 ...
5
votes
1answer
809 views

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 ...
4
votes
2answers
2k views

Bash: Why is used eval and shift 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 : #!...
4
votes
1answer
266 views

How to deal with insecurity of `eval` evaluating external input in a script?

Binaryzebra said this script is evaluating external input, a highly insecure practice. What are some ways to deal with or even replace eval evaluating external inputs in a script? Thanks.
3
votes
1answer
390 views

What does eval X=\$$i mean in UNIX?

I have a small script with the following lines echo mom,dad |awk -F, '{print $1,$2}' | while read VAR1 VAR2 do for i in VAR1 VAR2 do eval X=\$$i echo $X done done OUTPUT: mom dad What ...
2
votes
2answers
106 views

building an alias from a function

I need a lot of different aliases to create ssh tunnels to different servers. To give you a few of them: alias tunnel_1='autossh -M 20000 -N -L 8080:localhost:8080 -N -L 9200:localhost:9200 -N -L ...
2
votes
3answers
900 views

Using the eval command twice

Suppose: a=b; b=c; c=d Then eval echo \$$a produces the output: c If we want to extract the output d using just input a, I tried the following way: (i) eval echo \$(eval echo \$$a) produces the ...
2
votes
3answers
141 views

Get contents of passing string from a script

I would like to get, in the output, the content of KW0_TEXT and KW1_TEXT from the "for" of this script: #!/bin/sh STRS=" KW0 KW1 " KW0_TEXT="text text text" KW1_TEXT="text text text text" for str ...
2
votes
2answers
275 views

Bash: How to kill eval if the process that receives its output terminates

I have a little ugly bash script on my Ubuntu machine that contains the lines: search_command="find -L $(printf "%q" "$search_folder") \( ! -regex '.*/\..*/..*' \) -mindepth 1 2> /dev/null" for i ...
2
votes
1answer
208 views

How to execute something in current shell [duplicate]

I want to create a bash script or something similar that I can execute in the current shell: echo '#!/bin/bash export foo="bar" ' > zoom.sh but if I want to run this script in the current ...
2
votes
2answers
224 views

Does `eval` apply the shell to the redirection part of the following command?

When use eval on a command, does eval apply the shell twice to the redirection part of the command? Suppose the filename in the redirection contains a whitespace, if eval applies all the steps of ...
2
votes
1answer
1k views

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 ...
1
vote
2answers
4k views

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 # ...
1
vote
2answers
415 views

eval not evaluating echo? [duplicate]

I'm trying to write something that stores an API access token in a bash variable, so I'm modelling it on ssh-agent, where you use eval. ssh-agent If you run ssh-agent by itself, it outputs something ...
1
vote
2answers
81 views

cksum files using substituted variable string as a pattern

Can I avoid eval in the below example or is there no other way? The list of files on variable is dynamic, and I want to cksum all of them. There are many other files in the directory, but they should ...
1
vote
2answers
1k views

Why is eval not working with command substitution? [closed]

I have this command $("${DIR}/test.py" "$1") the output of which is export SOME_VAR="hello world" export ANOTHER_VAR="hello" But eval $("${DIR}/test.py" "$1") seems to do nothing. What am I ...
1
vote
1answer
51 views

Why does this bash idiom require eval?

Why does this produces _results=""... _results="$( grep ${_gopts[@]} )" And this produces the desired _results (list of SSHFS entries in fstab)... _results="$( eval grep ${_gopts[@]} )" The _gopts ...
1
vote
1answer
1k views

Will “dircolors” work here instead of “ eval `”dircolors`“` ”?

I was checking .bashrc to set colors for ls comand and found this export SHELL='/bin/bash' export LS_OPTIONS='--color=auto' eval "`dircolors`" alias ls='ls $LS_OPTIONS' Will there be any problem ...
1
vote
3answers
745 views

Get return code of first piped command using eval?

I wish to execute different commands and check the return code afterwards before moving to the next steps in the script. At the same time I also wish to log the output of the executed commands to a ...
1
vote
1answer
122 views

zsh: A prefix function that runs the rest of the line with no alias expansion

I am thinking of: function noalias() { setopt localoptions noexpandalias "$@" } But I don't know what the last line should be. eval "${*:q}"? eval "$@"? eval "${@:q}"? "${@:q}"? ${@:q}? $@? (...
1
vote
1answer
249 views

how to evaluate an dynamically generated if elif else statement in shell

im having trouble getting a dynamically generated shell function to evaluate properly (by evaluate i mean "eval" command) this is the desired statement to be evaluated when ran (except the output ...
0
votes
3answers
921 views

Is eval implemented by creating a shell process to evaluate the following command?

In eval <command>, how does eval evaluate the following command? does eval create a shell subprocess to evaluate <command>? If not, does eval rely on the current shell process to ...
0
votes
2answers
2k views

Error when eval a for-loop

I am writing a script which accepts two arguments: #! /bin/bash eval for i in {$1..$2}; do echo $i; done I run it like: $ ./myscript 0002 0010 syntax error near unexpected token `do' Why is ...
0
votes
2answers
391 views

eval ssh command in variable

I am trying to execute commands via ssh remotely. But I have the calling command saved as variable such as: SSH_CMD="ssh -i path_to/identity" SSH_USER=user SSH_DST=server dir1=dir1 dir2=dir2 now if ...
0
votes
1answer
610 views

why doesn't eval declare in a function work in bash?

Tracking down strange behavior a bash script resulted in the following MWE: set -o errexit set -o nounset set -x my_eval() { eval "$1" } my_eval "declare -A ASSOC" ASSOC[foo]=bar echo success ...
0
votes
1answer
44 views

How to check “eval” command?

My code docker-machine create --driver virtualbox dev VBoxManage list vms "minikube" {9c326ed5-faf4-42fe-acda-bf3a283f1a74} "kalinew" {de6de631-0d51-4638-b967-66db463cbf05} "dev" {84a116bf-02b9-...
0
votes
1answer
765 views

script set command output to variable [duplicate]

I'm writing a simple script that prompts the user for information and saves it as a variable to be passed as arguments in another command. One of the variables stores a path which may include a space. ...
0
votes
1answer
111 views

Why does parameter expansion happen before brace expansion in this example?

From https://unix.stackexchange.com/a/7739/674, why does parameter expansion happen before brace expansion in the following example? eval rm foo.{$ext0..$extN}
0
votes
3answers
730 views

diff two files from a command output [closed]

I have a bunch of xml file in the current directory. Problem 1. As far as I have read eval returns the results and stores it in a variable. But I get an error with the below command find ./ -name '...
0
votes
1answer
46 views

Evaluate part of a text line

I have an expression of the form @(<date calculation>) that I want to replace with the result of date +%s --date "now<date calculation>". So for example @(-1 day) would be replaced with ...
0
votes
1answer
103 views

Process Substitution (e. g. $(somecommand) ) In A Nutshell

On my shell from home directory on mac, I run the following eval "ls" and this just shows me the directories. But if I run eval "$(ls)" I get the following output: zsh: command not found: ...
0
votes
1answer
176 views

Weird behavior of su command

Everytime I run su command on this box, I constantly see the weird messages below. Switching user do eventually continue, but why are these messages showing? <Server>:~ # su - -bash: eval: line ...
0
votes
2answers
707 views

Eval and exec with variable substitution

I'd like to execute a statement to start a server. For that I have an environment variable to determine which server is to be started. I was given this command as a starting point: eval "exec ...
0
votes
0answers
25 views

How do I build a bash command with variables without eval? [duplicate]

I am trying to learn Bash scripting by making my own bash scripts, and i've spent a really long time trying to figure out why one of mine doesn't work, but for the life of me couldn't! I want to ...
0
votes
0answers
45 views

Can I avoid using eval in alias re-definition?

My system: Linux Mint 19.3. My default shell: Bash. Problem: I do not like using eval (man page) if unnecessary. Question: Is there another way of redefining stored alias, which I currently do as ...
0
votes
1answer
775 views

Trying to export a path from inside a bash script [duplicate]

I am trying to put my whole environment set up into a single bash file. I am only hitting one issue. When I try to export a new path from inside the file it is not setting the path in the environment ...
0
votes
1answer
970 views

dollar sign inside eval string in bash

I have hundreds of sub-directories which contain a dollar sign (i.e., $) as the 1st character in their names, each of which needs to be visited. These sub-directories can not be renamed. The bash ...
-3
votes
1answer
122 views

Eval not working

i try run this code to get random number but eval seems not working this is my code #!/bin/bash # Sequencia alfanumerica de 12 carateres aleatoriamente (maiusculas e minusculas) file="/home/...
-3
votes
1answer
201 views

What is the purpose of eval? [duplicate]

What is the purpose of running a command with eval? What kinds of commands can eval run? I find it can run builtin command, external executable command, pipeline, script. Can it run all kinds of ...
-3
votes
2answers
99 views

Loop through filenames without eval [closed]

In a shell script, if we want to loop over certain filenames, which we would get by shell globbing, as for example all MKV-files in all sub-directories starting with string "Example" – how can we do ...