2

I got stuck at a problem where I need to expand a variable in another variable as follow :

var1=abc
var2=$var1/pqr
echo ${!var2}

Here I want output to be abc/pqr , but not getting it , please help. and var2 value is like this only , I can't put it in double quotes.

3 Answers 3

1
>> a="abcd"
>> b='$a/xyz'
>> eval c="$b"

>> echo $b
   $a/xyz
>> echo $c
   abcd/xyz
Sign up to request clarification or add additional context in comments.

2 Comments

eval is certainly not indicated here.
Even in such tiny examples it would be a good idea to teach people to use quotes correctly.
1

Just remove the !.

var1=abc
var2=$var1/pqr
echo ${var2}

Note the curly braces are not necessary above, but they do not hurt either.

Comments

1

You don't need de-referencing in this case:

$ var1=abc
$ var2=$var1/pqr
$ echo $var2
abc/pqr

You de-reference when you assign variable as the value without the $ sigil. For example:

$ var1=abc/pqr
$ var2=var1
$ echo ${!var2}
abc/pqr

2 Comments

here I am getting output as below : echo $var2 : o/p $var1/pqr
I am not using any kind of quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.