How to pass a variable containing slashes to sed





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







65















How do you pass a variable containing slashes as a pattern to sed?



For example, if I have the following variable:



var="/Users/Documents/name/file"


I want to pass it to sed as so:



sed "s/$var/replace/g" "$file"


However I get errors. How can I circumvent the issue?










share|improve this question































    65















    How do you pass a variable containing slashes as a pattern to sed?



    For example, if I have the following variable:



    var="/Users/Documents/name/file"


    I want to pass it to sed as so:



    sed "s/$var/replace/g" "$file"


    However I get errors. How can I circumvent the issue?










    share|improve this question



























      65












      65








      65


      13






      How do you pass a variable containing slashes as a pattern to sed?



      For example, if I have the following variable:



      var="/Users/Documents/name/file"


      I want to pass it to sed as so:



      sed "s/$var/replace/g" "$file"


      However I get errors. How can I circumvent the issue?










      share|improve this question
















      How do you pass a variable containing slashes as a pattern to sed?



      For example, if I have the following variable:



      var="/Users/Documents/name/file"


      I want to pass it to sed as so:



      sed "s/$var/replace/g" "$file"


      However I get errors. How can I circumvent the issue?







      bash sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 12 '18 at 9:06









      tripleee

      96.1k13133190




      96.1k13133190










      asked Jan 5 '15 at 20:41









      BolboaBolboa

      3,08473781




      3,08473781
























          6 Answers
          6






          active

          oldest

          votes


















          121














          Use an alternate regex delimiter as sed allows you to use any delimiter (including control characters):



          sed "s~$var~replace~g" $file





          share|improve this answer





















          • 1





            Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

            – Paul Ericson
            Sep 1 '16 at 0:41











          • That depends on what is value of $var

            – anubhava
            Sep 1 '16 at 7:39






          • 1





            Doesn't work with ~ as @PaulEricson observed, but still works with |

            – Kenny Ho
            Nov 4 '16 at 21:15






          • 1





            on ubuntu 16.04 it works with ~

            – Andrzej Rehmann
            Jan 2 '17 at 9:40






          • 1





            The last "~" (after g) seems not to be needed, at least for AWS Amazon Linux (CentOS based)

            – Saúl Martínez Vidals
            Jan 22 '18 at 5:44



















          38














          A pure bash answer: use parameter expansion to backslash-escape any slashes in the variable:



          var="/Users/Documents/name/file"
          sed "s/${var////\/}/replace/g" $file





          share|improve this answer


























          • This is a good way, because you don't always know what characters contains the variable. So you can always escape the sed delimiter if there is a doubt.For instance : sed "s:${var//:/\:}:replace:g" $file

            – Stephane L
            May 11 '17 at 10:25













          • Beautiful. Made some renaming scripts of mine much cleaner.

            – DevNull
            Nov 29 '17 at 13:33











          • That helped me a lot thank you :)

            – gabtzi
            Dec 26 '17 at 12:19






          • 2





            Some clarity on the pattern being used here: ${parameter/pattern/string}. So, in this case, the parameter is var, the pattern is //, and the string is \/. All instances of the pattern are replaced because the pattern begins with a /.

            – Josh
            Aug 7 '18 at 22:17






          • 1





            @josh, and so the leading slash of the pattern is not actually part of the pattern to replace.

            – glenn jackman
            Aug 7 '18 at 23:33



















          25














          Another way of doing it, although uglier than anubhava's answer, is by escaping all the backslashes in var using another sed command:



          var=$(echo "$var" | sed 's///\//g')


          then, this will work:



          sed "s/$var/replace/g" $file





          share|improve this answer































            8














            Using / in sed as a delimiter will conflict with the slashes in the variable when substituted and as a result you will get an error. One way to circumvent this is to use another delimiter that is unique from any characters that is in that variable.



            var="/Users/Documents/name/file"


            you can use the octothorpe character which suits the occasion (or any other character that is not a / for ease of use)



            sed "s#$var#replace#g" 


            or



              sed 's#$'$var'#replace#g'


            this is suitable when the variable does not contain spaces



            or



            sed 's#$"'$var'"#replace#g'


            It is wiser to use the above since we are interested in substituting whatever is in that variable only as compared to double quoting the whole command which can cause your shell to interpet any character that might be considered a special shell character to the shell.






            share|improve this answer
























            • Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

              – Paul Ericson
              Sep 1 '16 at 0:42











            • @PaulEricson I am unable to repro this on any Ubuntu available to me. If your $var contains ~, you will obviously need to pick a different delimiter. The "unterminated" error sounds like your $var contains a newline; you might be able to fix it by backslash-escaping every newline in the value, but I don't think this is entirely portable. This is by and large unrelated to the problem of slashes in the value.

              – tripleee
              Dec 12 '18 at 9:21











            • @PaulEricson Oh and your quoting is incorrect, you should not have the file name inside quotes. sed -i "s~blah~$var~g" file with quotes only around the actual sed script.

              – tripleee
              Jan 25 at 12:08





















            3














            This is an old question, but none of the answers here discuss operations other than s/from/to/ in much detail.



            The general form of a sed statement is



            *address* *action*


            where address can be a regex range or a line number range (or empty, in which case the action is applied to every input line). So for example



            sed '1,4d' file


            will delete lines 1 through 4 (the address is the line number range 1,4 and the action is the d delete command); and



            sed '/ick/,$s/foo/bar/' file


            will replace the first occurrence of foo with bar on any line between the first match on the regex ick and the end of the file (the address is the range /ick/,$ and the action is the s substitute command s/foo/bar/).



            In this context, if ick came from a variable, you could do



            sed "/$variable/,$s/foo/bar/"


            (notice the use of double quotes instead of single, so that the shell can interpolate the variable, and the necessity to quote the literal dollar sign inside double quotes) but if the variable contains a slash, you will get a syntax error. (The shell expands the variable, then passes the resulting string to sed; so sed only sees literal text - it has no concept of the shell's variables.)



            The cure is to use a different delimiter (where obviously you need to be able to use a character which cannot occur in the variable's value), but unlike in the s%foo%bar% case, you also need a backslash before the delimiter if you want to use a different delimiter than the default /:



            sed "\%$variable%,$s/foo/bar/" file


            (inside single quotes, a single backslash would obviously suffice); or you can separately escape every slash in the value. This particular syntax is Bash only:



            sed "/${variable////\/}/,$s/foo/bar/" file


            or if you use a different shell, try



            escaped=$(echo "$variable" | sed 's%/%\/%g')
            sed "s/$escaped/,$s/foo/bar/" file


            For clarity, if $variable contained the string 1/2 then the above commands would be equivalent to



            sed '%1/2%,$s/foo/bar/' file


            in the first case, and



            sed '/1/2/,$s/foo/bar/' file


            in the second.






            share|improve this answer

































              0














              Use Perl, where variables are first class citizens, not just expanding macros:



              var=/Users/Documents/name/file perl -pe 's/Q$ENV{var}/replace/g' $file




              • -p reads the input line by line and prints the line after processing


              • Q quotes all the metacharacters in the following string (not needed for the value presented here, but necessary if the value contained [ or some other values special for regular expresisons)






              share|improve this answer
























                Your Answer






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

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27787536%2fhow-to-pass-a-variable-containing-slashes-to-sed%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                121














                Use an alternate regex delimiter as sed allows you to use any delimiter (including control characters):



                sed "s~$var~replace~g" $file





                share|improve this answer





















                • 1





                  Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                  – Paul Ericson
                  Sep 1 '16 at 0:41











                • That depends on what is value of $var

                  – anubhava
                  Sep 1 '16 at 7:39






                • 1





                  Doesn't work with ~ as @PaulEricson observed, but still works with |

                  – Kenny Ho
                  Nov 4 '16 at 21:15






                • 1





                  on ubuntu 16.04 it works with ~

                  – Andrzej Rehmann
                  Jan 2 '17 at 9:40






                • 1





                  The last "~" (after g) seems not to be needed, at least for AWS Amazon Linux (CentOS based)

                  – Saúl Martínez Vidals
                  Jan 22 '18 at 5:44
















                121














                Use an alternate regex delimiter as sed allows you to use any delimiter (including control characters):



                sed "s~$var~replace~g" $file





                share|improve this answer





















                • 1





                  Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                  – Paul Ericson
                  Sep 1 '16 at 0:41











                • That depends on what is value of $var

                  – anubhava
                  Sep 1 '16 at 7:39






                • 1





                  Doesn't work with ~ as @PaulEricson observed, but still works with |

                  – Kenny Ho
                  Nov 4 '16 at 21:15






                • 1





                  on ubuntu 16.04 it works with ~

                  – Andrzej Rehmann
                  Jan 2 '17 at 9:40






                • 1





                  The last "~" (after g) seems not to be needed, at least for AWS Amazon Linux (CentOS based)

                  – Saúl Martínez Vidals
                  Jan 22 '18 at 5:44














                121












                121








                121







                Use an alternate regex delimiter as sed allows you to use any delimiter (including control characters):



                sed "s~$var~replace~g" $file





                share|improve this answer















                Use an alternate regex delimiter as sed allows you to use any delimiter (including control characters):



                sed "s~$var~replace~g" $file






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 22 '18 at 5:47

























                answered Jan 5 '15 at 20:41









                anubhavaanubhava

                535k48333410




                535k48333410








                • 1





                  Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                  – Paul Ericson
                  Sep 1 '16 at 0:41











                • That depends on what is value of $var

                  – anubhava
                  Sep 1 '16 at 7:39






                • 1





                  Doesn't work with ~ as @PaulEricson observed, but still works with |

                  – Kenny Ho
                  Nov 4 '16 at 21:15






                • 1





                  on ubuntu 16.04 it works with ~

                  – Andrzej Rehmann
                  Jan 2 '17 at 9:40






                • 1





                  The last "~" (after g) seems not to be needed, at least for AWS Amazon Linux (CentOS based)

                  – Saúl Martínez Vidals
                  Jan 22 '18 at 5:44














                • 1





                  Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                  – Paul Ericson
                  Sep 1 '16 at 0:41











                • That depends on what is value of $var

                  – anubhava
                  Sep 1 '16 at 7:39






                • 1





                  Doesn't work with ~ as @PaulEricson observed, but still works with |

                  – Kenny Ho
                  Nov 4 '16 at 21:15






                • 1





                  on ubuntu 16.04 it works with ~

                  – Andrzej Rehmann
                  Jan 2 '17 at 9:40






                • 1





                  The last "~" (after g) seems not to be needed, at least for AWS Amazon Linux (CentOS based)

                  – Saúl Martínez Vidals
                  Jan 22 '18 at 5:44








                1




                1





                Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                – Paul Ericson
                Sep 1 '16 at 0:41





                Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                – Paul Ericson
                Sep 1 '16 at 0:41













                That depends on what is value of $var

                – anubhava
                Sep 1 '16 at 7:39





                That depends on what is value of $var

                – anubhava
                Sep 1 '16 at 7:39




                1




                1





                Doesn't work with ~ as @PaulEricson observed, but still works with |

                – Kenny Ho
                Nov 4 '16 at 21:15





                Doesn't work with ~ as @PaulEricson observed, but still works with |

                – Kenny Ho
                Nov 4 '16 at 21:15




                1




                1





                on ubuntu 16.04 it works with ~

                – Andrzej Rehmann
                Jan 2 '17 at 9:40





                on ubuntu 16.04 it works with ~

                – Andrzej Rehmann
                Jan 2 '17 at 9:40




                1




                1





                The last "~" (after g) seems not to be needed, at least for AWS Amazon Linux (CentOS based)

                – Saúl Martínez Vidals
                Jan 22 '18 at 5:44





                The last "~" (after g) seems not to be needed, at least for AWS Amazon Linux (CentOS based)

                – Saúl Martínez Vidals
                Jan 22 '18 at 5:44













                38














                A pure bash answer: use parameter expansion to backslash-escape any slashes in the variable:



                var="/Users/Documents/name/file"
                sed "s/${var////\/}/replace/g" $file





                share|improve this answer


























                • This is a good way, because you don't always know what characters contains the variable. So you can always escape the sed delimiter if there is a doubt.For instance : sed "s:${var//:/\:}:replace:g" $file

                  – Stephane L
                  May 11 '17 at 10:25













                • Beautiful. Made some renaming scripts of mine much cleaner.

                  – DevNull
                  Nov 29 '17 at 13:33











                • That helped me a lot thank you :)

                  – gabtzi
                  Dec 26 '17 at 12:19






                • 2





                  Some clarity on the pattern being used here: ${parameter/pattern/string}. So, in this case, the parameter is var, the pattern is //, and the string is \/. All instances of the pattern are replaced because the pattern begins with a /.

                  – Josh
                  Aug 7 '18 at 22:17






                • 1





                  @josh, and so the leading slash of the pattern is not actually part of the pattern to replace.

                  – glenn jackman
                  Aug 7 '18 at 23:33
















                38














                A pure bash answer: use parameter expansion to backslash-escape any slashes in the variable:



                var="/Users/Documents/name/file"
                sed "s/${var////\/}/replace/g" $file





                share|improve this answer


























                • This is a good way, because you don't always know what characters contains the variable. So you can always escape the sed delimiter if there is a doubt.For instance : sed "s:${var//:/\:}:replace:g" $file

                  – Stephane L
                  May 11 '17 at 10:25













                • Beautiful. Made some renaming scripts of mine much cleaner.

                  – DevNull
                  Nov 29 '17 at 13:33











                • That helped me a lot thank you :)

                  – gabtzi
                  Dec 26 '17 at 12:19






                • 2





                  Some clarity on the pattern being used here: ${parameter/pattern/string}. So, in this case, the parameter is var, the pattern is //, and the string is \/. All instances of the pattern are replaced because the pattern begins with a /.

                  – Josh
                  Aug 7 '18 at 22:17






                • 1





                  @josh, and so the leading slash of the pattern is not actually part of the pattern to replace.

                  – glenn jackman
                  Aug 7 '18 at 23:33














                38












                38








                38







                A pure bash answer: use parameter expansion to backslash-escape any slashes in the variable:



                var="/Users/Documents/name/file"
                sed "s/${var////\/}/replace/g" $file





                share|improve this answer















                A pure bash answer: use parameter expansion to backslash-escape any slashes in the variable:



                var="/Users/Documents/name/file"
                sed "s/${var////\/}/replace/g" $file






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 8 '15 at 17:43

























                answered Jan 5 '15 at 22:02









                glenn jackmanglenn jackman

                171k26151244




                171k26151244













                • This is a good way, because you don't always know what characters contains the variable. So you can always escape the sed delimiter if there is a doubt.For instance : sed "s:${var//:/\:}:replace:g" $file

                  – Stephane L
                  May 11 '17 at 10:25













                • Beautiful. Made some renaming scripts of mine much cleaner.

                  – DevNull
                  Nov 29 '17 at 13:33











                • That helped me a lot thank you :)

                  – gabtzi
                  Dec 26 '17 at 12:19






                • 2





                  Some clarity on the pattern being used here: ${parameter/pattern/string}. So, in this case, the parameter is var, the pattern is //, and the string is \/. All instances of the pattern are replaced because the pattern begins with a /.

                  – Josh
                  Aug 7 '18 at 22:17






                • 1





                  @josh, and so the leading slash of the pattern is not actually part of the pattern to replace.

                  – glenn jackman
                  Aug 7 '18 at 23:33



















                • This is a good way, because you don't always know what characters contains the variable. So you can always escape the sed delimiter if there is a doubt.For instance : sed "s:${var//:/\:}:replace:g" $file

                  – Stephane L
                  May 11 '17 at 10:25













                • Beautiful. Made some renaming scripts of mine much cleaner.

                  – DevNull
                  Nov 29 '17 at 13:33











                • That helped me a lot thank you :)

                  – gabtzi
                  Dec 26 '17 at 12:19






                • 2





                  Some clarity on the pattern being used here: ${parameter/pattern/string}. So, in this case, the parameter is var, the pattern is //, and the string is \/. All instances of the pattern are replaced because the pattern begins with a /.

                  – Josh
                  Aug 7 '18 at 22:17






                • 1





                  @josh, and so the leading slash of the pattern is not actually part of the pattern to replace.

                  – glenn jackman
                  Aug 7 '18 at 23:33

















                This is a good way, because you don't always know what characters contains the variable. So you can always escape the sed delimiter if there is a doubt.For instance : sed "s:${var//:/\:}:replace:g" $file

                – Stephane L
                May 11 '17 at 10:25







                This is a good way, because you don't always know what characters contains the variable. So you can always escape the sed delimiter if there is a doubt.For instance : sed "s:${var//:/\:}:replace:g" $file

                – Stephane L
                May 11 '17 at 10:25















                Beautiful. Made some renaming scripts of mine much cleaner.

                – DevNull
                Nov 29 '17 at 13:33





                Beautiful. Made some renaming scripts of mine much cleaner.

                – DevNull
                Nov 29 '17 at 13:33













                That helped me a lot thank you :)

                – gabtzi
                Dec 26 '17 at 12:19





                That helped me a lot thank you :)

                – gabtzi
                Dec 26 '17 at 12:19




                2




                2





                Some clarity on the pattern being used here: ${parameter/pattern/string}. So, in this case, the parameter is var, the pattern is //, and the string is \/. All instances of the pattern are replaced because the pattern begins with a /.

                – Josh
                Aug 7 '18 at 22:17





                Some clarity on the pattern being used here: ${parameter/pattern/string}. So, in this case, the parameter is var, the pattern is //, and the string is \/. All instances of the pattern are replaced because the pattern begins with a /.

                – Josh
                Aug 7 '18 at 22:17




                1




                1





                @josh, and so the leading slash of the pattern is not actually part of the pattern to replace.

                – glenn jackman
                Aug 7 '18 at 23:33





                @josh, and so the leading slash of the pattern is not actually part of the pattern to replace.

                – glenn jackman
                Aug 7 '18 at 23:33











                25














                Another way of doing it, although uglier than anubhava's answer, is by escaping all the backslashes in var using another sed command:



                var=$(echo "$var" | sed 's///\//g')


                then, this will work:



                sed "s/$var/replace/g" $file





                share|improve this answer




























                  25














                  Another way of doing it, although uglier than anubhava's answer, is by escaping all the backslashes in var using another sed command:



                  var=$(echo "$var" | sed 's///\//g')


                  then, this will work:



                  sed "s/$var/replace/g" $file





                  share|improve this answer


























                    25












                    25








                    25







                    Another way of doing it, although uglier than anubhava's answer, is by escaping all the backslashes in var using another sed command:



                    var=$(echo "$var" | sed 's///\//g')


                    then, this will work:



                    sed "s/$var/replace/g" $file





                    share|improve this answer













                    Another way of doing it, although uglier than anubhava's answer, is by escaping all the backslashes in var using another sed command:



                    var=$(echo "$var" | sed 's///\//g')


                    then, this will work:



                    sed "s/$var/replace/g" $file






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 5 '15 at 23:45









                    BolboaBolboa

                    3,08473781




                    3,08473781























                        8














                        Using / in sed as a delimiter will conflict with the slashes in the variable when substituted and as a result you will get an error. One way to circumvent this is to use another delimiter that is unique from any characters that is in that variable.



                        var="/Users/Documents/name/file"


                        you can use the octothorpe character which suits the occasion (or any other character that is not a / for ease of use)



                        sed "s#$var#replace#g" 


                        or



                          sed 's#$'$var'#replace#g'


                        this is suitable when the variable does not contain spaces



                        or



                        sed 's#$"'$var'"#replace#g'


                        It is wiser to use the above since we are interested in substituting whatever is in that variable only as compared to double quoting the whole command which can cause your shell to interpet any character that might be considered a special shell character to the shell.






                        share|improve this answer
























                        • Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                          – Paul Ericson
                          Sep 1 '16 at 0:42











                        • @PaulEricson I am unable to repro this on any Ubuntu available to me. If your $var contains ~, you will obviously need to pick a different delimiter. The "unterminated" error sounds like your $var contains a newline; you might be able to fix it by backslash-escaping every newline in the value, but I don't think this is entirely portable. This is by and large unrelated to the problem of slashes in the value.

                          – tripleee
                          Dec 12 '18 at 9:21











                        • @PaulEricson Oh and your quoting is incorrect, you should not have the file name inside quotes. sed -i "s~blah~$var~g" file with quotes only around the actual sed script.

                          – tripleee
                          Jan 25 at 12:08


















                        8














                        Using / in sed as a delimiter will conflict with the slashes in the variable when substituted and as a result you will get an error. One way to circumvent this is to use another delimiter that is unique from any characters that is in that variable.



                        var="/Users/Documents/name/file"


                        you can use the octothorpe character which suits the occasion (or any other character that is not a / for ease of use)



                        sed "s#$var#replace#g" 


                        or



                          sed 's#$'$var'#replace#g'


                        this is suitable when the variable does not contain spaces



                        or



                        sed 's#$"'$var'"#replace#g'


                        It is wiser to use the above since we are interested in substituting whatever is in that variable only as compared to double quoting the whole command which can cause your shell to interpet any character that might be considered a special shell character to the shell.






                        share|improve this answer
























                        • Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                          – Paul Ericson
                          Sep 1 '16 at 0:42











                        • @PaulEricson I am unable to repro this on any Ubuntu available to me. If your $var contains ~, you will obviously need to pick a different delimiter. The "unterminated" error sounds like your $var contains a newline; you might be able to fix it by backslash-escaping every newline in the value, but I don't think this is entirely portable. This is by and large unrelated to the problem of slashes in the value.

                          – tripleee
                          Dec 12 '18 at 9:21











                        • @PaulEricson Oh and your quoting is incorrect, you should not have the file name inside quotes. sed -i "s~blah~$var~g" file with quotes only around the actual sed script.

                          – tripleee
                          Jan 25 at 12:08
















                        8












                        8








                        8







                        Using / in sed as a delimiter will conflict with the slashes in the variable when substituted and as a result you will get an error. One way to circumvent this is to use another delimiter that is unique from any characters that is in that variable.



                        var="/Users/Documents/name/file"


                        you can use the octothorpe character which suits the occasion (or any other character that is not a / for ease of use)



                        sed "s#$var#replace#g" 


                        or



                          sed 's#$'$var'#replace#g'


                        this is suitable when the variable does not contain spaces



                        or



                        sed 's#$"'$var'"#replace#g'


                        It is wiser to use the above since we are interested in substituting whatever is in that variable only as compared to double quoting the whole command which can cause your shell to interpet any character that might be considered a special shell character to the shell.






                        share|improve this answer













                        Using / in sed as a delimiter will conflict with the slashes in the variable when substituted and as a result you will get an error. One way to circumvent this is to use another delimiter that is unique from any characters that is in that variable.



                        var="/Users/Documents/name/file"


                        you can use the octothorpe character which suits the occasion (or any other character that is not a / for ease of use)



                        sed "s#$var#replace#g" 


                        or



                          sed 's#$'$var'#replace#g'


                        this is suitable when the variable does not contain spaces



                        or



                        sed 's#$"'$var'"#replace#g'


                        It is wiser to use the above since we are interested in substituting whatever is in that variable only as compared to double quoting the whole command which can cause your shell to interpet any character that might be considered a special shell character to the shell.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jan 5 '15 at 23:59









                        repzerorepzero

                        6,4832726




                        6,4832726













                        • Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                          – Paul Ericson
                          Sep 1 '16 at 0:42











                        • @PaulEricson I am unable to repro this on any Ubuntu available to me. If your $var contains ~, you will obviously need to pick a different delimiter. The "unterminated" error sounds like your $var contains a newline; you might be able to fix it by backslash-escaping every newline in the value, but I don't think this is entirely portable. This is by and large unrelated to the problem of slashes in the value.

                          – tripleee
                          Dec 12 '18 at 9:21











                        • @PaulEricson Oh and your quoting is incorrect, you should not have the file name inside quotes. sed -i "s~blah~$var~g" file with quotes only around the actual sed script.

                          – tripleee
                          Jan 25 at 12:08





















                        • Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                          – Paul Ericson
                          Sep 1 '16 at 0:42











                        • @PaulEricson I am unable to repro this on any Ubuntu available to me. If your $var contains ~, you will obviously need to pick a different delimiter. The "unterminated" error sounds like your $var contains a newline; you might be able to fix it by backslash-escaping every newline in the value, but I don't think this is entirely portable. This is by and large unrelated to the problem of slashes in the value.

                          – tripleee
                          Dec 12 '18 at 9:21











                        • @PaulEricson Oh and your quoting is incorrect, you should not have the file name inside quotes. sed -i "s~blah~$var~g" file with quotes only around the actual sed script.

                          – tripleee
                          Jan 25 at 12:08



















                        Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                        – Paul Ericson
                        Sep 1 '16 at 0:42





                        Doesn't work. I try sed -i "s~blah~$var~g file" and get: "sed -e expression #1, char 70: unterminated `s' command". I have confirmed that the culprit is a slash in $var. Variants of this solution to this are all over the place and none of them work. Was sed recently updated? I'm on v4.2.2 on Ubuntu 14.04.4 LTS.

                        – Paul Ericson
                        Sep 1 '16 at 0:42













                        @PaulEricson I am unable to repro this on any Ubuntu available to me. If your $var contains ~, you will obviously need to pick a different delimiter. The "unterminated" error sounds like your $var contains a newline; you might be able to fix it by backslash-escaping every newline in the value, but I don't think this is entirely portable. This is by and large unrelated to the problem of slashes in the value.

                        – tripleee
                        Dec 12 '18 at 9:21





                        @PaulEricson I am unable to repro this on any Ubuntu available to me. If your $var contains ~, you will obviously need to pick a different delimiter. The "unterminated" error sounds like your $var contains a newline; you might be able to fix it by backslash-escaping every newline in the value, but I don't think this is entirely portable. This is by and large unrelated to the problem of slashes in the value.

                        – tripleee
                        Dec 12 '18 at 9:21













                        @PaulEricson Oh and your quoting is incorrect, you should not have the file name inside quotes. sed -i "s~blah~$var~g" file with quotes only around the actual sed script.

                        – tripleee
                        Jan 25 at 12:08







                        @PaulEricson Oh and your quoting is incorrect, you should not have the file name inside quotes. sed -i "s~blah~$var~g" file with quotes only around the actual sed script.

                        – tripleee
                        Jan 25 at 12:08













                        3














                        This is an old question, but none of the answers here discuss operations other than s/from/to/ in much detail.



                        The general form of a sed statement is



                        *address* *action*


                        where address can be a regex range or a line number range (or empty, in which case the action is applied to every input line). So for example



                        sed '1,4d' file


                        will delete lines 1 through 4 (the address is the line number range 1,4 and the action is the d delete command); and



                        sed '/ick/,$s/foo/bar/' file


                        will replace the first occurrence of foo with bar on any line between the first match on the regex ick and the end of the file (the address is the range /ick/,$ and the action is the s substitute command s/foo/bar/).



                        In this context, if ick came from a variable, you could do



                        sed "/$variable/,$s/foo/bar/"


                        (notice the use of double quotes instead of single, so that the shell can interpolate the variable, and the necessity to quote the literal dollar sign inside double quotes) but if the variable contains a slash, you will get a syntax error. (The shell expands the variable, then passes the resulting string to sed; so sed only sees literal text - it has no concept of the shell's variables.)



                        The cure is to use a different delimiter (where obviously you need to be able to use a character which cannot occur in the variable's value), but unlike in the s%foo%bar% case, you also need a backslash before the delimiter if you want to use a different delimiter than the default /:



                        sed "\%$variable%,$s/foo/bar/" file


                        (inside single quotes, a single backslash would obviously suffice); or you can separately escape every slash in the value. This particular syntax is Bash only:



                        sed "/${variable////\/}/,$s/foo/bar/" file


                        or if you use a different shell, try



                        escaped=$(echo "$variable" | sed 's%/%\/%g')
                        sed "s/$escaped/,$s/foo/bar/" file


                        For clarity, if $variable contained the string 1/2 then the above commands would be equivalent to



                        sed '%1/2%,$s/foo/bar/' file


                        in the first case, and



                        sed '/1/2/,$s/foo/bar/' file


                        in the second.






                        share|improve this answer






























                          3














                          This is an old question, but none of the answers here discuss operations other than s/from/to/ in much detail.



                          The general form of a sed statement is



                          *address* *action*


                          where address can be a regex range or a line number range (or empty, in which case the action is applied to every input line). So for example



                          sed '1,4d' file


                          will delete lines 1 through 4 (the address is the line number range 1,4 and the action is the d delete command); and



                          sed '/ick/,$s/foo/bar/' file


                          will replace the first occurrence of foo with bar on any line between the first match on the regex ick and the end of the file (the address is the range /ick/,$ and the action is the s substitute command s/foo/bar/).



                          In this context, if ick came from a variable, you could do



                          sed "/$variable/,$s/foo/bar/"


                          (notice the use of double quotes instead of single, so that the shell can interpolate the variable, and the necessity to quote the literal dollar sign inside double quotes) but if the variable contains a slash, you will get a syntax error. (The shell expands the variable, then passes the resulting string to sed; so sed only sees literal text - it has no concept of the shell's variables.)



                          The cure is to use a different delimiter (where obviously you need to be able to use a character which cannot occur in the variable's value), but unlike in the s%foo%bar% case, you also need a backslash before the delimiter if you want to use a different delimiter than the default /:



                          sed "\%$variable%,$s/foo/bar/" file


                          (inside single quotes, a single backslash would obviously suffice); or you can separately escape every slash in the value. This particular syntax is Bash only:



                          sed "/${variable////\/}/,$s/foo/bar/" file


                          or if you use a different shell, try



                          escaped=$(echo "$variable" | sed 's%/%\/%g')
                          sed "s/$escaped/,$s/foo/bar/" file


                          For clarity, if $variable contained the string 1/2 then the above commands would be equivalent to



                          sed '%1/2%,$s/foo/bar/' file


                          in the first case, and



                          sed '/1/2/,$s/foo/bar/' file


                          in the second.






                          share|improve this answer




























                            3












                            3








                            3







                            This is an old question, but none of the answers here discuss operations other than s/from/to/ in much detail.



                            The general form of a sed statement is



                            *address* *action*


                            where address can be a regex range or a line number range (or empty, in which case the action is applied to every input line). So for example



                            sed '1,4d' file


                            will delete lines 1 through 4 (the address is the line number range 1,4 and the action is the d delete command); and



                            sed '/ick/,$s/foo/bar/' file


                            will replace the first occurrence of foo with bar on any line between the first match on the regex ick and the end of the file (the address is the range /ick/,$ and the action is the s substitute command s/foo/bar/).



                            In this context, if ick came from a variable, you could do



                            sed "/$variable/,$s/foo/bar/"


                            (notice the use of double quotes instead of single, so that the shell can interpolate the variable, and the necessity to quote the literal dollar sign inside double quotes) but if the variable contains a slash, you will get a syntax error. (The shell expands the variable, then passes the resulting string to sed; so sed only sees literal text - it has no concept of the shell's variables.)



                            The cure is to use a different delimiter (where obviously you need to be able to use a character which cannot occur in the variable's value), but unlike in the s%foo%bar% case, you also need a backslash before the delimiter if you want to use a different delimiter than the default /:



                            sed "\%$variable%,$s/foo/bar/" file


                            (inside single quotes, a single backslash would obviously suffice); or you can separately escape every slash in the value. This particular syntax is Bash only:



                            sed "/${variable////\/}/,$s/foo/bar/" file


                            or if you use a different shell, try



                            escaped=$(echo "$variable" | sed 's%/%\/%g')
                            sed "s/$escaped/,$s/foo/bar/" file


                            For clarity, if $variable contained the string 1/2 then the above commands would be equivalent to



                            sed '%1/2%,$s/foo/bar/' file


                            in the first case, and



                            sed '/1/2/,$s/foo/bar/' file


                            in the second.






                            share|improve this answer















                            This is an old question, but none of the answers here discuss operations other than s/from/to/ in much detail.



                            The general form of a sed statement is



                            *address* *action*


                            where address can be a regex range or a line number range (or empty, in which case the action is applied to every input line). So for example



                            sed '1,4d' file


                            will delete lines 1 through 4 (the address is the line number range 1,4 and the action is the d delete command); and



                            sed '/ick/,$s/foo/bar/' file


                            will replace the first occurrence of foo with bar on any line between the first match on the regex ick and the end of the file (the address is the range /ick/,$ and the action is the s substitute command s/foo/bar/).



                            In this context, if ick came from a variable, you could do



                            sed "/$variable/,$s/foo/bar/"


                            (notice the use of double quotes instead of single, so that the shell can interpolate the variable, and the necessity to quote the literal dollar sign inside double quotes) but if the variable contains a slash, you will get a syntax error. (The shell expands the variable, then passes the resulting string to sed; so sed only sees literal text - it has no concept of the shell's variables.)



                            The cure is to use a different delimiter (where obviously you need to be able to use a character which cannot occur in the variable's value), but unlike in the s%foo%bar% case, you also need a backslash before the delimiter if you want to use a different delimiter than the default /:



                            sed "\%$variable%,$s/foo/bar/" file


                            (inside single quotes, a single backslash would obviously suffice); or you can separately escape every slash in the value. This particular syntax is Bash only:



                            sed "/${variable////\/}/,$s/foo/bar/" file


                            or if you use a different shell, try



                            escaped=$(echo "$variable" | sed 's%/%\/%g')
                            sed "s/$escaped/,$s/foo/bar/" file


                            For clarity, if $variable contained the string 1/2 then the above commands would be equivalent to



                            sed '%1/2%,$s/foo/bar/' file


                            in the first case, and



                            sed '/1/2/,$s/foo/bar/' file


                            in the second.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 12 '18 at 19:08

























                            answered Dec 12 '18 at 8:54









                            tripleeetripleee

                            96.1k13133190




                            96.1k13133190























                                0














                                Use Perl, where variables are first class citizens, not just expanding macros:



                                var=/Users/Documents/name/file perl -pe 's/Q$ENV{var}/replace/g' $file




                                • -p reads the input line by line and prints the line after processing


                                • Q quotes all the metacharacters in the following string (not needed for the value presented here, but necessary if the value contained [ or some other values special for regular expresisons)






                                share|improve this answer




























                                  0














                                  Use Perl, where variables are first class citizens, not just expanding macros:



                                  var=/Users/Documents/name/file perl -pe 's/Q$ENV{var}/replace/g' $file




                                  • -p reads the input line by line and prints the line after processing


                                  • Q quotes all the metacharacters in the following string (not needed for the value presented here, but necessary if the value contained [ or some other values special for regular expresisons)






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Use Perl, where variables are first class citizens, not just expanding macros:



                                    var=/Users/Documents/name/file perl -pe 's/Q$ENV{var}/replace/g' $file




                                    • -p reads the input line by line and prints the line after processing


                                    • Q quotes all the metacharacters in the following string (not needed for the value presented here, but necessary if the value contained [ or some other values special for regular expresisons)






                                    share|improve this answer













                                    Use Perl, where variables are first class citizens, not just expanding macros:



                                    var=/Users/Documents/name/file perl -pe 's/Q$ENV{var}/replace/g' $file




                                    • -p reads the input line by line and prints the line after processing


                                    • Q quotes all the metacharacters in the following string (not needed for the value presented here, but necessary if the value contained [ or some other values special for regular expresisons)







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 22 '18 at 10:35









                                    chorobachoroba

                                    159k14142210




                                    159k14142210






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27787536%2fhow-to-pass-a-variable-containing-slashes-to-sed%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 change which sound is reproduced for terminal bell?

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents