「eval」と「source /dev/stdin」の違いは何ですか?

「eval」と「source /dev/stdin」の違いは何ですか?

次のオプションの間で...

  1. そしてeval

    comd="ls"
    eval "$comd"
    
  2. そしてsource /dev/stdin

    printf "ls" | source /dev/stdin
    
  3. そしてsource /dev/stdinそして( )または{ }

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

    printf実行時に{ }サブシェルを使用しないことに加えて、どのような利点がありますか?)

    • それらの違いは何ですか?

    • どちらが好まれますか?

    • コマンドを実行するための好ましい方法は何ですか?()または{}

ベストアンサー1

さまざまな方法の違いは何ですか?

から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.

2つの方法の間に違いはありません。

注:evalすべての引数を連結して単一のコマンドで実行してください。sourceファイルの内容を読み取り、実行します。evalコマンドは引数としてのみ作成できますstdin。だからあなたはこれを行うことはできません:

printf "ls" | eval

どちらがより好ましいですか?

evalあなたの例は同じ結果を提供しますが、目的は異なりますsourcesource通常、他のスクリプトのライブラリを提供するために使用されますが、evalコマンドの評価にのみ使用されます。 'ed文字列はきれいであると保証されていないため、eval可能であれば避けるべきです。evalsubshell

()またはでいくつかのコマンドを実行すると、{}どちらがより望ましいですか?

コマンドシーケンスが中括弧内で実行されると、{ }すべてのコマンドが実行されます。現在のシェル、変えるサブシェル(これは括弧内で実行すると発生します(bashを参照)。引用する)).

これにより、subshell ( )より多くのリソースが必要ですが、現在の環境には影響しません。を使用すると、{ }現在のシェルのすべてのコマンドが実行されるため、環境が影響を受けます。どちらを選択するかは目的によって異なります。

おすすめ記事