How to replace one char to get many strings in Shell?

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











up vote
5
down vote

favorite
1












I can find many questions with answers in the other direction, but unfortunately not in the one I would like to have my replacements: I intend to replace a char, such as #, in a string, such as test#asdf, with a sequence, such as 0..10 to get a sequence of strings, in this example test0asdf test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf.



I tried, from others:




  • echo '_#.test' | tr # 0..10 (throws usage)


  • echo '_#.test' | sed -r 's/#/0..10/g' (returns _0..10.test)


  • echo '_#.test' | sed -r 's/#/'0..10'/g' (works for the first, afterwards I get sed: can't read (...) no such file or directory)

What is a working approach to this problem?



Edit, as I may not comment yet: I have to use # in the string, in which this character should be replaced, as the string passed from another program. I could first replace it with another char though.







share|improve this question






















  • Why not just echo _0..10.test? The way you have it, it's echoing the string first and then replacing the character which isn't going to work. You could also replace the character and then echo.
    – Nasir Riley
    Aug 27 at 19:02














up vote
5
down vote

favorite
1












I can find many questions with answers in the other direction, but unfortunately not in the one I would like to have my replacements: I intend to replace a char, such as #, in a string, such as test#asdf, with a sequence, such as 0..10 to get a sequence of strings, in this example test0asdf test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf.



I tried, from others:




  • echo '_#.test' | tr # 0..10 (throws usage)


  • echo '_#.test' | sed -r 's/#/0..10/g' (returns _0..10.test)


  • echo '_#.test' | sed -r 's/#/'0..10'/g' (works for the first, afterwards I get sed: can't read (...) no such file or directory)

What is a working approach to this problem?



Edit, as I may not comment yet: I have to use # in the string, in which this character should be replaced, as the string passed from another program. I could first replace it with another char though.







share|improve this question






















  • Why not just echo _0..10.test? The way you have it, it's echoing the string first and then replacing the character which isn't going to work. You could also replace the character and then echo.
    – Nasir Riley
    Aug 27 at 19:02












up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





I can find many questions with answers in the other direction, but unfortunately not in the one I would like to have my replacements: I intend to replace a char, such as #, in a string, such as test#asdf, with a sequence, such as 0..10 to get a sequence of strings, in this example test0asdf test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf.



I tried, from others:




  • echo '_#.test' | tr # 0..10 (throws usage)


  • echo '_#.test' | sed -r 's/#/0..10/g' (returns _0..10.test)


  • echo '_#.test' | sed -r 's/#/'0..10'/g' (works for the first, afterwards I get sed: can't read (...) no such file or directory)

What is a working approach to this problem?



Edit, as I may not comment yet: I have to use # in the string, in which this character should be replaced, as the string passed from another program. I could first replace it with another char though.







share|improve this question














I can find many questions with answers in the other direction, but unfortunately not in the one I would like to have my replacements: I intend to replace a char, such as #, in a string, such as test#asdf, with a sequence, such as 0..10 to get a sequence of strings, in this example test0asdf test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf.



I tried, from others:




  • echo '_#.test' | tr # 0..10 (throws usage)


  • echo '_#.test' | sed -r 's/#/0..10/g' (returns _0..10.test)


  • echo '_#.test' | sed -r 's/#/'0..10'/g' (works for the first, afterwards I get sed: can't read (...) no such file or directory)

What is a working approach to this problem?



Edit, as I may not comment yet: I have to use # in the string, in which this character should be replaced, as the string passed from another program. I could first replace it with another char though.









share|improve this question













share|improve this question




share|improve this question








edited Aug 27 at 18:31

























asked Aug 27 at 18:18









BernhardWebstudio

284




284











  • Why not just echo _0..10.test? The way you have it, it's echoing the string first and then replacing the character which isn't going to work. You could also replace the character and then echo.
    – Nasir Riley
    Aug 27 at 19:02
















  • Why not just echo _0..10.test? The way you have it, it's echoing the string first and then replacing the character which isn't going to work. You could also replace the character and then echo.
    – Nasir Riley
    Aug 27 at 19:02















Why not just echo _0..10.test? The way you have it, it's echoing the string first and then replacing the character which isn't going to work. You could also replace the character and then echo.
– Nasir Riley
Aug 27 at 19:02




Why not just echo _0..10.test? The way you have it, it's echoing the string first and then replacing the character which isn't going to work. You could also replace the character and then echo.
– Nasir Riley
Aug 27 at 19:02










6 Answers
6






active

oldest

votes

















up vote
6
down vote



accepted










The 0..10 zsh operator (now also supported by a few other shells including bash) is just another form of csh-style brace expansion.



It is expanded by the shell before calling the command. The command doesn't see those 0..10.



With tr '#' 0..10 (quoting that # as otherwise it's parsed by the shell as the start of a comment), tr ends being called with ("tr", "#", "0", "1", ..., "10") as arguments and tr doesn't expect that many arguments.



Here, you'd want:



echo '_'0..10'.test' 


for echo to be passed "_0.test", "_1.test", ..., "_10.test" as arguments.



Or if you wanted that # to be translated into that 0..10 operator, transform it into shell code to be evaluated:



eval "$(echo 'echo _#.test' | sed 's/#/0..10/')"


where eval is being passed echo _0..10.test as arguments.



(not that I would recommend doing anything like that).






share|improve this answer





























    up vote
    6
    down vote













    You can split the string on the delimiter, capture the prefix and the suffix, then use brace expansion to generate the names:



    str='test#asdf'
    IFS='#' read -r prefix suffix <<<"$str"
    names=( "$prefix"0..10"$suffix" )
    declare -p names




    declare -a names='([0]="test0asdf" [1]="test1asdf" [2]="test2asdf" [3]="test3asdf" [4]="test4asdf" [5]="test5asdf" [6]="test6asdf" [7]="test7asdf" [8]="test8asdf" [9]="test9asdf" [10]="test10asdf")'





    share|improve this answer





























      up vote
      2
      down vote













      Do you have to use #? Maybe you could use %d?



      $ for i in 1..10; do printf "_%d.test " "$i"; done
      _1.test _2.test _3.test _4.test _5.test _6.test _7.test _8.test _9.test _10.test





      share|improve this answer



























        up vote
        2
        down vote













        First # starts a comment. So, you need to escape it with .



        Second, use a for loop.



        Here is your solution:



        for i in 1..10
        do
        echo '_#.test' | tr # $i
        done


        tr unfortunately does not work for more than one character, such as when you want to substitute # with 10. You are better off using sed for that reason.



        for i in 1..10
        do
        echo '_#.test' | sed "s/#/$i/"
        done





        share|improve this answer


















        • 1




          What about the "10" result?
          – RudiC
          Aug 28 at 8:02










        • Thanks @RudiC. Fixed my answer.
          – unxnut
          Aug 28 at 13:13

















        up vote
        1
        down vote













        I would do it with parameter expansion:



        $ var='test#asdf'
        $ for i in 1..10; do echo "$var/#/"$i""; done
        test1asdf
        test2asdf
        test3asdf
        test4asdf
        test5asdf
        test6asdf
        test7asdf
        test8asdf
        test9asdf
        test10asdf


        The $parameter/pattern/string expansion



        • takes the expansion of $parameter (in our case, $var) and

        • replaces the first occurrence of pattern (the escaped # – /# has a special meaning in the context, "replace at the beginning of the string", which we want to avoid) with


        • string ("$i" in our case)

        Alternatively, you could replace the # with %d and use it as the format string for printf:



        printf "$var/#/%d\n" 1..10





        share|improve this answer



























          up vote
          0
          down vote













          If what you need is a string expansion, then this is enough:



          echo 'test'1..11'asdf'
          test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf test11asdf


          If you need to replace a # in several strings, you may use the positional arguments:



          $ set -- test#asdf,,,,,,,,,,
          $ printf '%s ' "$@"; echo
          test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf

          $ j=0; for i do echo "$i//#/"$((j+=1))""; done
          test1asdf
          test2asdf
          test3asdf
          test4asdf
          test5asdf
          test6asdf
          test7asdf
          test8asdf
          test9asdf
          test10asdf
          test11asdf





          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%2f465136%2fhow-to-replace-one-char-to-get-many-strings-in-shell%23new-answer', 'question_page');

            );

            Post as a guest






























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            6
            down vote



            accepted










            The 0..10 zsh operator (now also supported by a few other shells including bash) is just another form of csh-style brace expansion.



            It is expanded by the shell before calling the command. The command doesn't see those 0..10.



            With tr '#' 0..10 (quoting that # as otherwise it's parsed by the shell as the start of a comment), tr ends being called with ("tr", "#", "0", "1", ..., "10") as arguments and tr doesn't expect that many arguments.



            Here, you'd want:



            echo '_'0..10'.test' 


            for echo to be passed "_0.test", "_1.test", ..., "_10.test" as arguments.



            Or if you wanted that # to be translated into that 0..10 operator, transform it into shell code to be evaluated:



            eval "$(echo 'echo _#.test' | sed 's/#/0..10/')"


            where eval is being passed echo _0..10.test as arguments.



            (not that I would recommend doing anything like that).






            share|improve this answer


























              up vote
              6
              down vote



              accepted










              The 0..10 zsh operator (now also supported by a few other shells including bash) is just another form of csh-style brace expansion.



              It is expanded by the shell before calling the command. The command doesn't see those 0..10.



              With tr '#' 0..10 (quoting that # as otherwise it's parsed by the shell as the start of a comment), tr ends being called with ("tr", "#", "0", "1", ..., "10") as arguments and tr doesn't expect that many arguments.



              Here, you'd want:



              echo '_'0..10'.test' 


              for echo to be passed "_0.test", "_1.test", ..., "_10.test" as arguments.



              Or if you wanted that # to be translated into that 0..10 operator, transform it into shell code to be evaluated:



              eval "$(echo 'echo _#.test' | sed 's/#/0..10/')"


              where eval is being passed echo _0..10.test as arguments.



              (not that I would recommend doing anything like that).






              share|improve this answer
























                up vote
                6
                down vote



                accepted







                up vote
                6
                down vote



                accepted






                The 0..10 zsh operator (now also supported by a few other shells including bash) is just another form of csh-style brace expansion.



                It is expanded by the shell before calling the command. The command doesn't see those 0..10.



                With tr '#' 0..10 (quoting that # as otherwise it's parsed by the shell as the start of a comment), tr ends being called with ("tr", "#", "0", "1", ..., "10") as arguments and tr doesn't expect that many arguments.



                Here, you'd want:



                echo '_'0..10'.test' 


                for echo to be passed "_0.test", "_1.test", ..., "_10.test" as arguments.



                Or if you wanted that # to be translated into that 0..10 operator, transform it into shell code to be evaluated:



                eval "$(echo 'echo _#.test' | sed 's/#/0..10/')"


                where eval is being passed echo _0..10.test as arguments.



                (not that I would recommend doing anything like that).






                share|improve this answer














                The 0..10 zsh operator (now also supported by a few other shells including bash) is just another form of csh-style brace expansion.



                It is expanded by the shell before calling the command. The command doesn't see those 0..10.



                With tr '#' 0..10 (quoting that # as otherwise it's parsed by the shell as the start of a comment), tr ends being called with ("tr", "#", "0", "1", ..., "10") as arguments and tr doesn't expect that many arguments.



                Here, you'd want:



                echo '_'0..10'.test' 


                for echo to be passed "_0.test", "_1.test", ..., "_10.test" as arguments.



                Or if you wanted that # to be translated into that 0..10 operator, transform it into shell code to be evaluated:



                eval "$(echo 'echo _#.test' | sed 's/#/0..10/')"


                where eval is being passed echo _0..10.test as arguments.



                (not that I would recommend doing anything like that).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 27 at 19:24

























                answered Aug 27 at 18:32









                Stéphane Chazelas

                283k53521855




                283k53521855






















                    up vote
                    6
                    down vote













                    You can split the string on the delimiter, capture the prefix and the suffix, then use brace expansion to generate the names:



                    str='test#asdf'
                    IFS='#' read -r prefix suffix <<<"$str"
                    names=( "$prefix"0..10"$suffix" )
                    declare -p names




                    declare -a names='([0]="test0asdf" [1]="test1asdf" [2]="test2asdf" [3]="test3asdf" [4]="test4asdf" [5]="test5asdf" [6]="test6asdf" [7]="test7asdf" [8]="test8asdf" [9]="test9asdf" [10]="test10asdf")'





                    share|improve this answer


























                      up vote
                      6
                      down vote













                      You can split the string on the delimiter, capture the prefix and the suffix, then use brace expansion to generate the names:



                      str='test#asdf'
                      IFS='#' read -r prefix suffix <<<"$str"
                      names=( "$prefix"0..10"$suffix" )
                      declare -p names




                      declare -a names='([0]="test0asdf" [1]="test1asdf" [2]="test2asdf" [3]="test3asdf" [4]="test4asdf" [5]="test5asdf" [6]="test6asdf" [7]="test7asdf" [8]="test8asdf" [9]="test9asdf" [10]="test10asdf")'





                      share|improve this answer
























                        up vote
                        6
                        down vote










                        up vote
                        6
                        down vote









                        You can split the string on the delimiter, capture the prefix and the suffix, then use brace expansion to generate the names:



                        str='test#asdf'
                        IFS='#' read -r prefix suffix <<<"$str"
                        names=( "$prefix"0..10"$suffix" )
                        declare -p names




                        declare -a names='([0]="test0asdf" [1]="test1asdf" [2]="test2asdf" [3]="test3asdf" [4]="test4asdf" [5]="test5asdf" [6]="test6asdf" [7]="test7asdf" [8]="test8asdf" [9]="test9asdf" [10]="test10asdf")'





                        share|improve this answer














                        You can split the string on the delimiter, capture the prefix and the suffix, then use brace expansion to generate the names:



                        str='test#asdf'
                        IFS='#' read -r prefix suffix <<<"$str"
                        names=( "$prefix"0..10"$suffix" )
                        declare -p names




                        declare -a names='([0]="test0asdf" [1]="test1asdf" [2]="test2asdf" [3]="test3asdf" [4]="test4asdf" [5]="test5asdf" [6]="test6asdf" [7]="test7asdf" [8]="test8asdf" [9]="test9asdf" [10]="test10asdf")'






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Aug 27 at 20:42

























                        answered Aug 27 at 19:07









                        glenn jackman

                        47k265103




                        47k265103




















                            up vote
                            2
                            down vote













                            Do you have to use #? Maybe you could use %d?



                            $ for i in 1..10; do printf "_%d.test " "$i"; done
                            _1.test _2.test _3.test _4.test _5.test _6.test _7.test _8.test _9.test _10.test





                            share|improve this answer
























                              up vote
                              2
                              down vote













                              Do you have to use #? Maybe you could use %d?



                              $ for i in 1..10; do printf "_%d.test " "$i"; done
                              _1.test _2.test _3.test _4.test _5.test _6.test _7.test _8.test _9.test _10.test





                              share|improve this answer






















                                up vote
                                2
                                down vote










                                up vote
                                2
                                down vote









                                Do you have to use #? Maybe you could use %d?



                                $ for i in 1..10; do printf "_%d.test " "$i"; done
                                _1.test _2.test _3.test _4.test _5.test _6.test _7.test _8.test _9.test _10.test





                                share|improve this answer












                                Do you have to use #? Maybe you could use %d?



                                $ for i in 1..10; do printf "_%d.test " "$i"; done
                                _1.test _2.test _3.test _4.test _5.test _6.test _7.test _8.test _9.test _10.test






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Aug 27 at 18:29









                                DopeGhoti

                                40.7k54979




                                40.7k54979




















                                    up vote
                                    2
                                    down vote













                                    First # starts a comment. So, you need to escape it with .



                                    Second, use a for loop.



                                    Here is your solution:



                                    for i in 1..10
                                    do
                                    echo '_#.test' | tr # $i
                                    done


                                    tr unfortunately does not work for more than one character, such as when you want to substitute # with 10. You are better off using sed for that reason.



                                    for i in 1..10
                                    do
                                    echo '_#.test' | sed "s/#/$i/"
                                    done





                                    share|improve this answer


















                                    • 1




                                      What about the "10" result?
                                      – RudiC
                                      Aug 28 at 8:02










                                    • Thanks @RudiC. Fixed my answer.
                                      – unxnut
                                      Aug 28 at 13:13














                                    up vote
                                    2
                                    down vote













                                    First # starts a comment. So, you need to escape it with .



                                    Second, use a for loop.



                                    Here is your solution:



                                    for i in 1..10
                                    do
                                    echo '_#.test' | tr # $i
                                    done


                                    tr unfortunately does not work for more than one character, such as when you want to substitute # with 10. You are better off using sed for that reason.



                                    for i in 1..10
                                    do
                                    echo '_#.test' | sed "s/#/$i/"
                                    done





                                    share|improve this answer


















                                    • 1




                                      What about the "10" result?
                                      – RudiC
                                      Aug 28 at 8:02










                                    • Thanks @RudiC. Fixed my answer.
                                      – unxnut
                                      Aug 28 at 13:13












                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    First # starts a comment. So, you need to escape it with .



                                    Second, use a for loop.



                                    Here is your solution:



                                    for i in 1..10
                                    do
                                    echo '_#.test' | tr # $i
                                    done


                                    tr unfortunately does not work for more than one character, such as when you want to substitute # with 10. You are better off using sed for that reason.



                                    for i in 1..10
                                    do
                                    echo '_#.test' | sed "s/#/$i/"
                                    done





                                    share|improve this answer














                                    First # starts a comment. So, you need to escape it with .



                                    Second, use a for loop.



                                    Here is your solution:



                                    for i in 1..10
                                    do
                                    echo '_#.test' | tr # $i
                                    done


                                    tr unfortunately does not work for more than one character, such as when you want to substitute # with 10. You are better off using sed for that reason.



                                    for i in 1..10
                                    do
                                    echo '_#.test' | sed "s/#/$i/"
                                    done






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Aug 28 at 13:13

























                                    answered Aug 27 at 18:26









                                    unxnut

                                    3,3802918




                                    3,3802918







                                    • 1




                                      What about the "10" result?
                                      – RudiC
                                      Aug 28 at 8:02










                                    • Thanks @RudiC. Fixed my answer.
                                      – unxnut
                                      Aug 28 at 13:13












                                    • 1




                                      What about the "10" result?
                                      – RudiC
                                      Aug 28 at 8:02










                                    • Thanks @RudiC. Fixed my answer.
                                      – unxnut
                                      Aug 28 at 13:13







                                    1




                                    1




                                    What about the "10" result?
                                    – RudiC
                                    Aug 28 at 8:02




                                    What about the "10" result?
                                    – RudiC
                                    Aug 28 at 8:02












                                    Thanks @RudiC. Fixed my answer.
                                    – unxnut
                                    Aug 28 at 13:13




                                    Thanks @RudiC. Fixed my answer.
                                    – unxnut
                                    Aug 28 at 13:13










                                    up vote
                                    1
                                    down vote













                                    I would do it with parameter expansion:



                                    $ var='test#asdf'
                                    $ for i in 1..10; do echo "$var/#/"$i""; done
                                    test1asdf
                                    test2asdf
                                    test3asdf
                                    test4asdf
                                    test5asdf
                                    test6asdf
                                    test7asdf
                                    test8asdf
                                    test9asdf
                                    test10asdf


                                    The $parameter/pattern/string expansion



                                    • takes the expansion of $parameter (in our case, $var) and

                                    • replaces the first occurrence of pattern (the escaped # – /# has a special meaning in the context, "replace at the beginning of the string", which we want to avoid) with


                                    • string ("$i" in our case)

                                    Alternatively, you could replace the # with %d and use it as the format string for printf:



                                    printf "$var/#/%d\n" 1..10





                                    share|improve this answer
























                                      up vote
                                      1
                                      down vote













                                      I would do it with parameter expansion:



                                      $ var='test#asdf'
                                      $ for i in 1..10; do echo "$var/#/"$i""; done
                                      test1asdf
                                      test2asdf
                                      test3asdf
                                      test4asdf
                                      test5asdf
                                      test6asdf
                                      test7asdf
                                      test8asdf
                                      test9asdf
                                      test10asdf


                                      The $parameter/pattern/string expansion



                                      • takes the expansion of $parameter (in our case, $var) and

                                      • replaces the first occurrence of pattern (the escaped # – /# has a special meaning in the context, "replace at the beginning of the string", which we want to avoid) with


                                      • string ("$i" in our case)

                                      Alternatively, you could replace the # with %d and use it as the format string for printf:



                                      printf "$var/#/%d\n" 1..10





                                      share|improve this answer






















                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote









                                        I would do it with parameter expansion:



                                        $ var='test#asdf'
                                        $ for i in 1..10; do echo "$var/#/"$i""; done
                                        test1asdf
                                        test2asdf
                                        test3asdf
                                        test4asdf
                                        test5asdf
                                        test6asdf
                                        test7asdf
                                        test8asdf
                                        test9asdf
                                        test10asdf


                                        The $parameter/pattern/string expansion



                                        • takes the expansion of $parameter (in our case, $var) and

                                        • replaces the first occurrence of pattern (the escaped # – /# has a special meaning in the context, "replace at the beginning of the string", which we want to avoid) with


                                        • string ("$i" in our case)

                                        Alternatively, you could replace the # with %d and use it as the format string for printf:



                                        printf "$var/#/%d\n" 1..10





                                        share|improve this answer












                                        I would do it with parameter expansion:



                                        $ var='test#asdf'
                                        $ for i in 1..10; do echo "$var/#/"$i""; done
                                        test1asdf
                                        test2asdf
                                        test3asdf
                                        test4asdf
                                        test5asdf
                                        test6asdf
                                        test7asdf
                                        test8asdf
                                        test9asdf
                                        test10asdf


                                        The $parameter/pattern/string expansion



                                        • takes the expansion of $parameter (in our case, $var) and

                                        • replaces the first occurrence of pattern (the escaped # – /# has a special meaning in the context, "replace at the beginning of the string", which we want to avoid) with


                                        • string ("$i" in our case)

                                        Alternatively, you could replace the # with %d and use it as the format string for printf:



                                        printf "$var/#/%d\n" 1..10






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Aug 27 at 18:52









                                        Benjamin W.

                                        363110




                                        363110




















                                            up vote
                                            0
                                            down vote













                                            If what you need is a string expansion, then this is enough:



                                            echo 'test'1..11'asdf'
                                            test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf test11asdf


                                            If you need to replace a # in several strings, you may use the positional arguments:



                                            $ set -- test#asdf,,,,,,,,,,
                                            $ printf '%s ' "$@"; echo
                                            test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf

                                            $ j=0; for i do echo "$i//#/"$((j+=1))""; done
                                            test1asdf
                                            test2asdf
                                            test3asdf
                                            test4asdf
                                            test5asdf
                                            test6asdf
                                            test7asdf
                                            test8asdf
                                            test9asdf
                                            test10asdf
                                            test11asdf





                                            share|improve this answer
























                                              up vote
                                              0
                                              down vote













                                              If what you need is a string expansion, then this is enough:



                                              echo 'test'1..11'asdf'
                                              test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf test11asdf


                                              If you need to replace a # in several strings, you may use the positional arguments:



                                              $ set -- test#asdf,,,,,,,,,,
                                              $ printf '%s ' "$@"; echo
                                              test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf

                                              $ j=0; for i do echo "$i//#/"$((j+=1))""; done
                                              test1asdf
                                              test2asdf
                                              test3asdf
                                              test4asdf
                                              test5asdf
                                              test6asdf
                                              test7asdf
                                              test8asdf
                                              test9asdf
                                              test10asdf
                                              test11asdf





                                              share|improve this answer






















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                If what you need is a string expansion, then this is enough:



                                                echo 'test'1..11'asdf'
                                                test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf test11asdf


                                                If you need to replace a # in several strings, you may use the positional arguments:



                                                $ set -- test#asdf,,,,,,,,,,
                                                $ printf '%s ' "$@"; echo
                                                test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf

                                                $ j=0; for i do echo "$i//#/"$((j+=1))""; done
                                                test1asdf
                                                test2asdf
                                                test3asdf
                                                test4asdf
                                                test5asdf
                                                test6asdf
                                                test7asdf
                                                test8asdf
                                                test9asdf
                                                test10asdf
                                                test11asdf





                                                share|improve this answer












                                                If what you need is a string expansion, then this is enough:



                                                echo 'test'1..11'asdf'
                                                test1asdf test2asdf test3asdf test4asdf test5asdf test6asdf test7asdf test8asdf test9asdf test10asdf test11asdf


                                                If you need to replace a # in several strings, you may use the positional arguments:



                                                $ set -- test#asdf,,,,,,,,,,
                                                $ printf '%s ' "$@"; echo
                                                test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf test#asdf

                                                $ j=0; for i do echo "$i//#/"$((j+=1))""; done
                                                test1asdf
                                                test2asdf
                                                test3asdf
                                                test4asdf
                                                test5asdf
                                                test6asdf
                                                test7asdf
                                                test8asdf
                                                test9asdf
                                                test10asdf
                                                test11asdf






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Aug 27 at 22:58









                                                Isaac

                                                6,8001834




                                                6,8001834



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465136%2fhow-to-replace-one-char-to-get-many-strings-in-shell%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    Comments

                                                    Popular posts from this blog

                                                    What does second last employer means? [closed]

                                                    List of Gilmore Girls characters

                                                    Confectionery