Chinaunix

标题: bash中${VAR}和$VAR最本质的区别是什么? [打印本页]

作者: amarant    时间: 2014-05-22 15:28
标题: bash中${VAR}和$VAR最本质的区别是什么?
RT
作者: expert1    时间: 2014-05-22 15:43
13问里有,但我不记得了,我的理解是
${var}基本上等价于$var
第一个严谨一些,第二个:
比如$1...$9这个没区别,但是$10呢,${var}的这种就是${10}
而$var却是$10是$1后边带个0。

一般情况下,写{}的比较好,个人谬论姑且听之
作者: 关阴月飞    时间: 2014-05-22 15:44
为了标示边界,无区别。
作者: yestreenstars    时间: 2014-05-22 15:58
加上花括号可以防止歧义~{:2_172:}
作者: Herowinter    时间: 2014-05-22 16:09
回复 1# amarant
$AB不能表示${A}B这个含义吧。


   
作者: jason680    时间: 2014-05-22 16:15
回复 1# amarant

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

$ echo ${A}456
123456

   
作者: amarant    时间: 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.

  • 作者: amarant    时间: 2014-05-22 16:28
    根据上面的bash手册,可知$VAR可以看作${VAR}的子集。属于一种特殊情况,花括号可以去掉。

    ps
    Herowinter和jason680同学说的这点,我也知道。只是今天写bash脚本的时候,对写成$VAR还是${VAR}产生了困惑。UNIX下东西大部分都是尽量不做冗余重复的事,这里为什么要用两个不同的表达式表示同一个意思呢?故有此问。
    作者: tgwz88    时间: 2014-05-22 17:36
    为何楼主不写一个复杂的式子?
    作者: expert1    时间: 2014-05-22 17:45
    ${}还有个作用就是你贴的那些,字符串处理了。
    作者: Shell_HAT    时间: 2014-05-22 19:37
    只是今天写bash脚本的时候,对写成$VAR还是${VAR}产生了困惑。

    判断两个字符串是否相当,用=还是==,楼主困惑吗?
    作者: 用户名注册后不能更改    时间: 2014-05-23 15:39
    amarant 发表于 2014-05-22 16:28
    根据上面的bash手册,可知$VAR可以看作${VAR}的子集。属于一种特殊情况,花括号可以去掉。

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


    不如问``与$()的区别,这两个不同的表达式通常也是表达同一个意思。
    作者: zongg    时间: 2014-05-23 16:08
    引用变量有两种方式 $varname和${varname} 为防止变量在字符串中产生歧义建议使用${}   我是来打酱油的。
    作者: amarant    时间: 2014-05-23 16:11
    回复 11# Shell_HAT


        是啊。 shell真是一个特别糟糕的语言。太多这种情况了!!
    作者: Shell_HAT    时间: 2014-05-23 16:38
    回复 14# amarant


        你比较喜欢哪个语言呀?
    作者: jason680    时间: 2014-05-23 16:44
    回复 14# amarant


    you study shell script 最本质是什么?
       
    作者: 用户名注册后不能更改    时间: 2014-05-23 16:47
    amarant 发表于 2014-05-23 16:11
    回复 11# Shell_HAT


        是啊。 shell真是一个特别糟糕的语言。太多这种情况了!!


    还好你没有写循环,不然一定会迷茫究竟该用for还是while还是until。
    作者: amarant    时间: 2014-05-23 16:54
    回复 15# Shell_HAT


        养活我的语言是c和汇编,偶尔用shell做一些简单的任务。最喜欢的是python
    作者: amarant    时间: 2014-05-23 17:01
    回复 16# jason680


        shell就这系统的根基一样。必须用。虽然他很强大,通过简单的组合可以实现一些复杂的操作。但是学习的曲线的是非常长的。就像Unix 憎恨者手册里说的一样
    用久了就是觉得有种不协调的感觉,不知道你有没有这种感觉
    作者: jason680    时间: 2014-05-23 17:10
    本帖最后由 jason680 于 2014-05-23 17:12 编辑

    回复 19# amarant

    just get useful things only

    Note: usually I using awk and Perl ...
    作者: amarant    时间: 2014-05-23 17:18
    回复 20# jason680


         语言上的这些问题引来一个问题,就是很久没用的时候,对这些奇奇怪怪的符号就不认识了。
    从前有一段时间很认真的学习了shell awk sed这些工具,现在工作忙了,shell用的少,忘记了一大半,awk就全部忘记光了

    作者: jason680    时间: 2014-05-23 17:32
    回复 21# amarant

    python can work fine ...
       
    作者: 用户名注册后不能更改    时间: 2014-05-23 20:22
    回复 18# amarant

    python,嘛。

    1. >>> 1 if True else 0  
    2. 1  
    3. >>> True and 1 or 0  
    4. 1  
    复制代码
    “两个不同的表达式表示同一个意思”什么的,python“真是一个特别糟糕的语言”什么的。




    欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2