4

Any ideas why this is happening? Why do I have to manually explicitly reassign the variable but can't do it if I have another variable in the name of the variable?

SCRIPT:

#!/bin/bash

a_1=1
a_2=1

for temp in 1 2
do
    a_$temp="2"
    echo $((a_$temp))
done

a_1=2
a_2=2
echo $a_1
echo $a_2

OUTPUT:

[dgupta@della4 Rates_Of_Quenching]$ ./test.sh
./test.sh: line 8: a_1=2: command not found
1
./test.sh: line 8: a_2=2: command not found
1
2
2
0

2 Answers 2

5

Instead of:

a_$temp="2"

Use:

declare a_$temp="2"

to create variable with dynamic name.

Sign up to request clarification or add additional context in comments.

Comments

2

As far as bash is concerned, you are trying to execute the command 'a_1=2', rather than perform an assignment. You can get around this by using declare, or its synonym typeset:

'a_1=2'                       # bash: a_1=2: command not found
typeset 'a_1=2' 
echo $a_1                     # 2
declare 'a_1=3'
echo $a_1                     # 3

While it is possible to use declare, you might want to take advantage of bash arrays (which have been around since bash version 2) rather than using variables with numerical suffixes:

a=(1 1)
echo ${a[0]}                  # 1
echo ${a[1]}                  # 1
for i in 0 1; do a[i]=2; done
echo ${a[0]}                  # 2
echo ${a[1]}                  # 2

6 Comments

+1, but (a) bash considers typeset obsolete (it still has its place if maximum portability is required) and (b) your inclusion of a prompt string (bash-4.3$) both creates visual noise and makes it harder to copy-paste your code for verification (it's commendable to state the bash version your code was run on, but that could be done separately, although, depending on the feature discussed, stating the version may not be necessary at all, if a feature was introduced a loooong time ago).
You're welcome; I was actually thinking of not using a prompt string at all (even just $), as it makes copy-pasting any sequence of commands cumbersome. I know that $ is seductive, because it is widely used to symbolize a prompt, whereas I know of no equally well-known symbol to indicate output (is there one?). However, I think on SO reversing the logic is beneficial: no prefix for the commands, and some indicator for output. One - not very elegant - way around this is to place output in a separate code snippet.
@mklement0 I agree with your thoughts. For a longer script, I would normally show the output separately. I've edited to show the output of the commands in comments, which at least means that the commands can be directly copy-pasted.
Good idea; placing the output in comments is a simpler alternative.
@mklement0 of course, my answer would have been three times as well appreciated if I'd instead displayed no output, provided no explanation but crucially got there first. Not that I'm bitter ;) Thanks again for your feedback.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.