Set value of string in variable [duplicate] - bash

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"

Related

Double substituting Unix variable [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
variable variables in sh or dash
(1 answer)
Closed 4 months ago.
I'm trying to get a value of the variable by double substituting inside the string as follows
i=1
df1 ='date_col'
X='value of the string is $df$i'
But getting invalid substitution error
I need the output as follows
echo $X
value of the string is date_col

How to append a sting and a variable in BASH [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How can I generate new variable names on the fly in a shell script?
(6 answers)
Closed 2 years ago.
I have a variable that points at a file called point.c
I would like to temporarily append "A" to "_SRCS" so I can rename the variable and use it in a function as "SRCS".
A_SRCS=point.c
pro="A B"
for x in $pro
do
SRCS=${${x}"_SRCS"}
echo $SRCS #(will point to a function later on..)
done

bash one variable for another [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How can I generate new variable names on the fly in a shell script?
(6 answers)
Closed 2 years ago.
I would like to do in bash something I was doing with perl years ago
$4 (value from a script) can have the value "S" or "U"
I have:
DirectoryU="$Directory/*.$2"
DirectoryS="$Directory/area $3/*.$2"
and I want to define "DirectoryC" to something like:
DirectoryC=Directory$4
but there I dont want DirectoryC=DirectoryU (or DirectoryS) I want DirectoryC to have the value of DirectoryU (or DirectoryS) so after I can base my script on $DirectoryC ...
Does it make sense?

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)
**

Unix variable usage [duplicate]

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}"

Resources