Shell script variable reflection [duplicate] - bash

This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 8 years ago.
I am writing a shell script (#!/bin/sh) which has a variable VAR which contains the name of another variable FOO, which in turn is set to BAR.
FOO=BAR
VAR=FOO
I want to get the value of the variable named in VAR, so something like:
echo "${$VAR}"
But that does not seem to work. Suggestions?

In Bash:
echo "${!VAR}"
Without Bash (though it works in Bash too):
eval echo "\${$VAR}"
Beware: eval is a very general mechanism that can run you into problems very easily. It works fine here, but be cautious about using it more generally.

Related

how to call a string as a variable in shell script? [duplicate]

This question already has answers here:
Can ${var} parameter expansion expressions be nested in bash?
(15 answers)
Closed 1 year ago.
I have a problem that I will not know the name of the variable and the name of the variable will be stored in an array that I have,
the problem here how can I call it, I tried some in the cli to see:
$ hello=sup
$ hi=hello
$ echo $`echo $hi`
$hello
as you see it prints "$hello" instead of "sup" that I want
To dereference a variable you have to use the $ so the second assignment isn't working in your code sample.
>hello=sup
>hi=$hello
>echo $hi
sup
See https://tldp.org/LDP/abs/html/ivr.html for more info on indirect variable references.
Just use the following in bash:
echo ${!hi}
or eval in other shells
eval "echo \$$hi"

bash bad substitution - access an environment variable from another [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Reference an appended variable?
(3 answers)
Closed 5 years ago.
How can I access an environment variable from another? I have the following in my shell
#!/usr/bin/env bash
set -x
export A_version=1.0.0
component=A
echo ${${component}_version}}
the bash script after the run gives me
temp.sh: line 9: ${${component}_version}}: bad substitution
You can use eval to do this. Here is a working version of your script that prints 1.0.0:
export A_version=1.0.0
component=A
eval "echo \$${component}_version"
For more information, see this page:
http://tldp.org/LDP/abs/html/ivr.html
Update: A safer way to do the same thing in Bash would be:
export A_version=1.0.0
component=A
var=${component}_version; echo "${!var}"
Note that you have to run this script with bash, not sh.

How to dynamically compute value of a variable and use it in a command? [duplicate]

This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 6 years ago.
Reducing what I'm trying to do to a simple example:
env temp=`pwd` echo "$temp"
I get this message:
temp: Undefined variable.
How do I make it work (in a shell-agnostic way)? I'm expecting the result of pwd to be printed.
My actual requirement uses a complicated expression in place of pwd, a script in place of "echo", and the variable $temp as an argument to that script.
Also, I want to set this variable only for this single command, and not for the whole shell (or any subsequent subshells).
How about something like this for sh and bash
(export temp=`pwd`; echo $temp)
And this for csh
csh -c 'setenv temp `pwd`; echo "$temp"'

Shell variable inside variable [duplicate]

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
Closed 7 years ago.
What is wrong in this substitution.
$ m_d_ver=0.2
$ m=mod
$ d=dom
$ echo ${$m_$d_ver}
-bash: ${$m_$d_ver}: bad substitution
Thanks,
What you're trying to do is an indirect variable lookup. The syntax for that is ${!namevar}, where namevar is a variable that contains the name you actually want to evaluate. Thus:
mod_dom_ver=0.2
m=mod
d=dom
var=${m}_${d}_ver
echo "${!var}"
See BashFAQ #006.

Bash - force error if variable not defined [duplicate]

This question already has answers here:
How can I make bash treat undefined variables as errors?
(2 answers)
Closed 6 years ago.
I work with a lot of shell scripts that use bash variables. So, for example, I might have a script like this:
option1="-blah_blah"
option2="-yada_yada"
option3="-whatever"
...
option99="-something_else"
./myCommand "$option1 $option12 $option97 $option45"
I am constantly editing that last command to run various engineering tests. The problem is, sometimes I misspell a variable. In that case, Bash simply substitutes an empty string, and my command does the wrong thing silently.
Is there a way to have Bash throw an exception when I try to use a variable that is not defined?
Use:
set -e # Stop on error. I can't believe that this is not default.
set -u # Stop if trying to use un-initialized variables.

Resources