Bash 中的 eval 命令及其典型用途 [英] eval command in Bash and its typical uses

查看:84
本文介绍了Bash 中的 eval 命令及其典型用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了 bash 手册页和关于这个 post.

After reading the bash man pages and with respect to this post.

我仍然无法理解 eval 命令的作用以及它的典型用途.例如,如果我们这样做:

I am still having trouble understanding what exactly the eval command does and which would be its typical uses. For example if we do:

bash$ set -- one two three  # sets $1 $2 $3
bash$ echo $1
one
bash$ n=1
bash$ echo ${$n}       ## First attempt to echo $1 using brackets fails
bash: ${$n}: bad substitution
bash$ echo $($n)       ## Second attempt to echo $1 using parentheses fails
bash: 1: command not found
bash$ eval echo ${$n} ## Third attempt to echo $1 using 'eval' succeeds
one

这里到底发生了什么,美元符号和反斜杠如何与问题联系起来?

What exactly is happening here and how do the dollar sign and the backslash tie into the problem?

推荐答案

eval 将字符串作为其参数,并像在命令行中键入该字符串一样对其进行计算.(如果你传递多个参数,它们首先用它们之间的空格连接.)

eval takes a string as its argument, and evaluates it as if you'd typed that string on a command line. (If you pass several arguments, they are first joined with spaces between them.)

${$n} 是 bash 中的语法错误.在大括号内,您只能有一个变量名,带有一些可能的前缀和后缀,但不能有任意的 bash 语法,特别是不能使用变量扩展.有一种说法是名称在这个变量中的变量的值",不过:

${$n} is a syntax error in bash. Inside the braces, you can only have a variable name, with some possible prefix and suffixes, but you can't have arbitrary bash syntax and in particular you can't use variable expansion. There is a way of saying "the value of the variable whose name is in this variable", though:

echo ${!n}
one

$(...) 在子 shell 中运行括号内指定的命令(即在一个单独的进程中,从当前 shell 继承所有设置,例如变量值),并收集其输出.所以 echo $($n)$n 作为 shell 命令运行,并显示其输出.由于 $n 的计算结果为 1$($n) 尝试运行命令 1,该命令不会存在.

$(…) runs the command specified inside the parentheses in a subshell (i.e. in a separate process that inherits all settings such as variable values from the current shell), and gathers its output. So echo $($n) runs $n as a shell command, and displays its output. Since $n evaluates to 1, $($n) attempts to run the command 1, which does not exist.

eval echo ${$n} 运行传递给 eval 的参数.展开后的参数为echo${1}.所以 eval echo ${$n} 运行命令 echo ${1}.

eval echo ${$n} runs the parameters passed to eval. After expansion, the parameters are echo and ${1}. So eval echo ${$n} runs the command echo ${1}.

请注意,大多数情况下,您必须在变量替换和命令替换周围使用双引号(即任何时候都有 $):"$foo", "$(foo)".始终在变量和命令替换周围加上双引号,除非您知道需要将它们去掉.如果没有双引号,shell 将执行字段拆分(即将变量的值或命令的输出拆分为单独的单词),然后将每个单词视为通配符模式.例如:

Note that most of the time, you must use double quotes around variable substitutions and command substitutions (i.e. anytime there's a $): "$foo", "$(foo)". Always put double quotes around variable and command substitutions, unless you know you need to leave them off. Without the double quotes, the shell performs field splitting (i.e. it splits value of the variable or the output from the command into separate words) and then treats each word as a wildcard pattern. For example:

$ ls
file1 file2 otherfile
$ set -- 'f* *'
$ echo "$1"
f* *
$ echo $1
file1 file2 file1 file2 otherfile
$ n=1
$ eval echo ${$n}
file1 file2 file1 file2 otherfile
$eval echo "${$n}"
f* *
$ echo "${!n}"
f* *

eval 不经常使用.在某些 shell 中,最常见的用途是获取直到运行时才知道名称的变量的值.在 bash 中,由于 ${!VAR} 语法,这不是必需的.eval 在您需要构造包含运算符、保留字等的更长命令时仍然很有用.

eval is not used very often. In some shells, the most common use is to obtain the value of a variable whose name is not known until runtime. In bash, this is not necessary thanks to the ${!VAR} syntax. eval is still useful when you need to construct a longer command containing operators, reserved words, etc.

这篇关于Bash 中的 eval 命令及其典型用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