This question already has an answer here:
Bash indirect variable reference [duplicate]
(1 answer)
Closed 7 years ago.
How do I solve this in bash
I have a variable called `
DEV_IP=192.168.0.1
I have another variable called
ENV_NAME=DEV
How would I call ENV_NAME to get the IP Addess ?
like:
echo ${ENV_NAME}_IP
This is what I get when I run
echo ${"$ENV"_IP}
-bash: ${"$ENV"_IP}: bad substitution
You can use Bash's metavariable syntax (indirect expansion as they call it) if you add a step:
KEY="${ENV_NAME}_IP"
echo ${!KEY}
With variables as you have, outputs:
192.168.0.1
From the TLDP entry on Shell paramter and variable expansion:
If the first character of "PARAMETER" is an exclamation point, Bash
uses the value of the variable formed from the rest of "PARAMETER" as
the name of the variable; this variable is then expanded and that
value is used in the rest of the substitution, rather than the value
of "PARAMETER" itself. This is known as indirect expansion.
Related
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"
This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
there is a simple bash shell fragment
function gettop()
{
abc= /bin/pwd
}
funret=$(gettop)
echo $funret
the output of funret is the ouput of command /bin/pwd:
/home/xxx/xxx
how does this happen?
according to shell standard, function return either with a echo or return,
but here non of these involved; but the result show me that getopt
returned /bin/pwd and $() just run this command.
another thing is, after I delete the extra empty space after abc=
the output will become null;
what is going on here exactly?
The output i.e. /home/xxx/xxx is an expected behavior because in shell the variables are assigned without space in them i.e. abc=/bin/pwd. Now since you have defined the variable using space shell tried to execute the string after the space during the function and the output /home/xxx/xxx is because of that execution as /bin/pwd is a know command to shell.
If you use the below code snippet it would throw an error.
function gettop()
{
abc= testing
}
funret=$(gettop)
echo $funret
Error:
./tmp.sh: line 5: testing: command not found
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"'
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.
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.