How to copy an array to a new array with dynamic name?

Multi tool use
Multi tool use












0















I have a complex data structure in Bash like this:



Test1_Name="test 1"
Test1_Files=(
file01.txt
file02.txt
)
Test2_Name="test 2"
Test2_Files=(
file11.txt
file12.txt
)
TestNames="Test1 Test2"


In my improved script, I would like read the files from disk. Each test resides in a directory.



So I have a Bash snippet reading directories and reading all the file names. The result is present in an array: $Files[*].



How can I copy that array to an array prefixed with the test's name. Let's assume $TestName holds the test's name.



I know how to create a dynamically named array:



for $Name in $TestNames; do
declare -a "${TestName}_Files"
done


Will create e.g. Test1_Files and Test2_Files. The variables are of type array, because I used -a in declare.



But how can I copy $Files[*] to "${TestName}_Files" in such a loop?



I tried this:



declare -a "${TestName}_Files"=${Files[*]}


But it gives an error that =file01.txt is not a valid identifier.










share|improve this question



























    0















    I have a complex data structure in Bash like this:



    Test1_Name="test 1"
    Test1_Files=(
    file01.txt
    file02.txt
    )
    Test2_Name="test 2"
    Test2_Files=(
    file11.txt
    file12.txt
    )
    TestNames="Test1 Test2"


    In my improved script, I would like read the files from disk. Each test resides in a directory.



    So I have a Bash snippet reading directories and reading all the file names. The result is present in an array: $Files[*].



    How can I copy that array to an array prefixed with the test's name. Let's assume $TestName holds the test's name.



    I know how to create a dynamically named array:



    for $Name in $TestNames; do
    declare -a "${TestName}_Files"
    done


    Will create e.g. Test1_Files and Test2_Files. The variables are of type array, because I used -a in declare.



    But how can I copy $Files[*] to "${TestName}_Files" in such a loop?



    I tried this:



    declare -a "${TestName}_Files"=${Files[*]}


    But it gives an error that =file01.txt is not a valid identifier.










    share|improve this question

























      0












      0








      0








      I have a complex data structure in Bash like this:



      Test1_Name="test 1"
      Test1_Files=(
      file01.txt
      file02.txt
      )
      Test2_Name="test 2"
      Test2_Files=(
      file11.txt
      file12.txt
      )
      TestNames="Test1 Test2"


      In my improved script, I would like read the files from disk. Each test resides in a directory.



      So I have a Bash snippet reading directories and reading all the file names. The result is present in an array: $Files[*].



      How can I copy that array to an array prefixed with the test's name. Let's assume $TestName holds the test's name.



      I know how to create a dynamically named array:



      for $Name in $TestNames; do
      declare -a "${TestName}_Files"
      done


      Will create e.g. Test1_Files and Test2_Files. The variables are of type array, because I used -a in declare.



      But how can I copy $Files[*] to "${TestName}_Files" in such a loop?



      I tried this:



      declare -a "${TestName}_Files"=${Files[*]}


      But it gives an error that =file01.txt is not a valid identifier.










      share|improve this question














      I have a complex data structure in Bash like this:



      Test1_Name="test 1"
      Test1_Files=(
      file01.txt
      file02.txt
      )
      Test2_Name="test 2"
      Test2_Files=(
      file11.txt
      file12.txt
      )
      TestNames="Test1 Test2"


      In my improved script, I would like read the files from disk. Each test resides in a directory.



      So I have a Bash snippet reading directories and reading all the file names. The result is present in an array: $Files[*].



      How can I copy that array to an array prefixed with the test's name. Let's assume $TestName holds the test's name.



      I know how to create a dynamically named array:



      for $Name in $TestNames; do
      declare -a "${TestName}_Files"
      done


      Will create e.g. Test1_Files and Test2_Files. The variables are of type array, because I used -a in declare.



      But how can I copy $Files[*] to "${TestName}_Files" in such a loop?



      I tried this:



      declare -a "${TestName}_Files"=${Files[*]}


      But it gives an error that =file01.txt is not a valid identifier.







      arrays bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 12:05









      PaebbelsPaebbels

      7,42983577




      7,42983577
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You'll want to use newarray=( "${oldarray[@]}" ) to keep the array elements intact. ${oldarray[*]} will involve word splitting, which will break at least with elements containing white space.



          However, the obvious declare -a "$name"=("${oldarray[@]}") doesn't work, with the parenthesis quoted or not. One way to do it seems to be to use a name ref. This would copy the contents of old to new, where the name of new can be given dynamically:



          #!/bin/bash
          old=("abc" "white space")
          name=new

          declare -a "$name" # declare the new array, make 'ref' point to it
          declare -n ref="$name"

          ref=( "${old[@]}" ) # copy

          #unset -n ref # unset the nameref, if required
          declare -p "$name" # verify results





          share|improve this answer


























          • What is the purpose of declare -p "$name"?

            – Paebbels
            Dec 31 '18 at 12:29













          • @Paebbels, just to print out the new copy to verify the result is correct

            – ilkkachu
            Dec 31 '18 at 13:35











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53987310%2fhow-to-copy-an-array-to-a-new-array-with-dynamic-name%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









          2














          You'll want to use newarray=( "${oldarray[@]}" ) to keep the array elements intact. ${oldarray[*]} will involve word splitting, which will break at least with elements containing white space.



          However, the obvious declare -a "$name"=("${oldarray[@]}") doesn't work, with the parenthesis quoted or not. One way to do it seems to be to use a name ref. This would copy the contents of old to new, where the name of new can be given dynamically:



          #!/bin/bash
          old=("abc" "white space")
          name=new

          declare -a "$name" # declare the new array, make 'ref' point to it
          declare -n ref="$name"

          ref=( "${old[@]}" ) # copy

          #unset -n ref # unset the nameref, if required
          declare -p "$name" # verify results





          share|improve this answer


























          • What is the purpose of declare -p "$name"?

            – Paebbels
            Dec 31 '18 at 12:29













          • @Paebbels, just to print out the new copy to verify the result is correct

            – ilkkachu
            Dec 31 '18 at 13:35
















          2














          You'll want to use newarray=( "${oldarray[@]}" ) to keep the array elements intact. ${oldarray[*]} will involve word splitting, which will break at least with elements containing white space.



          However, the obvious declare -a "$name"=("${oldarray[@]}") doesn't work, with the parenthesis quoted or not. One way to do it seems to be to use a name ref. This would copy the contents of old to new, where the name of new can be given dynamically:



          #!/bin/bash
          old=("abc" "white space")
          name=new

          declare -a "$name" # declare the new array, make 'ref' point to it
          declare -n ref="$name"

          ref=( "${old[@]}" ) # copy

          #unset -n ref # unset the nameref, if required
          declare -p "$name" # verify results





          share|improve this answer


























          • What is the purpose of declare -p "$name"?

            – Paebbels
            Dec 31 '18 at 12:29













          • @Paebbels, just to print out the new copy to verify the result is correct

            – ilkkachu
            Dec 31 '18 at 13:35














          2












          2








          2







          You'll want to use newarray=( "${oldarray[@]}" ) to keep the array elements intact. ${oldarray[*]} will involve word splitting, which will break at least with elements containing white space.



          However, the obvious declare -a "$name"=("${oldarray[@]}") doesn't work, with the parenthesis quoted or not. One way to do it seems to be to use a name ref. This would copy the contents of old to new, where the name of new can be given dynamically:



          #!/bin/bash
          old=("abc" "white space")
          name=new

          declare -a "$name" # declare the new array, make 'ref' point to it
          declare -n ref="$name"

          ref=( "${old[@]}" ) # copy

          #unset -n ref # unset the nameref, if required
          declare -p "$name" # verify results





          share|improve this answer















          You'll want to use newarray=( "${oldarray[@]}" ) to keep the array elements intact. ${oldarray[*]} will involve word splitting, which will break at least with elements containing white space.



          However, the obvious declare -a "$name"=("${oldarray[@]}") doesn't work, with the parenthesis quoted or not. One way to do it seems to be to use a name ref. This would copy the contents of old to new, where the name of new can be given dynamically:



          #!/bin/bash
          old=("abc" "white space")
          name=new

          declare -a "$name" # declare the new array, make 'ref' point to it
          declare -n ref="$name"

          ref=( "${old[@]}" ) # copy

          #unset -n ref # unset the nameref, if required
          declare -p "$name" # verify results






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 31 '18 at 13:34

























          answered Dec 31 '18 at 12:16









          ilkkachuilkkachu

          3,509318




          3,509318













          • What is the purpose of declare -p "$name"?

            – Paebbels
            Dec 31 '18 at 12:29













          • @Paebbels, just to print out the new copy to verify the result is correct

            – ilkkachu
            Dec 31 '18 at 13:35



















          • What is the purpose of declare -p "$name"?

            – Paebbels
            Dec 31 '18 at 12:29













          • @Paebbels, just to print out the new copy to verify the result is correct

            – ilkkachu
            Dec 31 '18 at 13:35

















          What is the purpose of declare -p "$name"?

          – Paebbels
          Dec 31 '18 at 12:29







          What is the purpose of declare -p "$name"?

          – Paebbels
          Dec 31 '18 at 12:29















          @Paebbels, just to print out the new copy to verify the result is correct

          – ilkkachu
          Dec 31 '18 at 13:35





          @Paebbels, just to print out the new copy to verify the result is correct

          – ilkkachu
          Dec 31 '18 at 13:35




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53987310%2fhow-to-copy-an-array-to-a-new-array-with-dynamic-name%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







          8kI rLfU3r0eIH ac0M01Qk4u,wIPypmrvnlg8iFGjEzXAJIPyNo42eeuXaxqlTDtrJH,E0PZyBkYmog3Jjm
          JGaXzqBYB 5blCA

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas