What does ${!i} do in bash? In that context, what is the effect of ((i+=1))?

前端 未结 2 535
孤街浪徒
孤街浪徒 2021-01-17 03:24

I do not find anything about the meaning of this:

case ${!i} in
     --fa)
       ((i+=1))
       fa=${!i}
       ;;

What is the meaning of

相关标签:
2条回答
  • 2021-01-17 03:54

    Indirect expansion and name lookup

    Starting an expansion with an exclamation point is indirect expansion described below.

    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 variable; 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 expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.

    Indirect expansion

    So if you have a declaration like i=foo and another like foo=123, you can reference the expansion of "foo" this way:

    echo ${!i}
    

    Name enumeration

    But if you just want to know what defined names start with "i":

    $ iPad=123
    $ i=foo
    $ echo "${!i*}"
    i iPad
    

    Index enumeration

    And if you want to know what indices (or, in bash 4 associative arrays, keys) an array has:

    $ i=(1 2)
    $ i[9]=45
    $ echo "${!i[@]}"
    0 1 9
    

    Note that indexed arrays in Bash are sparse!

    Arithmetic Evaluation

    The other part of your question is really a completely different question about the context of Arithmetic Evaluation. There are a handful of contexts where names are treated as integers.

    1. If they occur as indices to a (non-associative) array. ("${foo[i++]}")
    2. If they occur in explicit Arithmetic Evaluation context. ((( i++ )))
    3. Following the let keyword. ( let j++ )
    4. If they have the integer attribute. (declare -i)

    The sigil ($) is optional in arithmetic context, as long as it's not ambiguous. (It would be ambiguous to use $1 without the dollar sign.) In fact, you probably want to avoid it, given that (( $i++ )) is a syntax error.

    Implied Indirect Expansion

    One really interesting side effect to declaring a name to have the integer attribute is that it implies a form of indirect expansion, distinct from the ${!name} expression:

    $ aname=123
    $ anothername=aname
    $ echo $anothername
    aname
    $ declare -i anothername
    $ anothername=aname
    $ echo $anothername
    123
    

    What's really going on here is that when anothername is declared to be an integer, assignment expressions have arithmetic context on the right hand side. Even though aname wasn't declared explicitly to be an integer, it is treated as one here.

    $ anothername=aname++
    $ echo $anothername $aname
    123 124
    

    For more information see ARITHMETIC EVALUATION in the bash manual.

    Your code:

    case ${!i} in
         --fa)
           ((i+=1))
           fa=${!i}
           ;;
    

    Let's say you're looping over options, and $3 expands to --fa. But also, i=3. This will cause "fa" to be set to the next option on the commandline ($4), because i=4 by the time fa is assigned.

    0 讨论(0)
  • 2021-01-17 04:09

    ${!i} refers to the variable whose name is the value of $i. It is called variable indirection. See an example to make it more clear:

    $ i="hello"      # variable $i contains 'hello'
    $ hello="bye"    # variable $hello contains 'bye'
    $ echo "${!i}"   # when doing variable expansion of $i, it fetches $hello
    bye
    

    Regarding ((i+=1)), it is a way to increment the variable i. See:

    $ i=3
    $ ((i+=1))
    $ echo $i
    4
    

    You can also use either of these:

    i=$((i+1))
    
    ((i++))
    
    let "i=i+1"
    

    For further reference, see Bash Reference Manual - Shell Parameter Expansion:

    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 variable; 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

    0 讨论(0)
提交回复
热议问题