Assign a variable from grep [duplicate] - shell

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

Related

Bash - Appending values to variable [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 11 months ago.
I am still snooping around bash as a newbie and I wanted to ask some question
Is it possible to append additional text to value in bash?
Example below:
#!/bin/bash
value1="qwerty"
value2="asdfgh"
echo $value1 >> $value3
echo $value2 >> $value3
echo $value3
So my question is - can I create value that contain other values?
Why am I trying to do this? Because I want to do some logical functions for each value and if it meets the criteria it gets appended to my final value.
By the end I would echo value3 and it would contain all results that met criteria.
>> is for writing to files, not appending to variables. You just want string interpolation.
value3="$value1$value2"
If you want an embedded newline, you can do that:
value3="$value1
$value2"
If you want to append a value to an existing variable,
value3=$value1
value3="$value3
$value2"

Combine two variables to form the identifier for another variable in Bash [duplicate]

This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 1 year ago.
I want to be able to take the values of two variables and concatenate them together to form the identifier for another variable in a bash script.
final_answer="we did it"
one="final"
two="answer"
t="${one}_${two}"
echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"
Not sure this is possible but it seems like bash would have this capacity somehow.
Thank you!
$ echo "${!t}"
we did it
See http://mywiki.wooledge.org/BashFAQ/006#Indirection for details.

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"

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