Use a variable's value to name another variable in Bash [duplicate] - bash

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 12 months ago.
So, I'd like to do something like the following:
while [ $n -le 7 ]
do
char$1=somevalue
echo $char$1
n++
done
But even reading through the links below and trying declare or something like $char${!n}, I haven't been able to make it work... Halp! :)
I've already looked at:
How to use a variable's value as another variable's name in bash
and
Dynamic variable names in Bash
Thanks!

Related

Use command line argument in bash script substituted with value from script [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 2 years ago.
I recently started bash scripting and got stuck with a very basic usecase, searched stackoverflow/google but couldn't find a way to achieve what I am trying to do.
I have a script color.sh
#!/bin/bash
Apple="Red"
Orange="Orange"
Banana="Yello"
echo $$1
What I am trying to achieve is print the color of fruit and accept fruit from command line. The output I want is
./color.sh Apple -> Red, but what I get is some random number which I think is process Id.

bash script, want to use '$' without it looking for a variable [duplicate]

This question already has answers here:
Interpolating variables which contain '$' in a bash script
(1 answer)
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
I need to write a cron job, to send an mqtt message to "homie/$HOSTNAME/$state".
In the case of $HOSTNAME - it needs to use the env variable, but $state - it must use as is.
How would I add "homie/$HOSTNAME/$state" without bash thinking the $state is a variable as well?

Bash expand variable in an another variable [duplicate]

This question already has answers here:
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
What does "${!var}" mean in shell script? [duplicate]
(1 answer)
Closed 4 years ago.
I have the following variables
my_country_code="green"
x="country"
echo ${my_$x_code}
bash: ${my_$x_code}: bad substitution
echo should print green as output, but unable to find any technique which will give the correct output
my_x_code="my_${x}_code"
echo ${!my_x_code}

Shell variable inside variable [duplicate]

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.

How can i get the value from a file using bash? [duplicate]

This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.

Resources