Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

5 votes
2 answers
363 views
How to do indirect variable substitution in a GNU Makefile?
In the shell I can do ``` $ a=b b=c; eval echo \$$a c ``` How do I do the same with GNU Make's `$(eval)` function? ``` $ cat Makefile a=b b=c x:; echo {What goes here in order to get:} $ make c ```
In the shell I can do
$ a=b b=c; eval echo \$$a
c
How do I do the same with GNU Make's $(eval) function?
$ cat Makefile
a=b
b=c
x:; echo {What goes here in order to get:}
$ make
c
Dan Jacobson (560 rep)
Jun 30, 2025, 05:33 AM • Last activity: Jun 30, 2025, 06:55 AM
1 votes
4 answers
116 views
Synthetic Variables in Bash (was: How to address a variable ceated with indirection)
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 it was created: env | grep VAR_ONE #this displays: "VAR_ONE=thisIsVarOne" # use it in a simple echo comman...
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 it was created:
env | grep VAR_ONE      #this displays: "VAR_ONE=thisIsVarOne"
# use it in a simple echo command:
echo VAR_ONE=$VAR_ONE  #this displays: "VAR_ONE=thisIsVarOne"

# create a new var with the name that contains the value of VAR_ONE ("newvarthisIsVarOne") in its name:
export newvar${VAR_ONE}="somePrefix${VAR_ONE}"  
# verify if it has been created:
env | grep newvar  # this displays: "newvarthisIsVarOne=somePrefixthisIsVarOne"
### Now my question: How do I use that new, syntetic, variable? Please note that: env | grep newvar shows that there has been created a variable with the name newvar${VAR_ONE} But I can't **address** that new variable, e.g. in an echo command. I tried: echo ${newvar${VAR_ONE}} But that gave me bash: ${newvar${VAR_ONE}}: bad substitution ### Constraints: 1. The value of VAR_ONE can change, so I can't use that to address the newly created variable 2. I can't declare a new variable with a static name. For usage, further in the script, I need the keyword VAR_ONE as part of the name of the newly created variable. The name of the variable needs to be something like: newvar${VAR_ONE} Thank you in advance ChriV
Chris V. (21 rep)
Nov 17, 2023, 06:11 PM • Last activity: Nov 18, 2023, 05:26 AM
0 votes
1 answers
151 views
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 values: ``` linkerArgs+=("${!i}") ``` However, if I am using string sub...
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 values:
linkerArgs+=("${!i}")
However, if I am using string substitution, it's fine for me to just use expansion in a recursive manner as you would figure logically:
"${x// /$newChar}"
Why does the bash interpreter consider the latter type of expansion (with just the dollar sign inside the brackets) as "bad substitution" when used in the context of assigning items to an array, but not with string substitution?
thinksinbinary (217 rep)
Jun 6, 2023, 02:15 AM • Last activity: Jun 6, 2023, 06:52 PM
4 votes
4 answers
1635 views
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 array by doing something like: foo=([abc]=1) (( ${+foo[a...
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 array by doing something like: foo=([abc]=1) (( ${+foo[abc]} )) && print "abc exists" However I can't figure out how to combine the two and test for the existence of a key inside an associative array via indirect expansion. Is this possible without using eval? I tried several combinations including the following, and none of them worked: foo=([abc]=1) bar=foo (( ${(P)+bar[abc]} )) && print "$bar has key abc" # Test fails (( ${(P)+${bar}[abc]} )) && print "$bar has key abc" # Passes for nonexistant keys (( ${${(P)+bar}[abc]} )) && print "$bar has key abc" # Test fails (( ${${(P)bar}+[abc]} )) && print "$bar has key abc" # prints "zsh: bad output format specification"
Matt Wilder (71 rep)
Jan 23, 2020, 11:52 PM • Last activity: Aug 7, 2021, 04:41 PM
0 votes
2 answers
674 views
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 ": ${mat_1[@]} echo Indirect value of "mat_1": ${!indirect_var} #...
#!/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 ": ${mat_1[@]}
echo Indirect value of "mat_1": ${!indirect_var}

# echo Indirect value of "mat_1": ${!indirect_var}  ##output> ${mat_1[@]}
# But it is not recognized as a real ${mat_1[@]}.

# -- What actually have ----
for (( i=0; i  function-name $1, where $1 is
                                     # an string, and this string already
                                     # exist above , it will interpret as
                                     # an existing array
 

do
   echo ${mat_1[i]};
done

#And I would like those strings 
#that are part of the name of existing 
#variables , will be treated as an input
# and this loop works. I will show What I have done,
# what I have reached, and what I expect to have.
# Thank you!


#------What I expect works-----

#for (( i=0; i < ${#$st[@]}; i++ ))  #I would like $st works like 'mat_1'
                                     #and this loop can be run without 
                                     #problems
#do
#   echo ${$st[i]};
#done

#--- Actual Output ------------
#$ ./matrix.sh 
# Values of mat_1 : ServerAB ServerFR ServerPE ServerAM ServerHU
# ./matrix.sh: line 8: ${mat_1[@]}: invalid variable name
# ServerAB
# ServerFR
# ServerPE

#--- Desired Output ----------

#$ ./matrix.sh 
# Values of mat_1 : ServerAB ServerFR ServerPE ServerAM ServerHU
# Indirect Value of mat_1: ServerAB ServerFR ServerPE ServerAM ServerHU
# ServerAB
# ServerFR
# ServerPE
# ServerAM
# ServerHU
" Hi Friends, I would like some ideas to achieve the following". - I have many existing 'arrays vars' that I would like to be called within a for loop by 'concatenating strings' in order to form those ' existing array names'.But in the Script above I just only Working with 01 array 'var_mat1'. I jut only need work for 01 array... - Example of the existing 'array names': var_mat1=( ".." ".." ".." ) var_mat2=( ".." ".." ".." ) . . . var_matN=( ".." ".." ".." )
dcubaz (23 rep)
Apr 12, 2021, 04:23 PM • Last activity: Apr 12, 2021, 09:13 PM
4 votes
4 answers
1281 views
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 what feature of Unix is in play, her...
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 what feature of Unix is in play, here?
Swaroop Joshi (149 rep)
Jul 22, 2020, 06:43 PM • Last activity: Jul 23, 2020, 05:18 PM
1 votes
3 answers
333 views
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[@]}; do current_host_ip=${!item[0]} current_host_user=${!item[1]} current_host_pass=${!item[2]} echo "IP: $current_host_ip Us...
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[@]}; do

current_host_ip=${!item}
current_host_user=${!item}
current_host_pass=${!item}

echo "IP: $current_host_ip User: $current_host_user Pass: $current_host_pass"

done
I'm trying to understand how should I perform this indirect request so I pull the hostname from the array "hostlist", and then I should do indirect request to pull the host 1 IP, user and pass. But when I'm trying to do it, I'm stuck with either only first variable (only IP), or all variables inside one (if I add [@] into the end of variable name), empty result, or numbers from array. I can't understand how can I first copy the host_1 array into current_ variables and then (after my script does some work) I need to pass the host_2 variables to the same variables current_. Can you pinpoint my mistake? I think this is the solution to the problem I just can't adopt it: https://unix.stackexchange.com/questions/20171/indirect-return-of-all-elements-in-an-array
sxiii (9 rep)
Jul 17, 2020, 08:06 PM • Last activity: Jul 19, 2020, 01:56 AM
2 votes
2 answers
435 views
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 this to more levels? --- It's mostly as for wri...
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 this to more levels? --- It's mostly as for writing obfuscated code - some of my friends are challenging each other.
user322103
Nov 21, 2018, 01:30 PM • Last activity: Nov 25, 2018, 09:53 PM
2 votes
1 answers
109 views
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 a1 a2; do echo "${!x[1]}"; done Arrays just by...
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}"; done 1 a However with the index 1 it prints nothing: $ for x in a1 a2; do echo "${!x}"; done Arrays just by themselves are fine: $ echo "${a1} ${a2}" 2 b ### Edit - A real life use case based on *ilkkachu* answer SHIBB=(https://shibboleth.net/downloads/service-provider/3.0.2/ shibboleth-sp-3.0.2 .tar.gz) XERCES=(http://apache.mirrors.nublue.co.uk//xerces/c/3/sources/ xerces-c-3.2.1 .tar.gz) XMLSEC=(http://apache.mirror.anlx.net/santuario/c-library/ xml-security-c-2.0.1 .tar.gz) XMLTOOL=(http://shibboleth.net/downloads/c++-opensaml/latest/ xmltooling-3.0.2 .tar.gz) OPENSAML=(http://shibboleth.net/downloads/c++-opensaml/latest/ opensaml-3.0.0 .tar.gz) typeset -n x for x in XERCES XMLSEC XMLTOOL OPENSAML SHIBB; do url="${x}" app="${x}" ext="${x}" [ -f "./${app}${ext}" ] || wget "${url}${app}${ext}" tar -xf "./${app}${ext}" cd "./${app}" && ./configure && make -j2 && make install && ldconfig cd .. done
NarūnasK (2525 rep)
Sep 4, 2018, 08:03 PM • Last activity: Oct 15, 2018, 08:03 PM
Showing page 1 of 9 total questions