“invalid arithmetic operator” in shell

2019-03-21 10:01发布

cat test.sh

#!/bin/bash
key="index";
arr[$key]="val"
echo ${arr[${key}]}

/bin/bash-x test.sh

+ key=index
+ arr[$key]=val
+ echo val
val

then I modify the test.sh:

#!/bin/bash
key="index.index";
arr[$key]="val"
echo ${arr[${key}]}

/bin/bash -x test.sh

+ key=index.index
+ arr[$key]=val
test.sh: line 3: index.index: syntax error: invalid arithmetic operator (error token is ".index")
test.sh: line 4: index.index: syntax error: invalid arithmetic operator (error token is ".index")

why this error appears, any suggestion will be appreciate!

3条回答
疯言疯语
2楼-- · 2019-03-21 10:40

Declare the array variable as an associative array with declare -A arr.

$ cat test.sh 
#!/bin/bash
set -x 
declare -A arr
key="index.index";
arr["$key"]="val"
echo "${arr["${key}"]}"

$ ./test.sh 
+ declare -A arr
+ key=index.index
+ arr["$key"]=val
+ echo val
val
查看更多
老娘就宠你
3楼-- · 2019-03-21 10:46

This behavior varies by BASH version. Older BASH versions only allowed non negative integers as the keys in arrays.

Please note that dot/period is not allowed in a variable name in BASH.

See this Q&A for more details about allowed characters in BASH: Allowed characters in linux environment variable names

EDIT:

(Many thanks to @chepner for this addendum)

Regular arrays (not associative array) are indexed by integer only. Any expression used as an index between square brackets is treated as an arithmetic expression. $key expands to index, which is then treated as an (unset) variable which expands to 0. If you assigned a value of, say, 3 to index, then ${array[$key]} -> ${array[index]} -> ${array[3]}. It's a type of implicit indirect parameter expansion

查看更多
Deceive 欺骗
4楼-- · 2019-03-21 10:57

This:

key="index";
arr[$key]="val"
echo ${arr[${key}]}

only appears to work. Since arr is an ordinary array, not an associative array, it can only be indexed by non-negative integer values.

Consider this valid code:

index=42
key="index"
arr[$key]="val"
echo ${arr[42]}
echo ${arr[index]}
echo ${arr[$index]}
echo ${arr['index']}
echo ${arr["index"]}
echo ${arr[\index]}

All the echo statements print val. Since the index is treated as an arithmetic expression, it can refer to a variable (in this case, $index) either with or without the $ prefix -- even if it's a quoted string.

In your code, where you never assigned a value to $index, ${arr[${key}]} expands to ${arr[index]}, which is equivalent to ${arr[$index]}, which is treated (by default) as equivalent to ${arr[0]}.

(If you have set -o nounset, then references to unset variables are treated as errors, and your code will produce an error message.)

Your second chunk of code:

key="index.index";
arr[$key]="val"
echo ${arr[${key}]}

is invalid because index.index is not a valid variable name -- even though you probably meant it to be just a string used as an array index.

If you want arr to permit arbitrary strings as indices, it needs to be an associative array. You can create a non-associative array simply by assigning to it (or by using declare -a), but an associative array can only be created with declare -A.

Associative arrays were added to bash in version 4. If you're using an earlier version of bash, declare -A is not supported. You'll need to upgrade to a newer bash, code up some clumsy alternative, or use a language that does support associative arrays, like Awk, Python, or Perl.

Adding declare -A arr (as user000001's answer suggests) should solve the problem (if you have bash 4), but it's instructive to understand what your original code is actually doing (or rather not doing).

(BTW, thanks for asking this; I learned a lot as I was composing this answer.)

查看更多
登录 后发表回答