繁体   English   中英

Linux Bash用于确定变量名称Greedy或Non-Greedy的规则

[英]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 因此,如果您希望它被解释为$test连接,那么您需要说${t}est

1info 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM