bash + how to shift to the next string in list

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











up vote
1
down vote

favorite












The following list represented the tokens between sdb and sd$MAX (while MAX range could be c-z)



For example:



MAX=z
list=$(eval echo sdb..$MAX)
echo $list


strings are: ( example )



sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz


my question how to shift the list each loop to the next string



example:



for i in disk1 disk2 disk3 disk4 ..................
do
echo $i <...syntax...>
done


expected output



disk1 sdb
disk2 sdc
disk3 sdd
disk4 sde
.
.
.






share|improve this question






















  • Are those disk names just numbers like that, or can they be arbitrary strings? i.e. why take them off a list if you can generate them by iterating over the number?
    – ilkkachu
    Jan 15 at 20:45














up vote
1
down vote

favorite












The following list represented the tokens between sdb and sd$MAX (while MAX range could be c-z)



For example:



MAX=z
list=$(eval echo sdb..$MAX)
echo $list


strings are: ( example )



sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz


my question how to shift the list each loop to the next string



example:



for i in disk1 disk2 disk3 disk4 ..................
do
echo $i <...syntax...>
done


expected output



disk1 sdb
disk2 sdc
disk3 sdd
disk4 sde
.
.
.






share|improve this question






















  • Are those disk names just numbers like that, or can they be arbitrary strings? i.e. why take them off a list if you can generate them by iterating over the number?
    – ilkkachu
    Jan 15 at 20:45












up vote
1
down vote

favorite









up vote
1
down vote

favorite











The following list represented the tokens between sdb and sd$MAX (while MAX range could be c-z)



For example:



MAX=z
list=$(eval echo sdb..$MAX)
echo $list


strings are: ( example )



sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz


my question how to shift the list each loop to the next string



example:



for i in disk1 disk2 disk3 disk4 ..................
do
echo $i <...syntax...>
done


expected output



disk1 sdb
disk2 sdc
disk3 sdd
disk4 sde
.
.
.






share|improve this question














The following list represented the tokens between sdb and sd$MAX (while MAX range could be c-z)



For example:



MAX=z
list=$(eval echo sdb..$MAX)
echo $list


strings are: ( example )



sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz


my question how to shift the list each loop to the next string



example:



for i in disk1 disk2 disk3 disk4 ..................
do
echo $i <...syntax...>
done


expected output



disk1 sdb
disk2 sdc
disk3 sdd
disk4 sde
.
.
.








share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 20:39









ilkkachu

49.8k674137




49.8k674137










asked Jan 15 at 18:07









yael

2,0091145




2,0091145











  • Are those disk names just numbers like that, or can they be arbitrary strings? i.e. why take them off a list if you can generate them by iterating over the number?
    – ilkkachu
    Jan 15 at 20:45
















  • Are those disk names just numbers like that, or can they be arbitrary strings? i.e. why take them off a list if you can generate them by iterating over the number?
    – ilkkachu
    Jan 15 at 20:45















Are those disk names just numbers like that, or can they be arbitrary strings? i.e. why take them off a list if you can generate them by iterating over the number?
– ilkkachu
Jan 15 at 20:45




Are those disk names just numbers like that, or can they be arbitrary strings? i.e. why take them off a list if you can generate them by iterating over the number?
– ilkkachu
Jan 15 at 20:45










4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










It is better to use array variables:



#!/bin/bash

