how does reference an associative array variable by the value of a variable in bash? The Next CEO of Stack OverflowEscaping strings in associative arrays (bash)Loop over associative arrays by substringCreate array in bash with variables as array nameDynamically create array in bash with variables as array namebash array with variable in the nameAdd text to each value while looping thru and printing them in a array?Bash Array Contains false positivesbash + how to combine output values in the same lineHow to create a possibly empty array from filename glob?Bash - Looping through nested for loop using arrays
Only print output after finding pattern
Why doesn't a table tennis ball float on the surface? How do we calculate buoyancy here?
What do "high sea" and "carry" mean in this sentence?
Whats the best way to handle refactoring a big file?
How should I support this large drywall patch?
What is the difference between "behavior" and "behaviour"?
Why do remote companies require working in the US?
Customer Requests (Sometimes) Drive Me Bonkers!
Failed to fetch jessie backports repository
India just shot down a satellite from the ground. At what altitude range is the resulting debris field?
Shade part of a Venn diagram
Does the Brexit deal have to be agreed by both Houses?
Return the Closest Prime Number
Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
How to safely derail a train during transit?
What makes a siege story/plot interesting?
Anatomically Correct Strange Women In Ponds Distributing Swords
Describing a person. What needs to be mentioned?
Where to find order of arguments for default functions
Why here is plural "We went to the movies last night."
How to make a software documentation "officially" citable?
Natural language into sentence logic
How to write papers efficiently when English isn't my first language?
how does reference an associative array variable by the value of a variable in bash?
The Next CEO of Stack OverflowEscaping strings in associative arrays (bash)Loop over associative arrays by substringCreate array in bash with variables as array nameDynamically create array in bash with variables as array namebash array with variable in the nameAdd text to each value while looping thru and printing them in a array?Bash Array Contains false positivesbash + how to combine output values in the same lineHow to create a possibly empty array from filename glob?Bash - Looping through nested for loop using arrays
I have three associative arrays:
declare -A start_obj end_obj gopath
start_obj['one']="start-obj-one"
start_obj['two']="start-obj-two"
end_obj['one']="end-obj-one"
end_obj['two']="end-obj-two"
gopath['start']="/path/to/start"
gopath['end']="/path/to/end"
I want to get the key and value of start_obj
, end_obj
arrays by the key of the gopath
array, code show as below:
for t in "$!gopath[@]"
do
current=$t"_obj"[@]
cd $gopath[$t]
for k in $!current
do
printf "[$t]key is : $k ; value is : $current[$k]n"
done
done
But , the result of this code execution is :
[start]key is : start-obj-one ; value is : start_obj[@]
[start]key is : start-obj-two ; value is : start_obj[@]
[end]key is : end-obj-one ; value is : end_obj[@]
[end]key is : end-obj-two ; value is : end_obj[@]
The result I expect is:
[start]key is : one ; value is : start-obj-one
[start]key is : two ; value is : start-obj-two
[end]key is : one ; value is : end-obj-one
[end]key is : two ; value is : end-obj-two
So,how should I modify my code to get the results I expected?
bash shell-script shell
New contributor
add a comment |
I have three associative arrays:
declare -A start_obj end_obj gopath
start_obj['one']="start-obj-one"
start_obj['two']="start-obj-two"
end_obj['one']="end-obj-one"
end_obj['two']="end-obj-two"
gopath['start']="/path/to/start"
gopath['end']="/path/to/end"
I want to get the key and value of start_obj
, end_obj
arrays by the key of the gopath
array, code show as below:
for t in "$!gopath[@]"
do
current=$t"_obj"[@]
cd $gopath[$t]
for k in $!current
do
printf "[$t]key is : $k ; value is : $current[$k]n"
done
done
But , the result of this code execution is :
[start]key is : start-obj-one ; value is : start_obj[@]
[start]key is : start-obj-two ; value is : start_obj[@]
[end]key is : end-obj-one ; value is : end_obj[@]
[end]key is : end-obj-two ; value is : end_obj[@]
The result I expect is:
[start]key is : one ; value is : start-obj-one
[start]key is : two ; value is : start-obj-two
[end]key is : one ; value is : end-obj-one
[end]key is : two ; value is : end-obj-two
So,how should I modify my code to get the results I expected?
bash shell-script shell
New contributor
add a comment |
I have three associative arrays:
declare -A start_obj end_obj gopath
start_obj['one']="start-obj-one"
start_obj['two']="start-obj-two"
end_obj['one']="end-obj-one"
end_obj['two']="end-obj-two"
gopath['start']="/path/to/start"
gopath['end']="/path/to/end"
I want to get the key and value of start_obj
, end_obj
arrays by the key of the gopath
array, code show as below:
for t in "$!gopath[@]"
do
current=$t"_obj"[@]
cd $gopath[$t]
for k in $!current
do
printf "[$t]key is : $k ; value is : $current[$k]n"
done
done
But , the result of this code execution is :
[start]key is : start-obj-one ; value is : start_obj[@]
[start]key is : start-obj-two ; value is : start_obj[@]
[end]key is : end-obj-one ; value is : end_obj[@]
[end]key is : end-obj-two ; value is : end_obj[@]
The result I expect is:
[start]key is : one ; value is : start-obj-one
[start]key is : two ; value is : start-obj-two
[end]key is : one ; value is : end-obj-one
[end]key is : two ; value is : end-obj-two
So,how should I modify my code to get the results I expected?
bash shell-script shell
New contributor
I have three associative arrays:
declare -A start_obj end_obj gopath
start_obj['one']="start-obj-one"
start_obj['two']="start-obj-two"
end_obj['one']="end-obj-one"
end_obj['two']="end-obj-two"
gopath['start']="/path/to/start"
gopath['end']="/path/to/end"
I want to get the key and value of start_obj
, end_obj
arrays by the key of the gopath
array, code show as below:
for t in "$!gopath[@]"
do
current=$t"_obj"[@]
cd $gopath[$t]
for k in $!current
do
printf "[$t]key is : $k ; value is : $current[$k]n"
done
done
But , the result of this code execution is :
[start]key is : start-obj-one ; value is : start_obj[@]
[start]key is : start-obj-two ; value is : start_obj[@]
[end]key is : end-obj-one ; value is : end_obj[@]
[end]key is : end-obj-two ; value is : end_obj[@]
The result I expect is:
[start]key is : one ; value is : start-obj-one
[start]key is : two ; value is : start-obj-two
[end]key is : one ; value is : end-obj-one
[end]key is : two ; value is : end-obj-two
So,how should I modify my code to get the results I expected?
bash shell-script shell
bash shell-script shell
New contributor
New contributor
edited yesterday
glenn jackman
52.8k573114
52.8k573114
New contributor
asked yesterday
ShaunShaun
83
83
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
With bash
4.3 or newer, you could use nameref variables:
for t in "$!gopath[@]"; do
(
typeset -n current="$t_obj"
cd -P -- "$gopath[$t]" || exit
for k in "$!current[@]"
do
printf '%sn' "[$t]key is: $k; value is: $current[$k]"
done
)
done
With older versions, you'd need to use eval
:
for t in "$!gopath[@]"; do
(
cd -P -- "$gopath[$t]" || exit
eval '
for k in "$!'"$t"'_obj[@]"
do
printf "%sn" "[$t]key is: $k; value is: $'"$t"'_obj[$k]"
done
'
)
done
bash
has a variable indirection operator: $!varname
, unrelated to the $!hash[@]
operator (and actually closer to the reverse of what $!varname
is in ksh93), but it cannot be combined with the $!hash[@]
operator (varname=hash; for key in "$!!varname[@]"...
won't work). For a shell with a usable variable indirection operator here (and that has supported associative arrays for much longer), you can look at zsh
(using $(P)varname
) which also lets you loop over both key and value at the same time:
typeset -A start_obj end_obj gopath
start_obj=(
one start-obj-one
two start-obj-two
)
end_obj=(
one end-obj-one
two end-obj-two
)
gopath=(
start /path/to/start
end /path/to/end
)
for t dir ("$(kv@)gopath") (
cd -P -- "$dir" || exit
current=$t_obj
for key value ("$(kvP@)current")
printf '%sn' "[$t]key is: $key; value is: $value}"
)
In any case, in both bash
and zsh
(and ksh93
the first shell that introduced associative arrays and that bash
tried to copy), associative arrays are implemented as hash tables, so the elements are not stored in any particular order, so those code above will loop over them in a seemingly random order.
Thanks , my bash version is 4.2.46(2)-release ,I got an error: "typeset: -n: invalid option" .It seems that it does not support "typeset -n".
– Shaun
yesterday
@shaun, see edit.
– Stéphane Chazelas
yesterday
The code you append is not work., i got the resule : "[start]key is : one ; value is : " , value is null . But, i know your thoughts. Thanks.
– Shaun
yesterday
@shaun, yes sorry, I had forgotten to replace the second occurrence ofcurrent
. See edit.
– Stéphane Chazelas
yesterday
add a comment |
With earlier bash versions, you can do this with indexed arrays, but not with associative arrays, using variable indirection:
$ declare -p start_obj end_obj gopath
declare -A start_obj='([one]="start-obj-one" [two]="start-obj-two" )'
declare -A end_obj='([one]="end-obj-one" [two]="end-obj-two" )'
declare -A gopath='([start]="/path/to/start" [end]="/path/to/end" )'
$ for t in "$!gopath[@]"; do tmp="$t_obj[@]"; ary=( "$!tmp" ); declare -p ary; done
declare -a ary='([0]="start-obj-one" [1]="start-obj-two")'
declare -a ary='([0]="end-obj-one" [1]="end-obj-two")'
Note how we can get the values but not the keys of the associative arrays.
For more info, see the 4th paragraph in https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Shaun is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508930%2fhow-does-reference-an-associative-array-variable-by-the-value-of-a-variable-in-b%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With bash
4.3 or newer, you could use nameref variables:
for t in "$!gopath[@]"; do
(
typeset -n current="$t_obj"
cd -P -- "$gopath[$t]" || exit
for k in "$!current[@]"
do
printf '%sn' "[$t]key is: $k; value is: $current[$k]"
done
)
done
With older versions, you'd need to use eval
:
for t in "$!gopath[@]"; do
(
cd -P -- "$gopath[$t]" || exit
eval '
for k in "$!'"$t"'_obj[@]"
do
printf "%sn" "[$t]key is: $k; value is: $'"$t"'_obj[$k]"
done
'
)
done
bash
has a variable indirection operator: $!varname
, unrelated to the $!hash[@]
operator (and actually closer to the reverse of what $!varname
is in ksh93), but it cannot be combined with the $!hash[@]
operator (varname=hash; for key in "$!!varname[@]"...
won't work). For a shell with a usable variable indirection operator here (and that has supported associative arrays for much longer), you can look at zsh
(using $(P)varname
) which also lets you loop over both key and value at the same time:
typeset -A start_obj end_obj gopath
start_obj=(
one start-obj-one
two start-obj-two
)
end_obj=(
one end-obj-one
two end-obj-two
)
gopath=(
start /path/to/start
end /path/to/end
)
for t dir ("$(kv@)gopath") (
cd -P -- "$dir" || exit
current=$t_obj
for key value ("$(kvP@)current")
printf '%sn' "[$t]key is: $key; value is: $value}"
)
In any case, in both bash
and zsh
(and ksh93
the first shell that introduced associative arrays and that bash
tried to copy), associative arrays are implemented as hash tables, so the elements are not stored in any particular order, so those code above will loop over them in a seemingly random order.
Thanks , my bash version is 4.2.46(2)-release ,I got an error: "typeset: -n: invalid option" .It seems that it does not support "typeset -n".
– Shaun
yesterday
@shaun, see edit.
– Stéphane Chazelas
yesterday
The code you append is not work., i got the resule : "[start]key is : one ; value is : " , value is null . But, i know your thoughts. Thanks.
– Shaun
yesterday
@shaun, yes sorry, I had forgotten to replace the second occurrence ofcurrent
. See edit.
– Stéphane Chazelas
yesterday
add a comment |
With bash
4.3 or newer, you could use nameref variables:
for t in "$!gopath[@]"; do
(
typeset -n current="$t_obj"
cd -P -- "$gopath[$t]" || exit
for k in "$!current[@]"
do
printf '%sn' "[$t]key is: $k; value is: $current[$k]"
done
)
done
With older versions, you'd need to use eval
:
for t in "$!gopath[@]"; do
(
cd -P -- "$gopath[$t]" || exit
eval '
for k in "$!'"$t"'_obj[@]"
do
printf "%sn" "[$t]key is: $k; value is: $'"$t"'_obj[$k]"
done
'
)
done
bash
has a variable indirection operator: $!varname
, unrelated to the $!hash[@]
operator (and actually closer to the reverse of what $!varname
is in ksh93), but it cannot be combined with the $!hash[@]
operator (varname=hash; for key in "$!!varname[@]"...
won't work). For a shell with a usable variable indirection operator here (and that has supported associative arrays for much longer), you can look at zsh
(using $(P)varname
) which also lets you loop over both key and value at the same time:
typeset -A start_obj end_obj gopath
start_obj=(
one start-obj-one
two start-obj-two
)
end_obj=(
one end-obj-one
two end-obj-two
)
gopath=(
start /path/to/start
end /path/to/end
)
for t dir ("$(kv@)gopath") (
cd -P -- "$dir" || exit
current=$t_obj
for key value ("$(kvP@)current")
printf '%sn' "[$t]key is: $key; value is: $value}"
)
In any case, in both bash
and zsh
(and ksh93
the first shell that introduced associative arrays and that bash
tried to copy), associative arrays are implemented as hash tables, so the elements are not stored in any particular order, so those code above will loop over them in a seemingly random order.
Thanks , my bash version is 4.2.46(2)-release ,I got an error: "typeset: -n: invalid option" .It seems that it does not support "typeset -n".
– Shaun
yesterday
@shaun, see edit.
– Stéphane Chazelas
yesterday
The code you append is not work., i got the resule : "[start]key is : one ; value is : " , value is null . But, i know your thoughts. Thanks.
– Shaun
yesterday
@shaun, yes sorry, I had forgotten to replace the second occurrence ofcurrent
. See edit.
– Stéphane Chazelas
yesterday
add a comment |
With bash
4.3 or newer, you could use nameref variables:
for t in "$!gopath[@]"; do
(
typeset -n current="$t_obj"
cd -P -- "$gopath[$t]" || exit
for k in "$!current[@]"
do
printf '%sn' "[$t]key is: $k; value is: $current[$k]"
done
)
done
With older versions, you'd need to use eval
:
for t in "$!gopath[@]"; do
(
cd -P -- "$gopath[$t]" || exit
eval '
for k in "$!'"$t"'_obj[@]"
do
printf "%sn" "[$t]key is: $k; value is: $'"$t"'_obj[$k]"
done
'
)
done
bash
has a variable indirection operator: $!varname
, unrelated to the $!hash[@]
operator (and actually closer to the reverse of what $!varname
is in ksh93), but it cannot be combined with the $!hash[@]
operator (varname=hash; for key in "$!!varname[@]"...
won't work). For a shell with a usable variable indirection operator here (and that has supported associative arrays for much longer), you can look at zsh
(using $(P)varname
) which also lets you loop over both key and value at the same time:
typeset -A start_obj end_obj gopath
start_obj=(
one start-obj-one
two start-obj-two
)
end_obj=(
one end-obj-one
two end-obj-two
)
gopath=(
start /path/to/start
end /path/to/end
)
for t dir ("$(kv@)gopath") (
cd -P -- "$dir" || exit
current=$t_obj
for key value ("$(kvP@)current")
printf '%sn' "[$t]key is: $key; value is: $value}"
)
In any case, in both bash
and zsh
(and ksh93
the first shell that introduced associative arrays and that bash
tried to copy), associative arrays are implemented as hash tables, so the elements are not stored in any particular order, so those code above will loop over them in a seemingly random order.
With bash
4.3 or newer, you could use nameref variables:
for t in "$!gopath[@]"; do
(
typeset -n current="$t_obj"
cd -P -- "$gopath[$t]" || exit
for k in "$!current[@]"
do
printf '%sn' "[$t]key is: $k; value is: $current[$k]"
done
)
done
With older versions, you'd need to use eval
:
for t in "$!gopath[@]"; do
(
cd -P -- "$gopath[$t]" || exit
eval '
for k in "$!'"$t"'_obj[@]"
do
printf "%sn" "[$t]key is: $k; value is: $'"$t"'_obj[$k]"
done
'
)
done
bash
has a variable indirection operator: $!varname
, unrelated to the $!hash[@]
operator (and actually closer to the reverse of what $!varname
is in ksh93), but it cannot be combined with the $!hash[@]
operator (varname=hash; for key in "$!!varname[@]"...
won't work). For a shell with a usable variable indirection operator here (and that has supported associative arrays for much longer), you can look at zsh
(using $(P)varname
) which also lets you loop over both key and value at the same time:
typeset -A start_obj end_obj gopath
start_obj=(
one start-obj-one
two start-obj-two
)
end_obj=(
one end-obj-one
two end-obj-two
)
gopath=(
start /path/to/start
end /path/to/end
)
for t dir ("$(kv@)gopath") (
cd -P -- "$dir" || exit
current=$t_obj
for key value ("$(kvP@)current")
printf '%sn' "[$t]key is: $key; value is: $value}"
)
In any case, in both bash
and zsh
(and ksh93
the first shell that introduced associative arrays and that bash
tried to copy), associative arrays are implemented as hash tables, so the elements are not stored in any particular order, so those code above will loop over them in a seemingly random order.
edited yesterday
answered yesterday
Stéphane ChazelasStéphane Chazelas
312k57589946
312k57589946
Thanks , my bash version is 4.2.46(2)-release ,I got an error: "typeset: -n: invalid option" .It seems that it does not support "typeset -n".
– Shaun
yesterday
@shaun, see edit.
– Stéphane Chazelas
yesterday
The code you append is not work., i got the resule : "[start]key is : one ; value is : " , value is null . But, i know your thoughts. Thanks.
– Shaun
yesterday
@shaun, yes sorry, I had forgotten to replace the second occurrence ofcurrent
. See edit.
– Stéphane Chazelas
yesterday
add a comment |
Thanks , my bash version is 4.2.46(2)-release ,I got an error: "typeset: -n: invalid option" .It seems that it does not support "typeset -n".
– Shaun
yesterday
@shaun, see edit.
– Stéphane Chazelas
yesterday
The code you append is not work., i got the resule : "[start]key is : one ; value is : " , value is null . But, i know your thoughts. Thanks.
– Shaun
yesterday
@shaun, yes sorry, I had forgotten to replace the second occurrence ofcurrent
. See edit.
– Stéphane Chazelas
yesterday
Thanks , my bash version is 4.2.46(2)-release ,I got an error: "typeset: -n: invalid option" .It seems that it does not support "typeset -n".
– Shaun
yesterday
Thanks , my bash version is 4.2.46(2)-release ,I got an error: "typeset: -n: invalid option" .It seems that it does not support "typeset -n".
– Shaun
yesterday
@shaun, see edit.
– Stéphane Chazelas
yesterday
@shaun, see edit.
– Stéphane Chazelas
yesterday
The code you append is not work., i got the resule : "[start]key is : one ; value is : " , value is null . But, i know your thoughts. Thanks.
– Shaun
yesterday
The code you append is not work., i got the resule : "[start]key is : one ; value is : " , value is null . But, i know your thoughts. Thanks.
– Shaun
yesterday
@shaun, yes sorry, I had forgotten to replace the second occurrence of
current
. See edit.– Stéphane Chazelas
yesterday
@shaun, yes sorry, I had forgotten to replace the second occurrence of
current
. See edit.– Stéphane Chazelas
yesterday
add a comment |
With earlier bash versions, you can do this with indexed arrays, but not with associative arrays, using variable indirection:
$ declare -p start_obj end_obj gopath
declare -A start_obj='([one]="start-obj-one" [two]="start-obj-two" )'
declare -A end_obj='([one]="end-obj-one" [two]="end-obj-two" )'
declare -A gopath='([start]="/path/to/start" [end]="/path/to/end" )'
$ for t in "$!gopath[@]"; do tmp="$t_obj[@]"; ary=( "$!tmp" ); declare -p ary; done
declare -a ary='([0]="start-obj-one" [1]="start-obj-two")'
declare -a ary='([0]="end-obj-one" [1]="end-obj-two")'
Note how we can get the values but not the keys of the associative arrays.
For more info, see the 4th paragraph in https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
add a comment |
With earlier bash versions, you can do this with indexed arrays, but not with associative arrays, using variable indirection:
$ declare -p start_obj end_obj gopath
declare -A start_obj='([one]="start-obj-one" [two]="start-obj-two" )'
declare -A end_obj='([one]="end-obj-one" [two]="end-obj-two" )'
declare -A gopath='([start]="/path/to/start" [end]="/path/to/end" )'
$ for t in "$!gopath[@]"; do tmp="$t_obj[@]"; ary=( "$!tmp" ); declare -p ary; done
declare -a ary='([0]="start-obj-one" [1]="start-obj-two")'
declare -a ary='([0]="end-obj-one" [1]="end-obj-two")'
Note how we can get the values but not the keys of the associative arrays.
For more info, see the 4th paragraph in https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
add a comment |
With earlier bash versions, you can do this with indexed arrays, but not with associative arrays, using variable indirection:
$ declare -p start_obj end_obj gopath
declare -A start_obj='([one]="start-obj-one" [two]="start-obj-two" )'
declare -A end_obj='([one]="end-obj-one" [two]="end-obj-two" )'
declare -A gopath='([start]="/path/to/start" [end]="/path/to/end" )'
$ for t in "$!gopath[@]"; do tmp="$t_obj[@]"; ary=( "$!tmp" ); declare -p ary; done
declare -a ary='([0]="start-obj-one" [1]="start-obj-two")'
declare -a ary='([0]="end-obj-one" [1]="end-obj-two")'
Note how we can get the values but not the keys of the associative arrays.
For more info, see the 4th paragraph in https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
With earlier bash versions, you can do this with indexed arrays, but not with associative arrays, using variable indirection:
$ declare -p start_obj end_obj gopath
declare -A start_obj='([one]="start-obj-one" [two]="start-obj-two" )'
declare -A end_obj='([one]="end-obj-one" [two]="end-obj-two" )'
declare -A gopath='([start]="/path/to/start" [end]="/path/to/end" )'
$ for t in "$!gopath[@]"; do tmp="$t_obj[@]"; ary=( "$!tmp" ); declare -p ary; done
declare -a ary='([0]="start-obj-one" [1]="start-obj-two")'
declare -a ary='([0]="end-obj-one" [1]="end-obj-two")'
Note how we can get the values but not the keys of the associative arrays.
For more info, see the 4th paragraph in https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
answered yesterday
glenn jackmanglenn jackman
52.8k573114
52.8k573114
add a comment |
add a comment |
Shaun is a new contributor. Be nice, and check out our Code of Conduct.
Shaun is a new contributor. Be nice, and check out our Code of Conduct.
Shaun is a new contributor. Be nice, and check out our Code of Conduct.
Shaun is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508930%2fhow-does-reference-an-associative-array-variable-by-the-value-of-a-variable-in-b%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown