word=$word"`expr substr '${ -b :board}' 1 3`" 的含义?
Meaning of word=$word"`expr substr '${ -b :board}' 1 3`"?
我正在处理一些 bash 到 C++ 的脚本转换并遇到了这一行...
word="yahoo"
word=$word"`expr substr '${ -b :board}' 1 3`"
我理解 expr substr 的作用,但我提供的参数“${ -b :board}”对我来说没有任何意义。
当我在终端上尝试运行时:
echo $word
输出:
yahoo${
如果有任何意见,我将不胜感激,谢谢。
这个问题不是关于 Bash 或 sh,而是关于 expr
,一个独立的命令,它是 GNU Coreutils 的一部分。如果我们查阅 the manual,我们会发现
expr
evaluates an expression and writes the result on standard output. Each token of the expression must be a separate argument.
和
substr <i>string position length</i>
Returns the substring ofstring
beginning atposition
with length at mostlength
. If eitherposition
orlength
is negative, zero, or non-numeric, returns the null string.
所以命令
expr substr '${ -b :board}' 1 3
获取字符串 ${ -b :board}
并提取长度为 3 的子字符串,从位置 1 开始,即 ${
.
命令
word=$word"`expr substr '${ -b :board}' 1 3`"
将 expr
命令放入命令替换(反引号)并将结果附加到 $word
的扩展,此时包含 yahoo
,这就是您的方式结束 yahoo${
.
话虽这么说,但我看不出这样做的理由。 expr
命令的输出是一个常量字符串,因此您实际上可以将所有内容替换为
word='yahoo${ '
附带说明一下,在现代 Bash 中,您可以通过参数扩展获得相同的功能:
word='yahoo'
var='${ -b :board}'
word+=${var:0:3}
但结果是一样的,没有更多上下文似乎一开始就没有任何意义。