1

I want to be able to take the values of two variables and concatenate them together to form the identifier for another variable in a bash script.

  final_answer="we did it"
  one="final"
  two="answer"
  t="${one}_${two}"
  echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"

Not sure this is possible but it seems like bash would have this capacity somehow.

Thank you!

3
  • Why is this tagged bash and zsh? Commented Jul 5, 2021 at 7:26
  • Either answer would've been good with me i guess Commented Jul 6, 2021 at 23:39
  • @JLaF : Since the answers for bash and zsh will likely look pretty different, I suggest that you ask then two separate questions, one for each language. Commented Jul 7, 2021 at 5:16

1 Answer 1

2
$ echo "${!t}"
we did it

See http://mywiki.wooledge.org/BashFAQ/006#Indirection for details.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.