Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a variable name is there a function that will print its value?

Often times I pass the name of a variable to a bash function and instead of doing the expansion while passing it I would like to expand it inside the function. Of course when I do this I end up having to create the string with the variable name then running exec. It would be much more succinct if I could just run a command that echoed the output!

share|improve this question
Not sure what you mean. Can you provide an example? – forcefsck Oct 28 '11 at 15:06
up vote 6 down vote accepted
show_val() { echo "${!1}"; }
echo $(show_val foo)   # => bar
# works with arrays too
a=(zero one two)
echo $(show_val a[1])  # => one

Documented in the bash manual here

share|improve this answer
As always Ty for the guidance. – Prospero Oct 28 '11 at 18:48

Not sure what you mean in your question about "running exec". Perhaps you meant eval.

show_val() { eval echo \$$1; }
show_val foo
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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