MAX=d
eval list=( $(echo sdb..$MAX))
eval disks=( $(echo disk1..$#list[@]) )

for((i=0;i<=$#list[@];i++));do
echo "$disks[i] $list[i]"
done


On execution:



$ ./script
disk1 sdb
disk2 sdc
disk3 sdd


Adapt as required.






share|improve this answer



























    up vote
    3
    down vote













    You could use indirection to loop over the array's indices rather than its values directly e.g.



    Given



    $ list=(sda..f)
    $ echo "$list[@]"
    sda sdb sdc sdd sde sdf


    then



    $ for i in "$!list[@]"; do printf '%s %sn' "disk$i" "$list[$i]"; done
    disk0 sda
    disk1 sdb
    disk2 sdc
    disk3 sdd
    disk4 sde
    disk5 sdf



    If you really want to do it your way, then you will need to extract a usable integer index from the elements of the strings that you're looping over e.g. by removing the leading string disk using parameter substitution:



    for d in disk1 disk2 disk3 disk4; do printf '%s %sn' "$d" "$list[$d##disk]"; done
    disk1 sdb
    disk2 sdc
    disk3 sdd
    disk4 sde





    share|improve this answer






















    • I not want to use the $!list[@] in the for loop
      – yael
      Jan 15 at 19:17










    • for loop is only for the disks not for the sdX strings
      – yael
      Jan 15 at 19:17










    • @yael <shrug> OK - please see update for an alternative
      – steeldriver
      Jan 15 at 19:25










    • why not use - counter=0 ; list=(sda..f) ; and in the loop --> echo "$list[counter++]"
      – yael
      Jan 15 at 19:44










    • it much more simple
      – yael
      Jan 15 at 19:44

















    up vote
    0
    down vote













    You may build a counter for disk column to feet the list & replace strings for use as export



    -bash-4.4$ MAX=z
    -bash-4.4$ list=$(eval echo sdb..$MAX)
    -bash-4.4$ echo $list
    sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz
    -bash-4.4$ disk=1 ; for i in $(echo $list) ; do echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1)) ; done
    disk1 sdb
    disk2 sdc
    disk3 sdd
    disk4 sde
    disk5 sdf
    disk6 sdg
    disk7 sdh
    disk8 sdi
    disk9 sdj
    disk10 sdk
    disk11 sdl
    disk12 sdm
    disk13 sdn
    disk14 sdo
    disk15 sdp
    disk16 sdq
    disk17 sdr
    disk18 sds
    disk19 sdt
    disk20 sdu
    disk21 sdv
    disk22 sdw
    disk23 sdx
    disk24 sdy
    disk25 sdz
    -bash-4.4$


    be careful here I use a define variable disk to count & a t (tabulation) as separator; you may adapt to feet your needs if you prefer a space or something else.



    if you wanna get this output in a file add a redirection of output
    & of course in a script you may want to use code as this (without ; )



    disk=1 
    for i in $(echo $list)
    do
    echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1))
    done > file


    this is the easy way to understand it.
    A better method is to use the bash substitution instead of sed
    see bash method $!list[@] & either printf command or $parameter/pattern/string substituion advanced functions ...






    share|improve this answer



























      up vote
      0
      down vote













      Bash has two simple and straightforward built-in features that
      together will solve your problem: set and shift.



      set $list will assign each element of $list to one of bash's
      positional parameters.



      shift renames all positional parameter $n+1 to $n, effectively
      deleting $1 and "shifting" all positional parameters forward.



      So, in your example, your statement echo $i <...syntax...> would be
      echo $i " " $1; shift.



      BTW, personally, I would use bash's printf instead of echo (See
      elsewhere for trouble echo can give you): printf "%s %sn" $i $1; shift






      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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        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%2f417307%2fbash-how-to-shift-to-the-next-string-in-list%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote



        accepted










        It is better to use array variables:



        #!/bin/bash

        MAX=d
        eval list=( $(echo sdb..$MAX))
        eval disks=( $(echo disk1..$#list[@]) )

        for((i=0;i<=$#list[@];i++));do
        echo "$disks[i] $list[i]"
        done


        On execution:



        $ ./script
        disk1 sdb
        disk2 sdc
        disk3 sdd


        Adapt as required.






        share|improve this answer
























          up vote
          0
          down vote



          accepted










          It is better to use array variables:



          #!/bin/bash

          MAX=d
          eval list=( $(echo sdb..$MAX))
          eval disks=( $(echo disk1..$#list[@]) )

          for((i=0;i<=$#list[@];i++));do
          echo "$disks[i] $list[i]"
          done


          On execution:



          $ ./script
          disk1 sdb
          disk2 sdc
          disk3 sdd


          Adapt as required.






          share|improve this answer






















            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            It is better to use array variables:



            #!/bin/bash

            MAX=d
            eval list=( $(echo sdb..$MAX))
            eval disks=( $(echo disk1..$#list[@]) )

            for((i=0;i<=$#list[@];i++));do
            echo "$disks[i] $list[i]"
            done


            On execution:



            $ ./script
            disk1 sdb
            disk2 sdc
            disk3 sdd


            Adapt as required.






            share|improve this answer












            It is better to use array variables:



            #!/bin/bash

            MAX=d
            eval list=( $(echo sdb..$MAX))
            eval disks=( $(echo disk1..$#list[@]) )

            for((i=0;i<=$#list[@];i++));do
            echo "$disks[i] $list[i]"
            done


            On execution:



            $ ./script
            disk1 sdb
            disk2 sdc
            disk3 sdd


            Adapt as required.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 15 at 20:31









            Isaac

            6,7611834




            6,7611834






















                up vote
                3
                down vote













                You could use indirection to loop over the array's indices rather than its values directly e.g.



                Given



                $ list=(sda..f)
                $ echo "$list[@]"
                sda sdb sdc sdd sde sdf


                then



                $ for i in "$!list[@]"; do printf '%s %sn' "disk$i" "$list[$i]"; done
                disk0 sda
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde
                disk5 sdf



                If you really want to do it your way, then you will need to extract a usable integer index from the elements of the strings that you're looping over e.g. by removing the leading string disk using parameter substitution:



                for d in disk1 disk2 disk3 disk4; do printf '%s %sn' "$d" "$list[$d##disk]"; done
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde





                share|improve this answer






















                • I not want to use the $!list[@] in the for loop
                  – yael
                  Jan 15 at 19:17










                • for loop is only for the disks not for the sdX strings
                  – yael
                  Jan 15 at 19:17










                • @yael <shrug> OK - please see update for an alternative
                  – steeldriver
                  Jan 15 at 19:25










                • why not use - counter=0 ; list=(sda..f) ; and in the loop --> echo "$list[counter++]"
                  – yael
                  Jan 15 at 19:44










                • it much more simple
                  – yael
                  Jan 15 at 19:44














                up vote
                3
                down vote













                You could use indirection to loop over the array's indices rather than its values directly e.g.



                Given



                $ list=(sda..f)
                $ echo "$list[@]"
                sda sdb sdc sdd sde sdf


                then



                $ for i in "$!list[@]"; do printf '%s %sn' "disk$i" "$list[$i]"; done
                disk0 sda
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde
                disk5 sdf



                If you really want to do it your way, then you will need to extract a usable integer index from the elements of the strings that you're looping over e.g. by removing the leading string disk using parameter substitution:



                for d in disk1 disk2 disk3 disk4; do printf '%s %sn' "$d" "$list[$d##disk]"; done
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde





                share|improve this answer






















                • I not want to use the $!list[@] in the for loop
                  – yael
                  Jan 15 at 19:17










                • for loop is only for the disks not for the sdX strings
                  – yael
                  Jan 15 at 19:17










                • @yael <shrug> OK - please see update for an alternative
                  – steeldriver
                  Jan 15 at 19:25










                • why not use - counter=0 ; list=(sda..f) ; and in the loop --> echo "$list[counter++]"
                  – yael
                  Jan 15 at 19:44










                • it much more simple
                  – yael
                  Jan 15 at 19:44












                up vote
                3
                down vote










                up vote
                3
                down vote









                You could use indirection to loop over the array's indices rather than its values directly e.g.



                Given



                $ list=(sda..f)
                $ echo "$list[@]"
                sda sdb sdc sdd sde sdf


                then



                $ for i in "$!list[@]"; do printf '%s %sn' "disk$i" "$list[$i]"; done
                disk0 sda
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde
                disk5 sdf



                If you really want to do it your way, then you will need to extract a usable integer index from the elements of the strings that you're looping over e.g. by removing the leading string disk using parameter substitution:



                for d in disk1 disk2 disk3 disk4; do printf '%s %sn' "$d" "$list[$d##disk]"; done
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde





                share|improve this answer














                You could use indirection to loop over the array's indices rather than its values directly e.g.



                Given



                $ list=(sda..f)
                $ echo "$list[@]"
                sda sdb sdc sdd sde sdf


                then



                $ for i in "$!list[@]"; do printf '%s %sn' "disk$i" "$list[$i]"; done
                disk0 sda
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde
                disk5 sdf



                If you really want to do it your way, then you will need to extract a usable integer index from the elements of the strings that you're looping over e.g. by removing the leading string disk using parameter substitution:



                for d in disk1 disk2 disk3 disk4; do printf '%s %sn' "$d" "$list[$d##disk]"; done
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 15 at 19:23

























                answered Jan 15 at 18:23









                steeldriver

                31.6k34979




                31.6k34979











                • I not want to use the $!list[@] in the for loop
                  – yael
                  Jan 15 at 19:17










                • for loop is only for the disks not for the sdX strings
                  – yael
                  Jan 15 at 19:17










                • @yael <shrug> OK - please see update for an alternative
                  – steeldriver
                  Jan 15 at 19:25










                • why not use - counter=0 ; list=(sda..f) ; and in the loop --> echo "$list[counter++]"
                  – yael
                  Jan 15 at 19:44










                • it much more simple
                  – yael
                  Jan 15 at 19:44
















                • I not want to use the $!list[@] in the for loop
                  – yael
                  Jan 15 at 19:17










                • for loop is only for the disks not for the sdX strings
                  – yael
                  Jan 15 at 19:17










                • @yael <shrug> OK - please see update for an alternative
                  – steeldriver
                  Jan 15 at 19:25










                • why not use - counter=0 ; list=(sda..f) ; and in the loop --> echo "$list[counter++]"
                  – yael
                  Jan 15 at 19:44










                • it much more simple
                  – yael
                  Jan 15 at 19:44















                I not want to use the $!list[@] in the for loop
                – yael
                Jan 15 at 19:17




                I not want to use the $!list[@] in the for loop
                – yael
                Jan 15 at 19:17












                for loop is only for the disks not for the sdX strings
                – yael
                Jan 15 at 19:17




                for loop is only for the disks not for the sdX strings
                – yael
                Jan 15 at 19:17












                @yael <shrug> OK - please see update for an alternative
                – steeldriver
                Jan 15 at 19:25




                @yael <shrug> OK - please see update for an alternative
                – steeldriver
                Jan 15 at 19:25












                why not use - counter=0 ; list=(sda..f) ; and in the loop --> echo "$list[counter++]"
                – yael
                Jan 15 at 19:44




                why not use - counter=0 ; list=(sda..f) ; and in the loop --> echo "$list[counter++]"
                – yael
                Jan 15 at 19:44












                it much more simple
                – yael
                Jan 15 at 19:44




                it much more simple
                – yael
                Jan 15 at 19:44










                up vote
                0
                down vote













                You may build a counter for disk column to feet the list & replace strings for use as export



                -bash-4.4$ MAX=z
                -bash-4.4$ list=$(eval echo sdb..$MAX)
                -bash-4.4$ echo $list
                sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz
                -bash-4.4$ disk=1 ; for i in $(echo $list) ; do echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1)) ; done
                disk1 sdb
                disk2 sdc
                disk3 sdd
                disk4 sde
                disk5 sdf
                disk6 sdg
                disk7 sdh
                disk8 sdi
                disk9 sdj
                disk10 sdk
                disk11 sdl
                disk12 sdm
                disk13 sdn
                disk14 sdo
                disk15 sdp
                disk16 sdq
                disk17 sdr
                disk18 sds
                disk19 sdt
                disk20 sdu
                disk21 sdv
                disk22 sdw
                disk23 sdx
                disk24 sdy
                disk25 sdz
                -bash-4.4$


                be careful here I use a define variable disk to count & a t (tabulation) as separator; you may adapt to feet your needs if you prefer a space or something else.



                if you wanna get this output in a file add a redirection of output
                & of course in a script you may want to use code as this (without ; )



                disk=1 
                for i in $(echo $list)
                do
                echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1))
                done > file


                this is the easy way to understand it.
                A better method is to use the bash substitution instead of sed
                see bash method $!list[@] & either printf command or $parameter/pattern/string substituion advanced functions ...






                share|improve this answer
























                  up vote
                  0
                  down vote













                  You may build a counter for disk column to feet the list & replace strings for use as export



                  -bash-4.4$ MAX=z
                  -bash-4.4$ list=$(eval echo sdb..$MAX)
                  -bash-4.4$ echo $list
                  sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz
                  -bash-4.4$ disk=1 ; for i in $(echo $list) ; do echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1)) ; done
                  disk1 sdb
                  disk2 sdc
                  disk3 sdd
                  disk4 sde
                  disk5 sdf
                  disk6 sdg
                  disk7 sdh
                  disk8 sdi
                  disk9 sdj
                  disk10 sdk
                  disk11 sdl
                  disk12 sdm
                  disk13 sdn
                  disk14 sdo
                  disk15 sdp
                  disk16 sdq
                  disk17 sdr
                  disk18 sds
                  disk19 sdt
                  disk20 sdu
                  disk21 sdv
                  disk22 sdw
                  disk23 sdx
                  disk24 sdy
                  disk25 sdz
                  -bash-4.4$


                  be careful here I use a define variable disk to count & a t (tabulation) as separator; you may adapt to feet your needs if you prefer a space or something else.



                  if you wanna get this output in a file add a redirection of output
                  & of course in a script you may want to use code as this (without ; )



                  disk=1 
                  for i in $(echo $list)
                  do
                  echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1))
                  done > file


                  this is the easy way to understand it.
                  A better method is to use the bash substitution instead of sed
                  see bash method $!list[@] & either printf command or $parameter/pattern/string substituion advanced functions ...






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You may build a counter for disk column to feet the list & replace strings for use as export



                    -bash-4.4$ MAX=z
                    -bash-4.4$ list=$(eval echo sdb..$MAX)
                    -bash-4.4$ echo $list
                    sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz
                    -bash-4.4$ disk=1 ; for i in $(echo $list) ; do echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1)) ; done
                    disk1 sdb
                    disk2 sdc
                    disk3 sdd
                    disk4 sde
                    disk5 sdf
                    disk6 sdg
                    disk7 sdh
                    disk8 sdi
                    disk9 sdj
                    disk10 sdk
                    disk11 sdl
                    disk12 sdm
                    disk13 sdn
                    disk14 sdo
                    disk15 sdp
                    disk16 sdq
                    disk17 sdr
                    disk18 sds
                    disk19 sdt
                    disk20 sdu
                    disk21 sdv
                    disk22 sdw
                    disk23 sdx
                    disk24 sdy
                    disk25 sdz
                    -bash-4.4$


                    be careful here I use a define variable disk to count & a t (tabulation) as separator; you may adapt to feet your needs if you prefer a space or something else.



                    if you wanna get this output in a file add a redirection of output
                    & of course in a script you may want to use code as this (without ; )



                    disk=1 
                    for i in $(echo $list)
                    do
                    echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1))
                    done > file


                    this is the easy way to understand it.
                    A better method is to use the bash substitution instead of sed
                    see bash method $!list[@] & either printf command or $parameter/pattern/string substituion advanced functions ...






                    share|improve this answer












                    You may build a counter for disk column to feet the list & replace strings for use as export



                    -bash-4.4$ MAX=z
                    -bash-4.4$ list=$(eval echo sdb..$MAX)
                    -bash-4.4$ echo $list
                    sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz
                    -bash-4.4$ disk=1 ; for i in $(echo $list) ; do echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1)) ; done
                    disk1 sdb
                    disk2 sdc
                    disk3 sdd
                    disk4 sde
                    disk5 sdf
                    disk6 sdg
                    disk7 sdh
                    disk8 sdi
                    disk9 sdj
                    disk10 sdk
                    disk11 sdl
                    disk12 sdm
                    disk13 sdn
                    disk14 sdo
                    disk15 sdp
                    disk16 sdq
                    disk17 sdr
                    disk18 sds
                    disk19 sdt
                    disk20 sdu
                    disk21 sdv
                    disk22 sdw
                    disk23 sdx
                    disk24 sdy
                    disk25 sdz
                    -bash-4.4$


                    be careful here I use a define variable disk to count & a t (tabulation) as separator; you may adapt to feet your needs if you prefer a space or something else.



                    if you wanna get this output in a file add a redirection of output
                    & of course in a script you may want to use code as this (without ; )



                    disk=1 
                    for i in $(echo $list)
                    do
                    echo $i | sed 's/sd./disk'$disk't &/' ; disk=$(($disk +1))
                    done > file


                    this is the easy way to understand it.
                    A better method is to use the bash substitution instead of sed
                    see bash method $!list[@] & either printf command or $parameter/pattern/string substituion advanced functions ...







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 15 at 18:24









                    francois P

                    914114




                    914114




















                        up vote
                        0
                        down vote













                        Bash has two simple and straightforward built-in features that
                        together will solve your problem: set and shift.



                        set $list will assign each element of $list to one of bash's
                        positional parameters.



                        shift renames all positional parameter $n+1 to $n, effectively
                        deleting $1 and "shifting" all positional parameters forward.



                        So, in your example, your statement echo $i <...syntax...> would be
                        echo $i " " $1; shift.



                        BTW, personally, I would use bash's printf instead of echo (See
                        elsewhere for trouble echo can give you): printf "%s %sn" $i $1; shift






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Bash has two simple and straightforward built-in features that
                          together will solve your problem: set and shift.



                          set $list will assign each element of $list to one of bash's
                          positional parameters.



                          shift renames all positional parameter $n+1 to $n, effectively
                          deleting $1 and "shifting" all positional parameters forward.



                          So, in your example, your statement echo $i <...syntax...> would be
                          echo $i " " $1; shift.



                          BTW, personally, I would use bash's printf instead of echo (See
                          elsewhere for trouble echo can give you): printf "%s %sn" $i $1; shift






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Bash has two simple and straightforward built-in features that
                            together will solve your problem: set and shift.



                            set $list will assign each element of $list to one of bash's
                            positional parameters.



                            shift renames all positional parameter $n+1 to $n, effectively
                            deleting $1 and "shifting" all positional parameters forward.



                            So, in your example, your statement echo $i <...syntax...> would be
                            echo $i " " $1; shift.



                            BTW, personally, I would use bash's printf instead of echo (See
                            elsewhere for trouble echo can give you): printf "%s %sn" $i $1; shift






                            share|improve this answer












                            Bash has two simple and straightforward built-in features that
                            together will solve your problem: set and shift.



                            set $list will assign each element of $list to one of bash's
                            positional parameters.



                            shift renames all positional parameter $n+1 to $n, effectively
                            deleting $1 and "shifting" all positional parameters forward.



                            So, in your example, your statement echo $i <...syntax...> would be
                            echo $i " " $1; shift.



                            BTW, personally, I would use bash's printf instead of echo (See
                            elsewhere for trouble echo can give you): printf "%s %sn" $i $1; shift







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 15 at 20:17









                            user1404316

                            2,314520




                            2,314520






















                                 

                                draft saved


                                draft discarded


























                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417307%2fbash-how-to-shift-to-the-next-string-in-list%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                Peggy Mitchell

                                Palaiologos

                                The Forum (Inglewood, California)