Snoop05
Snoop05

Reputation: 55

How to set a variable which contains other variables that will change in Bash?

var1=0
var=$var1
(Some conditions that may change $var1)
var1=1
echo $var

I need to have 1 as output of echo $var. In other words, I need to get a new value if the variable1 contained in variable if it change. Are there some special options or quotes that will allow me to do that?

EDIT:

FILE=ArchLinuxARM-$DATE-$TARGET.img
DATE=2015.10
TARGET=rpi-2

echo ${!FILE}

Will echo nothing. (It should echo ArchLinuxARM-2015.10-rpi-2.img)

Upvotes: 3

Views: 298

Answers (2)

glenn jackman
glenn jackman

Reputation: 246774

For your specific question, you'd be better served with a function:

filename() { echo "ArchLinuxARM-$DATE-$TARGET.img"; }

DATE=2015.10
TARGET=rpi-2

echo "$(filename)"

Upvotes: 3

glenn jackman
glenn jackman

Reputation: 246774

In bash version 4.3, you can create reference variables:

$ echo $BASH_VERSION
4.3.42(1)-release
$ declare -n var=var1
$ var1=0; echo $var
0
$ var1=1; echo $var
1

Upvotes: 0

Related Questions