Bash – the difference between “eval” and “source /dev/stdin”

bashcommand line

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

Best Answer

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

Related Question