How to change the value of a second-level variable in Bash?

Consider 2 variables in bash as follows:

X = 8 Y = X 

If I want to print the value of X using the variable Y , I could do echo ${!Y} , and the value 8 will be printed

Now the question is, how to change the value of X using the variable Y ?

+4
source share
2 answers

Using eval :

 $ X=8 $ Y=X $ echo ${!Y} 8 $ eval $Y=3 $ echo $X 3 
+7
source

This might work for you:

 X=8; Y=X; echo ${!Y} 8 echo $(($Y=3)) 3 echo $X 3 (($Y=7)); echo $X 7 

Here are some more ways:

 let $Y=4; echo $X 4 _[$Y=6]=1; echo $X 6 
+2
source

Source: https://habr.com/ru/post/1389350/


All Articles