免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 11261 | 回复: 22
打印 上一主题 下一主题

[系统管理] bash中${VAR}和$VAR最本质的区别是什么? [复制链接]

论坛徽章:
22
丑牛
日期:2014-08-15 14:32:0015-16赛季CBA联赛之同曦
日期:2017-12-14 15:28:14黑曼巴
日期:2017-08-10 08:14:342017金鸡报晓
日期:2017-02-08 10:39:42黑曼巴
日期:2016-11-15 15:48:38CU十四周年纪念徽章
日期:2016-11-09 13:19:1015-16赛季CBA联赛之同曦
日期:2016-04-08 18:00:03平安夜徽章
日期:2015-12-26 00:06:30程序设计版块每日发帖之星
日期:2015-12-03 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-09 06:20:002015亚冠之吉达阿赫利
日期:2015-07-03 08:39:42
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-05-22 15:28 |只看该作者 |倒序浏览
RT

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
2 [报告]
发表于 2014-05-22 15:43 |只看该作者
13问里有,但我不记得了,我的理解是
${var}基本上等价于$var
第一个严谨一些,第二个:
比如$1...$9这个没区别,但是$10呢,${var}的这种就是${10}
而$var却是$10是$1后边带个0。

一般情况下,写{}的比较好,个人谬论姑且听之

论坛徽章:
39
辰龙
日期:2013-08-21 15:45:192015亚冠之广州富力
日期:2015-05-12 16:34:52亥猪
日期:2015-03-03 17:22:00申猴
日期:2015-03-03 17:21:37未羊
日期:2014-10-10 13:45:41戌狗
日期:2014-06-17 09:53:29巨蟹座
日期:2014-06-12 23:17:17双鱼座
日期:2014-06-10 12:42:44寅虎
日期:2014-06-09 12:52:172015亚冠之卡尔希纳萨夫
日期:2015-05-24 15:24:35黄金圣斗士
日期:2015-12-02 17:25:0815-16赛季CBA联赛之吉林
日期:2017-06-24 16:43:52
3 [报告]
发表于 2014-05-22 15:44 |只看该作者
为了标示边界,无区别。

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
4 [报告]
发表于 2014-05-22 15:58 |只看该作者
加上花括号可以防止歧义~{:2_172:}

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
5 [报告]
发表于 2014-05-22 16:09 |只看该作者
回复 1# amarant
$AB不能表示${A}B这个含义吧。


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
6 [报告]
发表于 2014-05-22 16:15 |只看该作者
回复 1# amarant

$ A=123
$ echo $A
123
$ echo $A456

$ echo ${A}456
123456

   

