Between the following alternatives...

  1. with eval.

    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
    Commented Apr 6, 2014 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. Commented Jan 6, 2017 at 17:46

4 Answers 4


What is the difference between the ways?

From man bash:

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 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. eval should be avoided if possible, because there is no guarantee that the eval'ed string is clean; sanity checks must be done, using subshell.

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

When sequences of commands are run inside curly braces { }, 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 the current environment is not affected. Using { } runs all the commands in the current shell, so the environment is affected. Which to choose depends on your purpose.

  • 4
    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. Commented Apr 4, 2014 at 3:59
  • @cuonglm ... that has to be the most concise answer on any Stack-xxx site I've seen to date. :-)
    – will
    Commented Jan 30, 2023 at 13:39

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"

As a complement to the answers already given:

A source equivalent to ...

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.

  • One being standard and the other one not being standard I'd say is a pretty big difference.
    – Kusalananda
    Commented Mar 18, 2021 at 20:51
  • Can you please explain what you mean by "standard"?
    – yaccob
    Commented Mar 19, 2021 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
    Commented Mar 20, 2021 at 7:21

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; }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .