在Bash和其典型用途eval命令 [英] eval command in Bash and its typical uses

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

问题描述

阅读bash手册页和相对于该

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

我仍然无法理解完全评估命令做什么以及这将是其典型用途。例如,如果我们这样做:

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 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语法错误。大括号内,只能有一个变量名,与一些可能的preFIX和后缀,但你不能有任意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变量值的所有设置)并收集其输出。因此,回声$($ 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回声\\ $ {$ N} 运行传递给评估的参数。扩建后,该参数是回声 $ {1} 。因此, EVAL回声\\ $ {$ N} 运行呼应命令$ {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}.

请注意,大部分的时间,因此必须将变量替换和命令susbtitutions双引号(即随时有一个 $ ):$富,$(富)始终把周围的变量和命令替换双引号,除非你知道你需要离开他们。如果没有双引号,外壳进行场分裂(即它将该变量或命令的输出为单独的字值),然后将每个字作为通配符模式。例如:

Note that most of the time, you must use double quotes around variable substitutions and command susbtitutions (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* *

评估不是很经常使用。在一些贝壳,最常见的用途是获得他的名字不知道,直到运行时变量的值。在bash中,这是没有必要感谢 $ {!VAR} 语法。 评估仍然是有用的,当你需要构建包含运营商,保留字等较长的命令。

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天全站免登陆