Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash re-assignment of value to variable "command not found" [duplicate]

Tags:

bash

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
like image 866
user3708602 Avatar asked Mar 19 '23 10:03

user3708602


1 Answers

Instead of:

a_$temp="2"

Use:

declare a_$temp="2"

to create variable with dynamic name.

like image 130
anubhava Avatar answered Apr 25 '23 08:04

anubhava