When trying the execute the following in bash
foo=foo
my_array[$foo]=bar
I get the error ‘bash: foo: expression recursion level exceeded (error token is “foo”)’. But this works fine:
foo=hello
my_array[$foo]=bar
Why is this happening?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is that you are not declaring your array to be associative, so it’s assumed to be a numeric array. When bash tries to evaluate
what he comes into is
but the array index ain’t still numeric, so he tries to evaluate it again, leading into
as you don’t need to use the $ when in between square brackets. You can see that this goes on and on until a recursion level exceeded exception is thrown.
To solve it, just declare the array as associative: