[英]Linux Bash's rule for determing a variable name Greedy or Non-Greedy
如果您在Ubuntu 12.04上运行以下bash脚本:
t="t"
echo "1st Output this $15"
echo "2nd this $test"
输出是:
1st Output this 5
2nd this
第一个echo
如何将$1
作为变量名称(非贪婪)将其解释为${1}5
而第二个echo
将$test
作为变量名称(贪婪)而不是将其解释为${t}est
$test
?
您的问题分为两部分:
$15
总是被解释为$1
,即第一个位置参数1 ,与5
连接。 为了使用第十五个位置参数,你需要说${15}
。
$test
将被解释为变量test
。 因此,如果您希望它被解释为$t
与est
连接,那么您需要说${t}est
1从info bash
引用:
When a positional parameter consisting of more than a single digit is
expanded, it must be enclosed in braces (see EXPANSION below).
...
EXPANSION
Expansion is performed on the command line after it has been split into
words. There are seven kinds of expansion performed: brace expansion,
tilde expansion, parameter and variable expansion, command substitu‐
tion, arithmetic expansion, word splitting, and pathname expansion.
您可能还想参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.