Linked Questions
14 questions linked to/from Use a variable reference "inside" another variable
2
votes
2
answers
10k
views
Bash: Echo a variable whose name is the value of another variable [duplicate]
Suppose I have the following:
foo1=abc
i=1
a="FOO${i}"
echo ${${a}}
echo ${`echo $a`} # I also tried that
I am getting the error bash: ${${a}}: bad substitution.
0
votes
2
answers
2k
views
Bash - How to access a variable with a number suffix using a FOR loop [duplicate]
I declared var0, var1, var2, var3 with a for loop. How do I echo the vars inside that for loop? Here's the code.
#!/bin/bash
for i in {0..3}
do
export var$i=$i
done;
There i defined var0, var1, ...
3
votes
2
answers
174
views
Variable from concatentation of variables [duplicate]
I think this is a rather simple question but, I cant make this work:
I have a whole lists of variables eg:
SP60=OLA SP61=BYE SP62=TRT
I want to create a loop to call them in the specific number of ...
0
votes
0
answers
362
views
print variables based on iterator in for loop - bash [duplicate]
I am trying to print the values of a variable which has naming sequentially in bash.
boxes1=abcd
boxes2=efgh
boxes3=ijkl
for (( i=1; i<=$boxCount; i++ ));
do
echo "\$$boxes{i}"
...
1
vote
4
answers
168
views
Synthetic Variables in Bash (was: How to address a variable ceated with indirection) [duplicate]
Sorry, that was all too confusing, (my bad)
Let me try to explain again, but with a more simple example:
#define a new var and export it to the "env":
export VAR_ONE=thisIsVarOne
# check if ...
0
votes
0
answers
162
views
How can I use an already defined variable reference in definition of another variable in shell script/ [duplicate]
I am trying to do something like below :
export a="ABC" # Works fine
export b_"$a"="DEF" # Works fine
Now how do I print or echo the new variable "b_$a" ??
I ...
0
votes
1
answer
90
views
Bash nested variable [duplicate]
I source these variables from a txt file in CentOS bash shell:
NAME01=dns1
HOST01=1.1.1.1
NAME02=dns2
HOST02=1.1.2.2
NAME03=dns3
HOST03=1.1.2.3
## many more lines of similar
NUM=02
How can I use $...
1
vote
3
answers
82
views
Bash: Access variable with another variable [duplicate]
How can I access a variable with another variable, like this?
varAble="Hello"
varA="\n"
varBable="World"
varB="END"
for part in A, B
do
echo var${part}ble
...
8
votes
3
answers
19k
views
What is the most correct way to pass an array to a function?
Consider I have a very large array $large_list, is there a way to write a function that will take the array as an argument? For example:
echo_idx_array () {
arr="$1"
idx="$2"
echo "${arr[...
4
votes
3
answers
14k
views
Calling a function which calls another function with a for loop
I am calling a function with a for loop and saving the returned value to a variable. When I run the code, I get a command not found error. What is wrong?
#!/bin/bash
check_1()
{
x_$1=$(check_2 $...
1
vote
3
answers
6k
views
How do i use variable's value as a dynamic variable name?
i've been looking around for this all over, but either i don't know how to ask the question correctly, or it's not a common enough issue...
In PHP this is (almost) trivial:
function getSite(string $...
0
votes
1
answer
790
views
Parsing a file to determine what values to use [duplicate]
I have a script and a "config" file that pull variables from. I created the configuration file called config.cfg and used it in my script as such:
#! /bin/bash
if [ -f ./config.cfg ]
then
...
0
votes
0
answers
81
views
What does `echo ${!a}` do and why? [duplicate]
The following bash script prints out c, but I'm not sure why.
a=b
b=c
echo ${!a}
I believe that !a will be converted to the last command which began with a, so in this case that would be a=b. This ...
0
votes
0
answers
74
views
pass function parameter as name instead of as a string
With this function:
repr() {
declare -p $1 | cut -d '=' -f 2- > /tmp/.repr
$1=$(</tmp/.repr)
rm /tmp/.repr
}
It gives an error message, when I write:
repr test
This see the ...