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"
Related
This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 1 year ago.
So I'm trying to compose a string using script shell
#!/bin/sh
blNumber=1111619832
echo '*'$blNumber+='*.xlsx'
I expect the output to be: *1111619832*.xlsx
but as a result I get: *+=*.xlsx
Btw I tried to figure out why so I tried this code
#!/bin/sh
blNumber=1111619832
output="*${blNumber}"
and whenever I add something after *${blNumber} it get concatenated at the begging of the string
Why are you using += in the first place?
$ echo '*'$blNumber'*.xlsx'
*1111619832*.xlsx
Or put it inside double-quotes. It's best practice to quote all variables anyway.
$ echo "*$blNumber*.xlsx"
*1111619832*.xlsx
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 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.
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.