25

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 {}?

2
  • 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
27
  • 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.

1
  • 3
    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
6

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"
3

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.

3
  • One being standard and the other one not being standard I'd say is a pretty big difference. – Kusalananda Mar 18 at 20:51
  • Can you please explain what you mean by "standard"? – yaccob Mar 19 at 23:54
  • Sorry. My initial thought, before I noticed the bash tag, was that the variation with eval obviously was standard and the other wasn't, as it used non-POSIX source. You then proposed using another non-POSIX feature, the process substitution. Personally, I would say that if there is an equivalent standard way of doing something, that is to be preferred due to portability, while the bash-only variations may be usable in bash scripts that never need to be ported to other systems or shells. Do take my comment lightly as I didn't initially spot that this was about bash specifically. – Kusalananda Mar 20 at 7:21
0

There's one important distinction that hasn't been made yet!

❯ echo return | . /dev/stdin

❯ echo return | eval "$(cat -)"
sh: return: can only `return' from a function or sourced script

That is - return only works when sourcing a script.

Depending on the contents and intention of your script, it likely won't matter which you choose, but I figured this difference is worth pointing out.


In the above I pipe to eval instead of just an eval return for two reasons:

  1. Just to demonstrate it's possible to pipe into eval.

  2. For consistency so both source and eval can run within a subshell (the pipe).

    For example, if we tried something like:

    ❯ echo a=1 | . /dev/stdin; echo $a
    ❯ echo a=1 | eval "$(cat -)"; echo $a
    

    We'll find 1 isn't printed since it wasn't evaluated within our current environment. We'd have to do the following instead to get the 1 to print:

    ❯ echo a=1 | { . /dev/stdin; echo $a; }
    ❯ echo a=1 | { eval "$(cat -)"; echo $a; }
    

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.