Unix variable usage [duplicate] - bash

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
Closed 7 years ago.
If I have 2 variables with values assigned as below -
a=1; s1=555
Can we print 555 using:
$ echo $s`echo $a`
...? My requirement is use variable 'a' in second variable to print final value of s1.
I have tried it already and failed. Is there any way?
Thanks.

Bash has native syntax for indirect evaluation support:
a=1
s1=555
varname="s$a"
echo "${!varname}"

Related

Bash - Add variable in associative array - bad substitution [duplicate]

This question already has answers here:
Bash indirect variable referencing
(1 answer)
Bash indirect array addressing?
(3 answers)
Closed 2 years ago.
Here is an expected behavior for associated array in bash
$ declare -A PC=( [Monitor]=Dell [CPU]=HP )
$ echo ${PC[CPU]}
HP
This gives me HP as output
Lets say I have these PC,Monitor amd CPU values stored in variable a , b and c. I am trying fetch the details now but I am getting "bad substitution" error when trying so.
$ a=PC; b=Dell; c=HP
$ echo ${$a[$b]}
bash: ${$a[$b]}: bad substitution
$ echo ${PC[$b]}
Dell
${PC[$b]} however is returning expected output but not {$a[$b]}
Not sure how this can be achieved. Thanks in advance.
What you are trying to do is called indirection - using one variable as the name of another variable.
In bash you do this for normal variables using the syntax ${!var}, as in
a=5
b=a
echo ${!b} # 5
Unfortunately this won't work how you want for an array variable because the syntax ${!array[*]} means something else (getting all keys from an associative array).
Instead, as suggested by a comment below, you can create a string for the entire reference and then use redirection on that:
lookup="$a[$b]"
echo ${!lookup} # will give Dell in your example

how to recall variables from array in bash [duplicate]

This question already has answers here:
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Closed 3 years ago.
I have the following system variables:
$var01="this is var01"
$var02="this is var02"
$var03="this is var03"
$Var04="this is var04"
I want to use a for loop to recall each of them:
for i in {0..4}
do
echo "content of var$i is: $[var$i]"
done
but the above for loop gives me an error saying "bad substitution".
How should I construct echo parameter so to get the following result:
content of var01 is: this is var01
content of var02 is: this is var02
content of var03 is: this is var03
content of var04 is: this is var04
I suggest with bash >= 4.3:
for i in {0..4}; do x="var$i"; echo "${!x}"; done
See: Nameref

Set value of string in variable [duplicate]

This question already has answers here:
Indirect variable assignment in bash
(7 answers)
Dynamic variable names in Bash
(19 answers)
Closed 3 years ago.
I want to set FOO="myvar" and set myvar to another variable. Not sure if that makes sense. Example below.
FOO="myvar"
BAR="true true false"
eval $FOO=$BAR <==== Problem line
echo $myvar
I want the last line to print "true true false"

Assign a variable from grep [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I'm trying to assign a variable with a value I get from grep, here's my code
a="i
am
a
string
"
b="$a"|grep am
echo "$b"
I expect the result is am, but the result b is empty. But when I code echo "$a"|grep am directly, I get the right result. why and how can I assign the result to b?
Do it like this:-
**b=$(echo "$a"|grep am)
**

Why Pycharm gives warning on "simple variable usage" in .sh bash script? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
In Pycharm when we use variable e.g. $privateKey, we get the warning Simple variable usage as below snapshot and recommend us to turn to the syntax ${privateKey}
My question is why we get such warning? What is the risk to use simple variable like that?
When clicking more
Thanks to #Whymarrh. One answer is as below.
since "$foobar" would instead expand foobar
My answer is to separate/distinguish $myVar and notInVar in string "$myVarnotInVar"
In other words
myVar=122
echo "$myVarnotInVar" # will print empty string "" since undefined variable $myVarnotInVar
echo "${myVar}notInVar" # will print 122notInVar

Resources