微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