Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash create variable then assign value to it

For this problem I have two values, curdir and curlevel, which change throughout my script. I want to know if it's possible to create a variable and then use that value as the name for another value. For example

temp="dir_${curdir}_${curlevel}"
$temp=$name_of_directory  **<----Is there a legitimate way to do this?**

so if initially curdir=1 and curlevel=0 then $(temp)=directory_one is equal to

dir_1_0=directory_one

then later if curdir=2 and curlevel=4, I can reset temp and then have

$(temp)=another_directory

is the same as

dir_2_4=another_directory

so I could make a call such as

cd $(temp)

which will move me to different directories when I need to

like image 757
mike Avatar asked Feb 26 '23 05:02

mike


1 Answers

I think what you want is to use eval. Like so:

 $ foo=bar
 $ bar=baz
 $ eval qux=\$$foo
 $ echo $qux
 baz

So what you could do is something like

eval temp=\$$"dir_${curdir}_${curlevel}"
cd $temp
like image 96
Brian Avatar answered Mar 04 '23 11:03

Brian