2

I'm trying to do the following:

set a = kitten
set temp_kitten = purr
echo ${temp_$a}

I want the echo command to return "purr".

The overall idea is that I have a bunch of variables in an array and a bunch of temp_variables in another array and I want to loop through them in a single foreach loop for comparison.

3

You can use eval:

eval echo \$temp_$a

References

  • Thanks! I knew it would be something simple like this... The world needs to ban non-bash shells, while they are at it, replacing all word processors and text editors with vim would be nice :D Is there any way to silence the eval return so I only get the output of the echo? – OhmArchitect Aug 23 '13 at 17:08
  • @DannyKmack This is not bash specific. According to the link I referenced, this should be portable. Also, if this answer solves your problem, please consider accepting it. – Joseph R. Aug 23 '13 at 17:09
  • Is there any way to silence the eval return so I only get the output of the echo? – OhmArchitect Aug 23 '13 at 17:15
  • @DannyKmack Not sure what you mean by the eval return. Using tcsh on my box and typing eval echo \$temp_$a returns purr and nothing else. – Joseph R. Aug 23 '13 at 17:18
  • Odd, I tried it in a new shell and it didn't happen to me. Thanks again. – OhmArchitect Aug 23 '13 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.