系统相关
首页 > 系统相关> > Linux Bash确定变量名称Greedy或Non-Greedy的规则

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

作者:互联网

如果您在Ubuntu 12.04上运行以下bash脚本:

t="t"
echo "1st Output this $15"
echo "2nd this $test"

输出为:

1st Output this 5
2nd this

第一个回声如何将$1作为变量名(非贪婪)将其解释为${1} 5,而第二个回声又如何将$test作为变量名(贪婪)而不是将其解释为${t} est?

解决方法:

您的问题分为两部分:

> $15将始终被解释为$1,即第一个位置参数1与5串联.要使用第15个位置参数,您需要说${15}.
> $test将被解释为变量测试.因此,如果您希望将其解释为与est串联的$t,则需要说出${t} est

1从信息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.

您可能还需要参考:

> What’s the difference between ${varname} and $varname in a shell scripts

标签:bash,ubuntu,ubuntu-12-04,linux
来源: https://codeday.me/bug/20191121/2054813.html