MS.*_*Kim 25 command-line bash
Between the following alternatives...
with eval.
comd="ls"
eval "$comd"
Run Code Online (Sandbox Code Playgroud)with source /dev/stdin
printf "ls" | source /dev/stdin
Run Code Online (Sandbox Code Playgroud)with source /dev/stdin and ( ) or { }
( printf "ls" ) | source /dev/stdin
{ printf "ls"; } | source /dev/stdin
Run Code Online (Sandbox Code Playgroud)
(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 {}?
cuo*_*glm 27
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.
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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.
当您在花括号内运行序列命令时{ },所有命令都在当前 shell中运行,而不是在子 shell 中运行(如果您在括号内运行就是这种情况(请参阅 bash参考))。
使用会subshell ( )占用更多资源,但不会影响您当前的环境。using 会{ }运行当前 shell 中的所有命令,因此您的环境会受到影响。根据您的目的,您可以选择其中之一。
小智 6
主要区别在于第 2 种和第 3 种形式使用管道,这将强制 bash 在子 shell 中运行“源”命令(除非设置了 lastpipe,仅在 bash 4.2+ 中可用),这将使其几乎等同于:
printf "ls" | bash
Run Code Online (Sandbox Code Playgroud)
结果是您的代码设置的任何环境变量都将丢失,因此这将无法按预期工作:
printf "abc=2" | source /dev/stdin
Run Code Online (Sandbox Code Playgroud)
要在当前 shell 中运行命令,您可以使用进程替换:
source <(printf "abc=2")
Run Code Online (Sandbox Code Playgroud)
您可以像往常一样使用分号将更多命令放在括号内。
如果您以这种方式消除管道,我相信使用“eval”和“source”之间没有区别。您应该更喜欢在您的特定情况下更易于使用的方法:
| 归档时间: |
|
| 查看次数: |
15371 次 |
| 最近记录: |