论坛徽章:
22
丑牛
日期:2014-08-15 14:32:0015-16赛季CBA联赛之同曦
日期:2017-12-14 15:28:14黑曼巴
日期:2017-08-10 08:14:342017金鸡报晓
日期:2017-02-08 10:39:42黑曼巴
日期:2016-11-15 15:48:38CU十四周年纪念徽章
日期:2016-11-09 13:19:1015-16赛季CBA联赛之同曦
日期:2016-04-08 18:00:03平安夜徽章
日期:2015-12-26 00:06:30程序设计版块每日发帖之星
日期:2015-12-03 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-09 06:20:002015亚冠之吉达阿赫利
日期:2015-07-03 08:39:42
7 [报告]
发表于 2014-05-22 16:16 |只看该作者

   
Parameter Expansion
       The `$' character introduces parameter expansion, command substitution,
       or arithmetic expansion.  The parameter name or symbol to  be  expanded
       may  be enclosed in braces, which are optional but serve to protect the
       variable to be expanded from characters immediately following it  which
       could be interpreted as part of the name.

       When  braces  are  used, the matching ending brace is the first `}' not
       escaped by a backslash or within a quoted string,  and  not  within  an
       embedded  arithmetic  expansion,  command  substitution,  or  parameter
       expansion.

       ${parameter}
              The value of parameter is substituted.  The braces are  required
              when  parameter  is  a  positional  parameter with more than one
              digit, or when parameter is followed by a character which is not
              to be interpreted as part of its name.

       If  the  first  character  of  parameter is an exclamation point (!), a
       level of variable indirection is introduced.  Bash uses  the  value  of
       the variable formed from the rest of parameter as the name of the vari‐
       able; this variable is then expanded and that value is used in the rest
       of  the  substitution, rather than the value of parameter itself.  This
       is known as indirect expansion.  The exceptions to this are the  expan‐
       sions  of ${!prefix*} and ${!name[@]} described below.  The exclamation
       point must immediately follow the left  brace  in  order  to  introduce
       indirection.

       In each of the cases below, word is subject to tilde expansion, parame‐
       ter expansion, command substitution, and arithmetic expansion.

       When not performing substring expansion,  using  the  forms  documented
       below,  bash tests for a parameter that is unset or null.  Omitting the
       colon results in a test only for a parameter that is unset.

       ${parameter:-word}
              Use Default Values.  If parameter is unset or null,  the  expan‐
              sion  of word is substituted.  Otherwise, the value of parameter
              is substituted.
       ${parameter:=word}
              Assign Default Values.  If  parameter  is  unset  or  null,  the
              expansion of word is assigned to parameter.  The value of param‐
              eter is then substituted.   Positional  parameters  and  special
              parameters may not be assigned to in this way.
       ${parameterword}
              Display  Error if Null or Unset.  If parameter is null or unset,
              the expansion of word (or a message to that effect  if  word  is
              not  present) is written to the standard error and the shell, if
              it is not interactive, exits.  Otherwise, the value of parameter
              is substituted.
       ${parameter:+word}
              Use  Alternate Value.  If parameter is null or unset, nothing is
              substituted, otherwise the expansion of word is substituted.
       ${parameterffset}
       ${parameterffset:length}
              Substring Expansion.  Expands to  up  to  length  characters  of
              parameter  starting  at  the  character specified by offset.  If
              length is omitted, expands to the substring of parameter  start‐
              ing at the character specified by offset.  length and offset are
              arithmetic expressions (see ARITHMETIC  EVALUATION  below).   If
              offset  evaluates  to a number less than zero, the value is used
              as an offset from the end of the value of parameter.  Arithmetic
              expressions  starting  with  a - must be separated by whitespace
              from the preceding : to be distinguished from  the  Use  Default
              Values  expansion.   If  length  evaluates to a number less than
              zero, and parameter is not @ and not an indexed  or  associative
              array,  it is interpreted as an offset from the end of the value
              of parameter rather than a number of characters, and the  expan‐
              sion is the characters between the two offsets.  If parameter is
              @, the result is length positional parameters beginning at  off‐
              set.   If parameter is an indexed array name subscripted by @ or
              *, the result is the length members of the array beginning  with
              ${parameter[offset]}.   A  negative  offset is taken relative to
              one greater than the maximum index of the specified array.  Sub‐
              string  expansion applied to an associative array produces unde‐
              fined results.  Note that a negative offset  must  be  separated
              from  the  colon  by  at least one space to avoid being confused
              with the :- expansion.  Substring indexing is zero-based  unless
              the  positional  parameters are used, in which case the indexing
              starts at 1 by default.  If offset  is  0,  and  the  positional
              parameters are used, $0 is prefixed to the list.

       ${!prefix*}
       ${!prefix@}
              Names  matching prefix.  Expands to the names of variables whose
              names begin with prefix, separated by the first character of the
              IFS  special variable.  When @ is used and the expansion appears
              within double quotes, each variable name expands to  a  separate
              word.

       ${!name[@]}
       ${!name
  • }
                  List  of  array  keys.  If name is an array variable, expands to
                  the list of array indices (keys) assigned in name.  If  name  is
                  not  an  array,  expands to 0 if name is set and null otherwise.
                  When @ is used and the expansion appears within  double  quotes,
                  each key expands to a separate word.

           ${#parameter}
                  Parameter  length.   The  length  in  characters of the value of
                  parameter is substituted.  If parameter is *  or  @,  the  value
                  substituted  is the number of positional parameters.  If parame‐
                  ter is an array name subscripted by * or @,  the  value  substi‐
                  tuted is the number of elements in the array.

           ${parameter#word}
           ${parameter##word}
                  Remove matching prefix pattern.  The word is expanded to produce
                  a pattern just as in pathname expansion.  If the pattern matches
                  the  beginning of the value of parameter, then the result of the
                  expansion is the expanded value of parameter with  the  shortest
                  matching  pattern  (the ``#'' case) or the longest matching pat‐
                  tern (the ``##'' case) deleted.  If parameter is  @  or  *,  the
                  pattern  removal operation is applied to each positional parame‐
                  ter in turn, and the expansion is the resultant list.  If param‐
                  eter  is  an array variable subscripted with @ or *, the pattern
                  removal operation is applied to each  member  of  the  array  in
                  turn, and the expansion is the resultant list.

           ${parameter%word}
           ${parameter%%word}
                  Remove matching suffix pattern.  The word is expanded to produce
                  a pattern just as in pathname expansion.  If the pattern matches
                  a  trailing portion of the expanded value of parameter, then the
                  result of the expansion is the expanded value of parameter  with
                  the  shortest  matching  pattern (the ``%'' case) or the longest
                  matching pattern (the ``%%'' case) deleted.  If parameter  is  @
                  or  *,  the  pattern  removal operation is applied to each posi‐
                  tional parameter in turn, and the  expansion  is  the  resultant
                  list.   If  parameter is an array variable subscripted with @ or
                  *, the pattern removal operation is applied to  each  member  of
                  the array in turn, and the expansion is the resultant list.

           ${parameter/pattern/string}
                  Pattern substitution.  The pattern is expanded to produce a pat‐
                  tern just as in pathname expansion.  Parameter is  expanded  and
                  the  longest match of pattern against its value is replaced with
                  string.  If pattern begins with /, all matches  of  pattern  are
                  replaced   with  string.   Normally  only  the  first  match  is
                  replaced.  If pattern begins with #, it must match at the begin‐
                  ning of the expanded value of parameter.  If pattern begins with
                  %, it must match at the end of the expanded value of  parameter.
                  If string is null, matches of pattern are deleted and the / fol‐
                  lowing pattern may be omitted.  If parameter is @ or *, the sub‐
                  stitution  operation  is applied to each positional parameter in
                  turn, and the expansion is the resultant list.  If parameter  is
                  an  array  variable  subscripted  with  @ or *, the substitution
                  operation is applied to each member of the array  in  turn,  and
                  the expansion is the resultant list.

           ${parameter^pattern}
           ${parameter^^pattern}
           ${parameter,pattern}
           ${parameter,,pattern}
                  Case  modification.   This expansion modifies the case of alpha‐
                  betic characters in parameter.  The pattern is expanded to  pro‐
                  duce  a  pattern  just as in pathname expansion.  The ^ operator
                  converts lowercase letters matching pattern to uppercase; the  ,
                  operator  converts matching uppercase letters to lowercase.  The
                  ^^ and ,, expansions  convert  each  matched  character  in  the
                  expanded  value;  the  ^ and , expansions match and convert only
                  the first character in the expanded value.  If pattern is  omit‐
                  ted,  it is treated like a ?, which matches every character.  If
                  parameter is @ or *, the case modification operation is  applied
                  to  each  positional parameter in turn, and the expansion is the
                  resultant list.  If parameter is an array  variable  subscripted
                  with  @ or *, the case modification operation is applied to each
                  member of the array in turn, and the expansion is the  resultant
                  list.
  • 论坛徽章:
    22
    丑牛
日期:2014-08-15 14:32:0015-16赛季CBA联赛之同曦
日期:2017-12-14 15:28:14黑曼巴
日期:2017-08-10 08:14:342017金鸡报晓
日期:2017-02-08 10:39:42黑曼巴
日期:2016-11-15 15:48:38CU十四周年纪念徽章
日期:2016-11-09 13:19:1015-16赛季CBA联赛之同曦
日期:2016-04-08 18:00:03平安夜徽章
日期:2015-12-26 00:06:30程序设计版块每日发帖之星
日期:2015-12-03 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-09 06:20:002015亚冠之吉达阿赫利
日期:2015-07-03 08:39:42
    8 [报告]
    发表于 2014-05-22 16:28 |只看该作者
    根据上面的bash手册,可知$VAR可以看作${VAR}的子集。属于一种特殊情况,花括号可以去掉。

    ps
    Herowinter和jason680同学说的这点,我也知道。只是今天写bash脚本的时候,对写成$VAR还是${VAR}产生了困惑。UNIX下东西大部分都是尽量不做冗余重复的事,这里为什么要用两个不同的表达式表示同一个意思呢?故有此问。

    论坛徽章:
    2
    狮子座
日期:2014-05-30 21:43:03巳蛇
日期:2014-05-30 21:46:14
    9 [报告]
    发表于 2014-05-22 17:36 |只看该作者
    为何楼主不写一个复杂的式子?

    论坛徽章:
    16
    IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
    10 [报告]
    发表于 2014-05-22 17:45 |只看该作者
    ${}还有个作用就是你贴的那些,字符串处理了。
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP