How to store (and load) a zsh String array to a file?












3















I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.



I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.










share|improve this question





























    3















    I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.



    I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.










    share|improve this question



























      3












      3








      3








      I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.



      I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.










      share|improve this question
















      I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.



      I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.







      zsh array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 9 '18 at 14:27









      Jeff Schaller

      39.3k1054125




      39.3k1054125










      asked Dec 5 '18 at 20:11









      HappyFaceHappyFace

      31811




      31811






















          2 Answers
          2






          active

          oldest

          votes


















          6














          Just do:



          typeset array > file


          To load:



          source file


          (you can also use typeset -p array to also save the attributes of the array variable (exported, unique...)).



          Alternatively:



          print -rl -- ${(qq)array} > file


          To load:



          eval "array=($(<file))"


          For your separator idea:



          print -r -- ${(j:separator:)array} > file


          To load:



          array=("${(@s:separator:)"$(<file)")


          (though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).






          share|improve this answer

































            1














            The two portable (ksh, zsh, bash) solutions AFAICT are:



             typeset -p arr >./file              # save array
            . ./file # read array


            And



            printf '%qn' "${arr[@]}" >./file    # save array
            eval "arr=( $(< ./file) )" # read array


            Note that the first solution will create a local variable if used inside a function in bash.






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


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486230%2fhow-to-store-and-load-a-zsh-string-array-to-a-file%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









              6














              Just do:



              typeset array > file


              To load:



              source file


              (you can also use typeset -p array to also save the attributes of the array variable (exported, unique...)).



              Alternatively:



              print -rl -- ${(qq)array} > file


              To load:



              eval "array=($(<file))"


              For your separator idea:



              print -r -- ${(j:separator:)array} > file


              To load:



              array=("${(@s:separator:)"$(<file)")


              (though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).






              share|improve this answer






























                6














                Just do:



                typeset array > file


                To load:



                source file


                (you can also use typeset -p array to also save the attributes of the array variable (exported, unique...)).



                Alternatively:



                print -rl -- ${(qq)array} > file


                To load:



                eval "array=($(<file))"


                For your separator idea:



                print -r -- ${(j:separator:)array} > file


                To load:



                array=("${(@s:separator:)"$(<file)")


                (though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).






                share|improve this answer




























                  6












                  6








                  6







                  Just do:



                  typeset array > file


                  To load:



                  source file


                  (you can also use typeset -p array to also save the attributes of the array variable (exported, unique...)).



                  Alternatively:



                  print -rl -- ${(qq)array} > file


                  To load:



                  eval "array=($(<file))"


                  For your separator idea:



                  print -r -- ${(j:separator:)array} > file


                  To load:



                  array=("${(@s:separator:)"$(<file)")


                  (though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).






                  share|improve this answer















                  Just do:



                  typeset array > file


                  To load:



                  source file


                  (you can also use typeset -p array to also save the attributes of the array variable (exported, unique...)).



                  Alternatively:



                  print -rl -- ${(qq)array} > file


                  To load:



                  eval "array=($(<file))"


                  For your separator idea:



                  print -r -- ${(j:separator:)array} > file


                  To load:



                  array=("${(@s:separator:)"$(<file)")


                  (though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 5 '18 at 20:49

























                  answered Dec 5 '18 at 20:32









                  Stéphane ChazelasStéphane Chazelas

                  301k55564916




                  301k55564916

























                      1














                      The two portable (ksh, zsh, bash) solutions AFAICT are:



                       typeset -p arr >./file              # save array
                      . ./file # read array


                      And



                      printf '%qn' "${arr[@]}" >./file    # save array
                      eval "arr=( $(< ./file) )" # read array


                      Note that the first solution will create a local variable if used inside a function in bash.






                      share|improve this answer




























                        1














                        The two portable (ksh, zsh, bash) solutions AFAICT are:



                         typeset -p arr >./file              # save array
                        . ./file # read array


                        And



                        printf '%qn' "${arr[@]}" >./file    # save array
                        eval "arr=( $(< ./file) )" # read array


                        Note that the first solution will create a local variable if used inside a function in bash.






                        share|improve this answer


























                          1












                          1








                          1







                          The two portable (ksh, zsh, bash) solutions AFAICT are:



                           typeset -p arr >./file              # save array
                          . ./file # read array


                          And



                          printf '%qn' "${arr[@]}" >./file    # save array
                          eval "arr=( $(< ./file) )" # read array


                          Note that the first solution will create a local variable if used inside a function in bash.






                          share|improve this answer













                          The two portable (ksh, zsh, bash) solutions AFAICT are:



                           typeset -p arr >./file              # save array
                          . ./file # read array


                          And



                          printf '%qn' "${arr[@]}" >./file    # save array
                          eval "arr=( $(< ./file) )" # read array


                          Note that the first solution will create a local variable if used inside a function in bash.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 6 '18 at 9:17









                          IsaacIsaac

                          11.4k11652




                          11.4k11652






























                              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%2f486230%2fhow-to-store-and-load-a-zsh-string-array-to-a-file%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

                              Wiesbaden

                              Marschland

                              Dieringhausen