19

Between the following alternatives...

  1. with eval.

    comd="ls"
    eval "$comd"
    
  2. with source /dev/stdin

    printf "ls" | source /dev/stdin
    
  3. with source /dev/stdin and ( ) or { }

    ( printf "ls" ) | source /dev/stdin
    { printf "ls"; } | source /dev/stdin
    

    (When we run printf in { }, is there any benefit other than not using subshell?)

    • What is the difference between them?

    • Which is preferred?

    • Which is the preferred way to run commands? () or {}?

  • 1
    I wouldn't recommend either approach. What are you actually trying to do, that you think you need to execute arbitrary code submitted by a user? – chepner Apr 6 '14 at 14:43
  • 2
    I also though they were executing arbitrary user input (as it is), until I read the question. But may be you are predicting that they will. – ctrl-alt-delor Jan 6 '17 at 17:46
19
  • What is the difference between the ways?

from bash manpage:

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 eval.  If there are
              no args, or only null arguments, eval returns 0.

source filename [arguments]
              Read and execute commands from filename  in  the  current  shell
              environment  and return the exit status of the last command exe‐
              cuted from filename.  If filename does not contain a slash, file
              names  in  PATH  are used to find the directory containing file‐
              name.  The file searched for in PATH  need  not  be  executable.
              When  bash  is  not  in  posix  mode,  the  current directory is
              searched if no file is found in PATH.  If the sourcepath  option
              to  the  shopt  builtin  command  is turned off, the PATH is not
              searched.  If any arguments are supplied, they become the  posi‐
              tional  parameters  when  filename  is  executed.  Otherwise the
              positional parameters are unchanged.  The return status  is  the
              status  of  the  last  command exited within the script (0 if no
              commands are executed), and false if filename is  not  found  or
              cannot be read.

There are no differences between the two ways.

There is only one note: eval concatenated all of its arguments, which is then run as a single command. source reads the contents of a file and executes them. eval can only build commands from its arguments, not stdin. So you can not do like this:

printf "ls" | eval
  • Which is more preferred?

Your example provides the same result, but the purpose of eval and source is different. source is usually used for providing a library for other scripts, while eval is used only to evaluate commands. You should avoid using eval if possible, because there is no guarantee that the evaled string is clean; we must do some sanity checks, using subshell instead.

  • If we run some commands in () or {}, which is more preferred?

When you run sequences commands inside curly brace { }, all commands are run in the current shell, instead of a subshell (which is the case if you run inside parentheses (see bash reference)).

Using subshell ( ) uses more resources, but your current environment is not affected. Using { } runs all the commands in the current shell, so your environment is affected. Depending on your purpose, you can choose one of them.

|improve this answer|||||
  • 2
    I think you misunderstood the question. Of course, you cannot replace eval by source. I guess the question is: Is eval "$cmd" equivalent to echo "$cmd" | source /dev/stdin. My current opinion is: yes. – Hauke Laging Apr 4 '14 at 3:59
3

The main difference is that 2nd and 3rd form are using a pipe, which will force bash to run the "source" command in a subshell (unless lastpipe is set, only available in bash 4.2+), which will make it pretty much equivalent to:

printf "ls" | bash

The consequences are that any environment variables set by your code will be lost, so this will not work as expected:

printf "abc=2" | source /dev/stdin

To run the commands in current shell, you can use process substitution:

source <(printf "abc=2")

You can put more commands inside parenthesis using semicolon as usual.

If you eliminate the pipe this way, I believe there is no difference between using "eval" and "source". You should prefer the one that is simpler to use in your particular case:

  • if you already have commands to run in variable, use "eval"
  • if you have them in a file or getting them from external command, use "source"
|improve this answer|||||
1

As a complement to the answers already given:

A source equivalent to ...

comd="ls"
eval "$comd"

... is ...

source <(printf ls)

In case of ls there is no significant difference.

But in case of a command that is intended to affect your current environment (what you usually intend when using source) this variant would do it (as your 1st solution with eval also would) while your 2nd approach just affects the environment of a subshell that won't be available after executing your line of code.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.