How to unset range of array in Bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















I'm trying to delete range of array element but it's fail..



My array



root@ubuntu:~/work# echo $a[@]
cocacola.com airtel.com pepsi.com


Print 0-1 array looks ok



root@ubuntu:~/work# echo $a[@]::2
cocacola.com airtel.com


Now I'm trying to delete only these element using :



root@ubuntu:~/work# unset a[@]::2
root@ubuntu:~/work# echo $a[@]


It's delete whole array..



What I'm doing wrong ?



I found other way of deleting range of array but why above things is not working ?



for ((i=0; i<2; i++)); do unset a[$i]; done


EDIT
I had also tried but no luck



unset -v 'a[@]::2'









share|improve this question






























    7















    I'm trying to delete range of array element but it's fail..



    My array



    root@ubuntu:~/work# echo $a[@]
    cocacola.com airtel.com pepsi.com


    Print 0-1 array looks ok



    root@ubuntu:~/work# echo $a[@]::2
    cocacola.com airtel.com


    Now I'm trying to delete only these element using :



    root@ubuntu:~/work# unset a[@]::2
    root@ubuntu:~/work# echo $a[@]


    It's delete whole array..



    What I'm doing wrong ?



    I found other way of deleting range of array but why above things is not working ?



    for ((i=0; i<2; i++)); do unset a[$i]; done


    EDIT
    I had also tried but no luck



    unset -v 'a[@]::2'









    share|improve this question


























      7












      7








      7


      4






      I'm trying to delete range of array element but it's fail..



      My array



      root@ubuntu:~/work# echo $a[@]
      cocacola.com airtel.com pepsi.com


      Print 0-1 array looks ok



      root@ubuntu:~/work# echo $a[@]::2
      cocacola.com airtel.com


      Now I'm trying to delete only these element using :



      root@ubuntu:~/work# unset a[@]::2
      root@ubuntu:~/work# echo $a[@]


      It's delete whole array..



      What I'm doing wrong ?



      I found other way of deleting range of array but why above things is not working ?



      for ((i=0; i<2; i++)); do unset a[$i]; done


      EDIT
      I had also tried but no luck



      unset -v 'a[@]::2'









      share|improve this question
















      I'm trying to delete range of array element but it's fail..



      My array



      root@ubuntu:~/work# echo $a[@]
      cocacola.com airtel.com pepsi.com


      Print 0-1 array looks ok



      root@ubuntu:~/work# echo $a[@]::2
      cocacola.com airtel.com


      Now I'm trying to delete only these element using :



      root@ubuntu:~/work# unset a[@]::2
      root@ubuntu:~/work# echo $a[@]


      It's delete whole array..



      What I'm doing wrong ?



      I found other way of deleting range of array but why above things is not working ?



      for ((i=0; i<2; i++)); do unset a[$i]; done


      EDIT
      I had also tried but no luck



      unset -v 'a[@]::2'






      bash array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 6 '13 at 23:16









      Gilles

      546k12911101624




      546k12911101624










      asked Apr 30 '13 at 16:02









      Rahul PatilRahul Patil

      15.2k186084




      15.2k186084




















          2 Answers
          2






          active

          oldest

          votes


















          12














          One thing to bear in mind is that bash implemented arrays like ksh, that is as associative arrays where keys are limited to positive integers (contrary to other languages like perl or zsh for instance).



          In:



          a[123]=foo a[456]=bar a[789]=baz


          In bash, you've got an associative array with 3 elements, while in perl, you'd have an array with 790 elements (789 with zsh).



          In ksh or bash, $a[@]:0:1 returns the first element of the array in the list of elements sorted numerically by key where the key is greater or equal to 0. So in that case, it returns $a[123], not $a[0].



          unset 'a[123]'


          (remember to quote it, otherwise it would fail if there was a file called a1 or a2 or a3 in the current directory) makes sense, as it removes a particular key in the array.



          unset 'a[@]::2'


          makes less sense though. bash only understands unset a, unset 'a[123]' or unset 'a[*/@]', anything after is ignored, so unset 'a[@]::2' and unset 'a[@]please' do the same: unset the whole array.



          If you want to unset a range of keys, you'd need to loop through the keys:



          To get the list of keys of the array, the syntax is "$!a[@]". Unfortunately, applying a range to that doesn't work with bash nor ksh, so you'd need a temporary array:



          keys=("$!a[@]")
          for i in "$keys[@]::2"; do unset "a[$i]"; done


          Now if you want to consider those arrays like in other languages, you don't want to use unset. Like, if the array is not sparse in the first place and you want to keep it so (that is shift all the elements by 2 instead of unsetting the first two), you can do things like:



          a=("$a[@]:2")


          That is reassign the array with the list of elements you want to keep.



          For comparison, with zsh.



          a=(1..20)
          unset 'a[12,16]'


          would set an empty value to elements 12 to 16. while unset 'a[16,20]' would shrink the array to 15 elements.



          a=(1..20)
          a[12,16]=()


          (still with zsh) would shift elements 17 to 20 by 5 positions so a[12] would contain 17.






          share|improve this answer

























          • Thanks.. you are my master... with help of this , I have solve one problem.. so please have look at this and let me if any improvement in that Bash Code .. unix.stackexchange.com/questions/74001/…

            – Rahul Patil
            May 1 '13 at 6:46


















          1















          1. If your array is continuous/not sparse (all elements from 0..N-1 set)



            You can remove the 2nd element of the array with



            unset 'a[1]'


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for ((i=1; i<=3; i++)); do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for ((i=2; i<$#a[@]; i++)); do unset "a[$i]"; done



          2. General solution (works also for sparse arrays):
            You can remove the 2nd element of the array with



            unset "a[$(echo $!a[@] | cut -d" " -f 2)]"


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-4) ; do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-) ; do unset "a[$i]"; done






          share|improve this answer

























          • I already did this n had updated in question.. but question remain about unset a[@]::2

            – Rahul Patil
            Apr 30 '13 at 16:32











          • That's wrong in the general case because $#a[@] is the number of elements in the array, not the greatest indice in the array (bash arrays, like ksh arrays are sparse, contrary to zsh ones).

            – Stéphane Chazelas
            Apr 30 '13 at 16:43












          • @StephaneChazelas after deleting element why not reset index ?

            – Rahul Patil
            Apr 30 '13 at 16:54











          • @StephaneChazelas after doing some RnD I Found this way to reset array a=( $(echo $a[@]) ) is this fine ?

            – Rahul Patil
            Apr 30 '13 at 17:05






          • 2





            @RahulPatil, if you want to unsparse the array, just write it: a=("$a[@]"), using echo and command substitution would only work in limited corner cases (like when none of the elements are empty or contain spc, tab, NL, backslash, *, ?, [ or start with -).

            – Stéphane Chazelas
            Apr 30 '13 at 19:40











          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%2f74197%2fhow-to-unset-range-of-array-in-bash%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









          12














          One thing to bear in mind is that bash implemented arrays like ksh, that is as associative arrays where keys are limited to positive integers (contrary to other languages like perl or zsh for instance).



          In:



          a[123]=foo a[456]=bar a[789]=baz


          In bash, you've got an associative array with 3 elements, while in perl, you'd have an array with 790 elements (789 with zsh).



          In ksh or bash, $a[@]:0:1 returns the first element of the array in the list of elements sorted numerically by key where the key is greater or equal to 0. So in that case, it returns $a[123], not $a[0].



          unset 'a[123]'


          (remember to quote it, otherwise it would fail if there was a file called a1 or a2 or a3 in the current directory) makes sense, as it removes a particular key in the array.



          unset 'a[@]::2'


          makes less sense though. bash only understands unset a, unset 'a[123]' or unset 'a[*/@]', anything after is ignored, so unset 'a[@]::2' and unset 'a[@]please' do the same: unset the whole array.



          If you want to unset a range of keys, you'd need to loop through the keys:



          To get the list of keys of the array, the syntax is "$!a[@]". Unfortunately, applying a range to that doesn't work with bash nor ksh, so you'd need a temporary array:



          keys=("$!a[@]")
          for i in "$keys[@]::2"; do unset "a[$i]"; done


          Now if you want to consider those arrays like in other languages, you don't want to use unset. Like, if the array is not sparse in the first place and you want to keep it so (that is shift all the elements by 2 instead of unsetting the first two), you can do things like:



          a=("$a[@]:2")


          That is reassign the array with the list of elements you want to keep.



          For comparison, with zsh.



          a=(1..20)
          unset 'a[12,16]'


          would set an empty value to elements 12 to 16. while unset 'a[16,20]' would shrink the array to 15 elements.



          a=(1..20)
          a[12,16]=()


          (still with zsh) would shift elements 17 to 20 by 5 positions so a[12] would contain 17.






          share|improve this answer

























          • Thanks.. you are my master... with help of this , I have solve one problem.. so please have look at this and let me if any improvement in that Bash Code .. unix.stackexchange.com/questions/74001/…

            – Rahul Patil
            May 1 '13 at 6:46















          12














          One thing to bear in mind is that bash implemented arrays like ksh, that is as associative arrays where keys are limited to positive integers (contrary to other languages like perl or zsh for instance).



          In:



          a[123]=foo a[456]=bar a[789]=baz


          In bash, you've got an associative array with 3 elements, while in perl, you'd have an array with 790 elements (789 with zsh).



          In ksh or bash, $a[@]:0:1 returns the first element of the array in the list of elements sorted numerically by key where the key is greater or equal to 0. So in that case, it returns $a[123], not $a[0].



          unset 'a[123]'


          (remember to quote it, otherwise it would fail if there was a file called a1 or a2 or a3 in the current directory) makes sense, as it removes a particular key in the array.



          unset 'a[@]::2'


          makes less sense though. bash only understands unset a, unset 'a[123]' or unset 'a[*/@]', anything after is ignored, so unset 'a[@]::2' and unset 'a[@]please' do the same: unset the whole array.



          If you want to unset a range of keys, you'd need to loop through the keys:



          To get the list of keys of the array, the syntax is "$!a[@]". Unfortunately, applying a range to that doesn't work with bash nor ksh, so you'd need a temporary array:



          keys=("$!a[@]")
          for i in "$keys[@]::2"; do unset "a[$i]"; done


          Now if you want to consider those arrays like in other languages, you don't want to use unset. Like, if the array is not sparse in the first place and you want to keep it so (that is shift all the elements by 2 instead of unsetting the first two), you can do things like:



          a=("$a[@]:2")


          That is reassign the array with the list of elements you want to keep.



          For comparison, with zsh.



          a=(1..20)
          unset 'a[12,16]'


          would set an empty value to elements 12 to 16. while unset 'a[16,20]' would shrink the array to 15 elements.



          a=(1..20)
          a[12,16]=()


          (still with zsh) would shift elements 17 to 20 by 5 positions so a[12] would contain 17.






          share|improve this answer

























          • Thanks.. you are my master... with help of this , I have solve one problem.. so please have look at this and let me if any improvement in that Bash Code .. unix.stackexchange.com/questions/74001/…

            – Rahul Patil
            May 1 '13 at 6:46













          12












          12








          12







          One thing to bear in mind is that bash implemented arrays like ksh, that is as associative arrays where keys are limited to positive integers (contrary to other languages like perl or zsh for instance).



          In:



          a[123]=foo a[456]=bar a[789]=baz


          In bash, you've got an associative array with 3 elements, while in perl, you'd have an array with 790 elements (789 with zsh).



          In ksh or bash, $a[@]:0:1 returns the first element of the array in the list of elements sorted numerically by key where the key is greater or equal to 0. So in that case, it returns $a[123], not $a[0].



          unset 'a[123]'


          (remember to quote it, otherwise it would fail if there was a file called a1 or a2 or a3 in the current directory) makes sense, as it removes a particular key in the array.



          unset 'a[@]::2'


          makes less sense though. bash only understands unset a, unset 'a[123]' or unset 'a[*/@]', anything after is ignored, so unset 'a[@]::2' and unset 'a[@]please' do the same: unset the whole array.



          If you want to unset a range of keys, you'd need to loop through the keys:



          To get the list of keys of the array, the syntax is "$!a[@]". Unfortunately, applying a range to that doesn't work with bash nor ksh, so you'd need a temporary array:



          keys=("$!a[@]")
          for i in "$keys[@]::2"; do unset "a[$i]"; done


          Now if you want to consider those arrays like in other languages, you don't want to use unset. Like, if the array is not sparse in the first place and you want to keep it so (that is shift all the elements by 2 instead of unsetting the first two), you can do things like:



          a=("$a[@]:2")


          That is reassign the array with the list of elements you want to keep.



          For comparison, with zsh.



          a=(1..20)
          unset 'a[12,16]'


          would set an empty value to elements 12 to 16. while unset 'a[16,20]' would shrink the array to 15 elements.



          a=(1..20)
          a[12,16]=()


          (still with zsh) would shift elements 17 to 20 by 5 positions so a[12] would contain 17.






          share|improve this answer















          One thing to bear in mind is that bash implemented arrays like ksh, that is as associative arrays where keys are limited to positive integers (contrary to other languages like perl or zsh for instance).



          In:



          a[123]=foo a[456]=bar a[789]=baz


          In bash, you've got an associative array with 3 elements, while in perl, you'd have an array with 790 elements (789 with zsh).



          In ksh or bash, $a[@]:0:1 returns the first element of the array in the list of elements sorted numerically by key where the key is greater or equal to 0. So in that case, it returns $a[123], not $a[0].



          unset 'a[123]'


          (remember to quote it, otherwise it would fail if there was a file called a1 or a2 or a3 in the current directory) makes sense, as it removes a particular key in the array.



          unset 'a[@]::2'


          makes less sense though. bash only understands unset a, unset 'a[123]' or unset 'a[*/@]', anything after is ignored, so unset 'a[@]::2' and unset 'a[@]please' do the same: unset the whole array.



          If you want to unset a range of keys, you'd need to loop through the keys:



          To get the list of keys of the array, the syntax is "$!a[@]". Unfortunately, applying a range to that doesn't work with bash nor ksh, so you'd need a temporary array:



          keys=("$!a[@]")
          for i in "$keys[@]::2"; do unset "a[$i]"; done


          Now if you want to consider those arrays like in other languages, you don't want to use unset. Like, if the array is not sparse in the first place and you want to keep it so (that is shift all the elements by 2 instead of unsetting the first two), you can do things like:



          a=("$a[@]:2")


          That is reassign the array with the list of elements you want to keep.



          For comparison, with zsh.



          a=(1..20)
          unset 'a[12,16]'


          would set an empty value to elements 12 to 16. while unset 'a[16,20]' would shrink the array to 15 elements.



          a=(1..20)
          a[12,16]=()


          (still with zsh) would shift elements 17 to 20 by 5 positions so a[12] would contain 17.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 12:38

























          answered Apr 30 '13 at 17:04









          Stéphane ChazelasStéphane Chazelas

          313k57592948




          313k57592948












          • Thanks.. you are my master... with help of this , I have solve one problem.. so please have look at this and let me if any improvement in that Bash Code .. unix.stackexchange.com/questions/74001/…

            – Rahul Patil
            May 1 '13 at 6:46

















          • Thanks.. you are my master... with help of this , I have solve one problem.. so please have look at this and let me if any improvement in that Bash Code .. unix.stackexchange.com/questions/74001/…

            – Rahul Patil
            May 1 '13 at 6:46
















          Thanks.. you are my master... with help of this , I have solve one problem.. so please have look at this and let me if any improvement in that Bash Code .. unix.stackexchange.com/questions/74001/…

          – Rahul Patil
          May 1 '13 at 6:46





          Thanks.. you are my master... with help of this , I have solve one problem.. so please have look at this and let me if any improvement in that Bash Code .. unix.stackexchange.com/questions/74001/…

          – Rahul Patil
          May 1 '13 at 6:46













          1















          1. If your array is continuous/not sparse (all elements from 0..N-1 set)



            You can remove the 2nd element of the array with



            unset 'a[1]'


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for ((i=1; i<=3; i++)); do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for ((i=2; i<$#a[@]; i++)); do unset "a[$i]"; done



          2. General solution (works also for sparse arrays):
            You can remove the 2nd element of the array with



            unset "a[$(echo $!a[@] | cut -d" " -f 2)]"


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-4) ; do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-) ; do unset "a[$i]"; done






          share|improve this answer

























          • I already did this n had updated in question.. but question remain about unset a[@]::2

            – Rahul Patil
            Apr 30 '13 at 16:32











          • That's wrong in the general case because $#a[@] is the number of elements in the array, not the greatest indice in the array (bash arrays, like ksh arrays are sparse, contrary to zsh ones).

            – Stéphane Chazelas
            Apr 30 '13 at 16:43












          • @StephaneChazelas after deleting element why not reset index ?

            – Rahul Patil
            Apr 30 '13 at 16:54











          • @StephaneChazelas after doing some RnD I Found this way to reset array a=( $(echo $a[@]) ) is this fine ?

            – Rahul Patil
            Apr 30 '13 at 17:05






          • 2





            @RahulPatil, if you want to unsparse the array, just write it: a=("$a[@]"), using echo and command substitution would only work in limited corner cases (like when none of the elements are empty or contain spc, tab, NL, backslash, *, ?, [ or start with -).

            – Stéphane Chazelas
            Apr 30 '13 at 19:40















          1















          1. If your array is continuous/not sparse (all elements from 0..N-1 set)



            You can remove the 2nd element of the array with



            unset 'a[1]'


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for ((i=1; i<=3; i++)); do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for ((i=2; i<$#a[@]; i++)); do unset "a[$i]"; done



          2. General solution (works also for sparse arrays):
            You can remove the 2nd element of the array with



            unset "a[$(echo $!a[@] | cut -d" " -f 2)]"


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-4) ; do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-) ; do unset "a[$i]"; done






          share|improve this answer

























          • I already did this n had updated in question.. but question remain about unset a[@]::2

            – Rahul Patil
            Apr 30 '13 at 16:32











          • That's wrong in the general case because $#a[@] is the number of elements in the array, not the greatest indice in the array (bash arrays, like ksh arrays are sparse, contrary to zsh ones).

            – Stéphane Chazelas
            Apr 30 '13 at 16:43












          • @StephaneChazelas after deleting element why not reset index ?

            – Rahul Patil
            Apr 30 '13 at 16:54











          • @StephaneChazelas after doing some RnD I Found this way to reset array a=( $(echo $a[@]) ) is this fine ?

            – Rahul Patil
            Apr 30 '13 at 17:05






          • 2





            @RahulPatil, if you want to unsparse the array, just write it: a=("$a[@]"), using echo and command substitution would only work in limited corner cases (like when none of the elements are empty or contain spc, tab, NL, backslash, *, ?, [ or start with -).

            – Stéphane Chazelas
            Apr 30 '13 at 19:40













          1












          1








          1








          1. If your array is continuous/not sparse (all elements from 0..N-1 set)



            You can remove the 2nd element of the array with



            unset 'a[1]'


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for ((i=1; i<=3; i++)); do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for ((i=2; i<$#a[@]; i++)); do unset "a[$i]"; done



          2. General solution (works also for sparse arrays):
            You can remove the 2nd element of the array with



            unset "a[$(echo $!a[@] | cut -d" " -f 2)]"


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-4) ; do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-) ; do unset "a[$i]"; done






          share|improve this answer
















          1. If your array is continuous/not sparse (all elements from 0..N-1 set)



            You can remove the 2nd element of the array with



            unset 'a[1]'


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for ((i=1; i<=3; i++)); do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for ((i=2; i<$#a[@]; i++)); do unset "a[$i]"; done



          2. General solution (works also for sparse arrays):
            You can remove the 2nd element of the array with



            unset "a[$(echo $!a[@] | cut -d" " -f 2)]"


            To remove the 2nd, 3rd and 4th element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-4) ; do unset "a[$i]"; done


            To delete all but the 1st and 2nd element, you can use e.g.



            for $(echo $!a[@] | cut -d" " -f 2-) ; do unset "a[$i]"; done







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 30 '13 at 17:05

























          answered Apr 30 '13 at 16:20









          jofeljofel

          20.8k34980




          20.8k34980












          • I already did this n had updated in question.. but question remain about unset a[@]::2

            – Rahul Patil
            Apr 30 '13 at 16:32











          • That's wrong in the general case because $#a[@] is the number of elements in the array, not the greatest indice in the array (bash arrays, like ksh arrays are sparse, contrary to zsh ones).

            – Stéphane Chazelas
            Apr 30 '13 at 16:43












          • @StephaneChazelas after deleting element why not reset index ?

            – Rahul Patil
            Apr 30 '13 at 16:54











          • @StephaneChazelas after doing some RnD I Found this way to reset array a=( $(echo $a[@]) ) is this fine ?

            – Rahul Patil
            Apr 30 '13 at 17:05






          • 2





            @RahulPatil, if you want to unsparse the array, just write it: a=("$a[@]"), using echo and command substitution would only work in limited corner cases (like when none of the elements are empty or contain spc, tab, NL, backslash, *, ?, [ or start with -).

            – Stéphane Chazelas
            Apr 30 '13 at 19:40

















          • I already did this n had updated in question.. but question remain about unset a[@]::2

            – Rahul Patil
            Apr 30 '13 at 16:32











          • That's wrong in the general case because $#a[@] is the number of elements in the array, not the greatest indice in the array (bash arrays, like ksh arrays are sparse, contrary to zsh ones).

            – Stéphane Chazelas
            Apr 30 '13 at 16:43












          • @StephaneChazelas after deleting element why not reset index ?

            – Rahul Patil
            Apr 30 '13 at 16:54











          • @StephaneChazelas after doing some RnD I Found this way to reset array a=( $(echo $a[@]) ) is this fine ?

            – Rahul Patil
            Apr 30 '13 at 17:05






          • 2





            @RahulPatil, if you want to unsparse the array, just write it: a=("$a[@]"), using echo and command substitution would only work in limited corner cases (like when none of the elements are empty or contain spc, tab, NL, backslash, *, ?, [ or start with -).

            – Stéphane Chazelas
            Apr 30 '13 at 19:40
















          I already did this n had updated in question.. but question remain about unset a[@]::2

          – Rahul Patil
          Apr 30 '13 at 16:32





          I already did this n had updated in question.. but question remain about unset a[@]::2

          – Rahul Patil
          Apr 30 '13 at 16:32













          That's wrong in the general case because $#a[@] is the number of elements in the array, not the greatest indice in the array (bash arrays, like ksh arrays are sparse, contrary to zsh ones).

          – Stéphane Chazelas
          Apr 30 '13 at 16:43






          That's wrong in the general case because $#a[@] is the number of elements in the array, not the greatest indice in the array (bash arrays, like ksh arrays are sparse, contrary to zsh ones).

          – Stéphane Chazelas
          Apr 30 '13 at 16:43














          @StephaneChazelas after deleting element why not reset index ?

          – Rahul Patil
          Apr 30 '13 at 16:54





          @StephaneChazelas after deleting element why not reset index ?

          – Rahul Patil
          Apr 30 '13 at 16:54













          @StephaneChazelas after doing some RnD I Found this way to reset array a=( $(echo $a[@]) ) is this fine ?

          – Rahul Patil
          Apr 30 '13 at 17:05





          @StephaneChazelas after doing some RnD I Found this way to reset array a=( $(echo $a[@]) ) is this fine ?

          – Rahul Patil
          Apr 30 '13 at 17:05




          2




          2





          @RahulPatil, if you want to unsparse the array, just write it: a=("$a[@]"), using echo and command substitution would only work in limited corner cases (like when none of the elements are empty or contain spc, tab, NL, backslash, *, ?, [ or start with -).

          – Stéphane Chazelas
          Apr 30 '13 at 19:40





          @RahulPatil, if you want to unsparse the array, just write it: a=("$a[@]"), using echo and command substitution would only work in limited corner cases (like when none of the elements are empty or contain spc, tab, NL, backslash, *, ?, [ or start with -).

          – Stéphane Chazelas
          Apr 30 '13 at 19:40

















          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%2f74197%2fhow-to-unset-range-of-array-in-bash%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 to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay