Pass associative array as parameter list to script












5















In a script I have an associative array like:



declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )


Is there a single command to tranform that into a parameter list in the form



--key1=value1 --key2=value2


without having to manually re-write



 --key1="${VARS[key1]}" --key2="${VARS[key2]}"


the use-case I had in mind was to pass the array to a script as a list of parameters, like



my_script.sh $(to_param_list $VARS)









share|improve this question



























    5















    In a script I have an associative array like:



    declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )


    Is there a single command to tranform that into a parameter list in the form



    --key1=value1 --key2=value2


    without having to manually re-write



     --key1="${VARS[key1]}" --key2="${VARS[key2]}"


    the use-case I had in mind was to pass the array to a script as a list of parameters, like



    my_script.sh $(to_param_list $VARS)









    share|improve this question

























      5












      5








      5


      2






      In a script I have an associative array like:



      declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )


      Is there a single command to tranform that into a parameter list in the form



      --key1=value1 --key2=value2


      without having to manually re-write



       --key1="${VARS[key1]}" --key2="${VARS[key2]}"


      the use-case I had in mind was to pass the array to a script as a list of parameters, like



      my_script.sh $(to_param_list $VARS)









      share|improve this question














      In a script I have an associative array like:



      declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )


      Is there a single command to tranform that into a parameter list in the form



      --key1=value1 --key2=value2


      without having to manually re-write



       --key1="${VARS[key1]}" --key2="${VARS[key2]}"


      the use-case I had in mind was to pass the array to a script as a list of parameters, like



      my_script.sh $(to_param_list $VARS)






      bash parameter associative-array






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 14 hours ago









      Matteo TassinariMatteo Tassinari

      1336




      1336






















          1 Answer
          1






          active

          oldest

          votes


















          7














          With a helper function:



          #!/bin/bash

          to_param_list () {
          declare -n outlist=$1
          declare -n inhash=$2

          for param in "${!inhash[@]}"; do
          outlist+=( "--$param=${inhash[$param]}" )
          done
          }

          declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )

          to_param_list list my_vars
          my_script.sh "${list[@]}"


          The final command in the above script would expand to the equivalent of having written



          my_script.sh "--key2=value" "--key1=value1"


          The to_param_list function takes the name of an array variable and the name of an associative array variable and uses these to create two "name reference" variables in the function (namerefs were introduced in bash release 4.3). These are then use to populate the given array variable with the keys and values in the appropriate format from the associative array.



          The loop in the function iterates over "${!inhash[@]}", which is the list of individually quoted keys in your associative array.



          Once the function call returns, the script would use the array to call your other script or command.



          Running the above with



          declare -A my_vars=( ["key1"]="hello world" ["key2"]="some thing" ["key3"]="* * *" )


          and with printf 'Arg: %sn' being the command, the script would output



          Arg: --key2=some thing
          Arg: --key3=* * *
          Arg: --key1=hello world


          This shows that the options are generated without word splitting or filename globbing coming into effect. It also shows that the order of the keys may not be preserved since accessing the keys out of an associative array will do so in a fairly random order.





          You can't really use a command substitution safely here as the result of it would be a single string. If unquoted, this string would then be split on whitespace characters (by default), which would additionally split both the keys and values of your associative array. The shell would also perform filename globbing on the resulting words. Double quoting the command substitution would not help as that would result in calling your my_script.sh with a single argument.






          share|improve this answer


























          • Thanks, I'm gonna try that right away.

            – Matteo Tassinari
            13 hours ago











          • I tried your solution, however if one of the values in the starting associative array contains any space, the resulting command will break up

            – Matteo Tassinari
            9 hours ago











          • @MatteoTassinari No it would not. If it does, then you have forgotten the double quotes in "--$param=${inhash[$param]}" or in "${list[@]}", or the script that receives the options does something wrong in parsing them.

            – Kusalananda
            8 hours ago













          • I can confirm that I used the quotes as you are showing, these parameters are passed to github.com/megastep/makeself to create an installer which, when executes, invokes a script with the given parametes, perhaps in this passage something goes wrong

            – Matteo Tassinari
            8 hours ago






          • 1





            No, it's not that, I'll try to edit my question to better show my use-case.

            – Matteo Tassinari
            5 hours ago











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506440%2fpass-associative-array-as-parameter-list-to-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          With a helper function:



          #!/bin/bash

          to_param_list () {
          declare -n outlist=$1
          declare -n inhash=$2

          for param in "${!inhash[@]}"; do
          outlist+=( "--$param=${inhash[$param]}" )
          done
          }

          declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )

          to_param_list list my_vars
          my_script.sh "${list[@]}"


          The final command in the above script would expand to the equivalent of having written



          my_script.sh "--key2=value" "--key1=value1"


          The to_param_list function takes the name of an array variable and the name of an associative array variable and uses these to create two "name reference" variables in the function (namerefs were introduced in bash release 4.3). These are then use to populate the given array variable with the keys and values in the appropriate format from the associative array.



          The loop in the function iterates over "${!inhash[@]}", which is the list of individually quoted keys in your associative array.



          Once the function call returns, the script would use the array to call your other script or command.



          Running the above with



          declare -A my_vars=( ["key1"]="hello world" ["key2"]="some thing" ["key3"]="* * *" )


          and with printf 'Arg: %sn' being the command, the script would output



          Arg: --key2=some thing
          Arg: --key3=* * *
          Arg: --key1=hello world


          This shows that the options are generated without word splitting or filename globbing coming into effect. It also shows that the order of the keys may not be preserved since accessing the keys out of an associative array will do so in a fairly random order.





          You can't really use a command substitution safely here as the result of it would be a single string. If unquoted, this string would then be split on whitespace characters (by default), which would additionally split both the keys and values of your associative array. The shell would also perform filename globbing on the resulting words. Double quoting the command substitution would not help as that would result in calling your my_script.sh with a single argument.






          share|improve this answer


























          • Thanks, I'm gonna try that right away.

            – Matteo Tassinari
            13 hours ago











          • I tried your solution, however if one of the values in the starting associative array contains any space, the resulting command will break up

            – Matteo Tassinari
            9 hours ago











          • @MatteoTassinari No it would not. If it does, then you have forgotten the double quotes in "--$param=${inhash[$param]}" or in "${list[@]}", or the script that receives the options does something wrong in parsing them.

            – Kusalananda
            8 hours ago













          • I can confirm that I used the quotes as you are showing, these parameters are passed to github.com/megastep/makeself to create an installer which, when executes, invokes a script with the given parametes, perhaps in this passage something goes wrong

            – Matteo Tassinari
            8 hours ago






          • 1





            No, it's not that, I'll try to edit my question to better show my use-case.

            – Matteo Tassinari
            5 hours ago
















          7














          With a helper function:



          #!/bin/bash

          to_param_list () {
          declare -n outlist=$1
          declare -n inhash=$2

          for param in "${!inhash[@]}"; do
          outlist+=( "--$param=${inhash[$param]}" )
          done
          }

          declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )

          to_param_list list my_vars
          my_script.sh "${list[@]}"


          The final command in the above script would expand to the equivalent of having written



          my_script.sh "--key2=value" "--key1=value1"


          The to_param_list function takes the name of an array variable and the name of an associative array variable and uses these to create two "name reference" variables in the function (namerefs were introduced in bash release 4.3). These are then use to populate the given array variable with the keys and values in the appropriate format from the associative array.



          The loop in the function iterates over "${!inhash[@]}", which is the list of individually quoted keys in your associative array.



          Once the function call returns, the script would use the array to call your other script or command.



          Running the above with



          declare -A my_vars=( ["key1"]="hello world" ["key2"]="some thing" ["key3"]="* * *" )


          and with printf 'Arg: %sn' being the command, the script would output



          Arg: --key2=some thing
          Arg: --key3=* * *
          Arg: --key1=hello world


          This shows that the options are generated without word splitting or filename globbing coming into effect. It also shows that the order of the keys may not be preserved since accessing the keys out of an associative array will do so in a fairly random order.





          You can't really use a command substitution safely here as the result of it would be a single string. If unquoted, this string would then be split on whitespace characters (by default), which would additionally split both the keys and values of your associative array. The shell would also perform filename globbing on the resulting words. Double quoting the command substitution would not help as that would result in calling your my_script.sh with a single argument.






          share|improve this answer


























          • Thanks, I'm gonna try that right away.

            – Matteo Tassinari
            13 hours ago











          • I tried your solution, however if one of the values in the starting associative array contains any space, the resulting command will break up

            – Matteo Tassinari
            9 hours ago











          • @MatteoTassinari No it would not. If it does, then you have forgotten the double quotes in "--$param=${inhash[$param]}" or in "${list[@]}", or the script that receives the options does something wrong in parsing them.

            – Kusalananda
            8 hours ago













          • I can confirm that I used the quotes as you are showing, these parameters are passed to github.com/megastep/makeself to create an installer which, when executes, invokes a script with the given parametes, perhaps in this passage something goes wrong

            – Matteo Tassinari
            8 hours ago






          • 1





            No, it's not that, I'll try to edit my question to better show my use-case.

            – Matteo Tassinari
            5 hours ago














          7












          7








          7







          With a helper function:



          #!/bin/bash

          to_param_list () {
          declare -n outlist=$1
          declare -n inhash=$2

          for param in "${!inhash[@]}"; do
          outlist+=( "--$param=${inhash[$param]}" )
          done
          }

          declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )

          to_param_list list my_vars
          my_script.sh "${list[@]}"


          The final command in the above script would expand to the equivalent of having written



          my_script.sh "--key2=value" "--key1=value1"


          The to_param_list function takes the name of an array variable and the name of an associative array variable and uses these to create two "name reference" variables in the function (namerefs were introduced in bash release 4.3). These are then use to populate the given array variable with the keys and values in the appropriate format from the associative array.



          The loop in the function iterates over "${!inhash[@]}", which is the list of individually quoted keys in your associative array.



          Once the function call returns, the script would use the array to call your other script or command.



          Running the above with



          declare -A my_vars=( ["key1"]="hello world" ["key2"]="some thing" ["key3"]="* * *" )


          and with printf 'Arg: %sn' being the command, the script would output



          Arg: --key2=some thing
          Arg: --key3=* * *
          Arg: --key1=hello world


          This shows that the options are generated without word splitting or filename globbing coming into effect. It also shows that the order of the keys may not be preserved since accessing the keys out of an associative array will do so in a fairly random order.





          You can't really use a command substitution safely here as the result of it would be a single string. If unquoted, this string would then be split on whitespace characters (by default), which would additionally split both the keys and values of your associative array. The shell would also perform filename globbing on the resulting words. Double quoting the command substitution would not help as that would result in calling your my_script.sh with a single argument.






          share|improve this answer















          With a helper function:



          #!/bin/bash

          to_param_list () {
          declare -n outlist=$1
          declare -n inhash=$2

          for param in "${!inhash[@]}"; do
          outlist+=( "--$param=${inhash[$param]}" )
          done
          }

          declare -A my_vars=( ["key1"]="value1" ["key2"]="value" )

          to_param_list list my_vars
          my_script.sh "${list[@]}"


          The final command in the above script would expand to the equivalent of having written



          my_script.sh "--key2=value" "--key1=value1"


          The to_param_list function takes the name of an array variable and the name of an associative array variable and uses these to create two "name reference" variables in the function (namerefs were introduced in bash release 4.3). These are then use to populate the given array variable with the keys and values in the appropriate format from the associative array.



          The loop in the function iterates over "${!inhash[@]}", which is the list of individually quoted keys in your associative array.



          Once the function call returns, the script would use the array to call your other script or command.



          Running the above with



          declare -A my_vars=( ["key1"]="hello world" ["key2"]="some thing" ["key3"]="* * *" )


          and with printf 'Arg: %sn' being the command, the script would output



          Arg: --key2=some thing
          Arg: --key3=* * *
          Arg: --key1=hello world


          This shows that the options are generated without word splitting or filename globbing coming into effect. It also shows that the order of the keys may not be preserved since accessing the keys out of an associative array will do so in a fairly random order.





          You can't really use a command substitution safely here as the result of it would be a single string. If unquoted, this string would then be split on whitespace characters (by default), which would additionally split both the keys and values of your associative array. The shell would also perform filename globbing on the resulting words. Double quoting the command substitution would not help as that would result in calling your my_script.sh with a single argument.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered 14 hours ago









          KusalanandaKusalananda

          135k17255422




          135k17255422













          • Thanks, I'm gonna try that right away.

            – Matteo Tassinari
            13 hours ago











          • I tried your solution, however if one of the values in the starting associative array contains any space, the resulting command will break up

            – Matteo Tassinari
            9 hours ago











          • @MatteoTassinari No it would not. If it does, then you have forgotten the double quotes in "--$param=${inhash[$param]}" or in "${list[@]}", or the script that receives the options does something wrong in parsing them.

            – Kusalananda
            8 hours ago













          • I can confirm that I used the quotes as you are showing, these parameters are passed to github.com/megastep/makeself to create an installer which, when executes, invokes a script with the given parametes, perhaps in this passage something goes wrong

            – Matteo Tassinari
            8 hours ago






          • 1





            No, it's not that, I'll try to edit my question to better show my use-case.

            – Matteo Tassinari
            5 hours ago



















          • Thanks, I'm gonna try that right away.

            – Matteo Tassinari
            13 hours ago











          • I tried your solution, however if one of the values in the starting associative array contains any space, the resulting command will break up

            – Matteo Tassinari
            9 hours ago











          • @MatteoTassinari No it would not. If it does, then you have forgotten the double quotes in "--$param=${inhash[$param]}" or in "${list[@]}", or the script that receives the options does something wrong in parsing them.

            – Kusalananda
            8 hours ago













          • I can confirm that I used the quotes as you are showing, these parameters are passed to github.com/megastep/makeself to create an installer which, when executes, invokes a script with the given parametes, perhaps in this passage something goes wrong

            – Matteo Tassinari
            8 hours ago






          • 1





            No, it's not that, I'll try to edit my question to better show my use-case.

            – Matteo Tassinari
            5 hours ago

















          Thanks, I'm gonna try that right away.

          – Matteo Tassinari
          13 hours ago





          Thanks, I'm gonna try that right away.

          – Matteo Tassinari
          13 hours ago













          I tried your solution, however if one of the values in the starting associative array contains any space, the resulting command will break up

          – Matteo Tassinari
          9 hours ago





          I tried your solution, however if one of the values in the starting associative array contains any space, the resulting command will break up

          – Matteo Tassinari
          9 hours ago













          @MatteoTassinari No it would not. If it does, then you have forgotten the double quotes in "--$param=${inhash[$param]}" or in "${list[@]}", or the script that receives the options does something wrong in parsing them.

          – Kusalananda
          8 hours ago







          @MatteoTassinari No it would not. If it does, then you have forgotten the double quotes in "--$param=${inhash[$param]}" or in "${list[@]}", or the script that receives the options does something wrong in parsing them.

          – Kusalananda
          8 hours ago















          I can confirm that I used the quotes as you are showing, these parameters are passed to github.com/megastep/makeself to create an installer which, when executes, invokes a script with the given parametes, perhaps in this passage something goes wrong

          – Matteo Tassinari
          8 hours ago





          I can confirm that I used the quotes as you are showing, these parameters are passed to github.com/megastep/makeself to create an installer which, when executes, invokes a script with the given parametes, perhaps in this passage something goes wrong

          – Matteo Tassinari
          8 hours ago




          1




          1





          No, it's not that, I'll try to edit my question to better show my use-case.

          – Matteo Tassinari
          5 hours ago





          No, it's not that, I'll try to edit my question to better show my use-case.

          – Matteo Tassinari
          5 hours ago


















          draft saved

          draft discarded




















































          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%2f506440%2fpass-associative-array-as-parameter-list-to-script%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

          How did Captain America manage to do this?

          迪纳利

          南乌拉尔铁路局