Bash: arithmetic expansion in array indices - is the dollar sign needed?

后端 未结 2 451
猫巷女王i
猫巷女王i 2020-12-21 04:36

When I use arithmetic expansion in an array index in bash, like this:

declare -a FILES
declare -i INDEX=0

for FILE in ./*
do
    FILES[((INDEX++))]=\"$FILE\         


        
2条回答
  •  醉话见心
    2020-12-21 05:16

    The dollar sign is required in some contexts but not the others:

    $ bash --version
    GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu)
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later 
    
    $ echo ((1+2))
    bash: syntax error near unexpected token `('
    
    $ echo $((1+2))
    3
    
    $ for ((x=0; x<3;++x)); do echo $x; done
    0
    1
    2
    
    $ for $((x=0; x<3;++x)); do echo $x; done
    bash: `$((x=0; x<3;++x))': not a valid identifier
    

    After reading bash man page, the dollar sign is not required in compound commands:

    Compound commands are the shell programming constructs. Each construct begins with a reserved word or control operator and is terminated by a corresponding reserved word or operator. Any redirections (see Redirections) associated with a compound command apply to all commands within that compound command unless explicitly overridden.

    In most cases a list of commands in a compound command’s description may be separated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon.

    Bash provides looping constructs, conditional commands, and mechanisms to group commands and execute them as a unit.

    for (( expr1 ; expr2 ; expr3 )) is a compound command and hence the dollar sign is not required to enable arithmetic evaluation.

    Whereas echo $((expr)) is not a compound command because it does not start with a reserved bash keyword, so it requires a dollar sign to enable arithmetic evaluation.

提交回复
热议问题