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










1















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?










share|improve this question









New contributor




Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    1















    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?










    share|improve this question









    New contributor




    Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      1












      1








      1








      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?










      share|improve this question









      New contributor




      Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      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






      share|improve this question









      New contributor




      Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday









      glenn jackman

      52.8k573114




      52.8k573114






      New contributor




      Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      ShaunShaun

      83




      83




      New contributor




      Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Shaun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          2














          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.






          share|improve this answer

























          • 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 of current. See edit.

            – Stéphane Chazelas
            yesterday


















          0














          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






          share|improve this answer























            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.









            draft saved

            draft discarded


















            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









            2














            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.






            share|improve this answer

























            • 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 of current. See edit.

              – Stéphane Chazelas
              yesterday















            2














            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.






            share|improve this answer

























            • 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 of current. See edit.

              – Stéphane Chazelas
              yesterday













            2












            2








            2







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 of current. 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












            • @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 of current. 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













            0














            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






            share|improve this answer



























              0














              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






              share|improve this answer

























                0












                0








                0







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                glenn jackmanglenn jackman

                52.8k573114




                52.8k573114




















                    Shaun is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                    Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                    Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.