Questions tagged [indirection]
7 questions
4
votes
4 answers
Command substitution in quotes cannot be used to call the variable
Say:
variable="Something that it holds"
then
echo "$variable" will output: Something that it holds
But say I also do:
var2="variable";
echo "\$$(echo $var2)"
will just output: $variable
And not: Something that it holds
Can anyone tell me about…
Swaroop Joshi
- 149
- 3
4
votes
4 answers
zsh testing existence of a key in an associative array via indirect expansion
So I know that you can test for the existence of a regular parameter via indirect expansion by doing something like:
foo=1
bar=foo
(( ${(P)+bar} )) && print "$bar exists"
And I know you can test for the existence of a key inside an associative…
Matt Wilder
- 71
- 4
2
votes
2 answers
How many levels of indirection can I apply in Bash?
In bash, I understand we can have variable indirect expansion via two ways:
Using declare: declare -n foo=bar
Using the ${!..} expansion.
We can combine both:
declare -n foo=SHELL
bar=foo
echo ${!bar}
gives:
/bin/bash
Is it possible to extend…
user322103
2
votes
1 answer
How to access further members of an array when using bash variable indirection?
Consider the following example, it seems it's working fine with the index 0:
$ a1=(1 2 3)
$ a2=(a b c)
$ for x in a1 a2; do echo "${!x}"; done
1
a
$ for x in a1 a2; do echo "${!x[0]}"; done
1
a
However with the index 1 it prints nothing:
$ for x in…
NarūnasK
- 2,276
- 4
- 25
- 35
1
vote
3 answers
Indirect access in bash arrays
I am trying to do the following indirect task:
host_1=(192.168.0.100 user1 pass1)
host_2=(192.168.0.101 user2 pass2)
hostlist=( "host_1" "host_2" )
for item in ${hostlist[@]};…
sxiii
- 9
- 2
0
votes
1 answer
Why don't I need to use bash indirection with string substitution?
One challenging thing I've encountered in bash scripting is using variable expansion in a recursive way.
However, I was able to figure out [through ChatGPT] that I could use indirect subsitution to reference positional parameter…
thinksinbinary
- 177
- 5
0
votes
2 answers
Concatenating string to form an existing variable name and working within array enclosure format
#!/bin/bash
mat_1=(ServerAB ServerFR ServerPE ServerAM ServerHU)
st="mat_1";
indirect_var='${'${st}'[@]}'
#(Please, see the "--Desired Ouput Section--" in comments)
#----- What is Hapenning now at output ----
echo Values of "mat_1 ":…
dcubaz
- 23
- 5