What characters do I need to escape when using sed in a sh script?

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












218














Take the following script:



#!/bin/sh
sed 's/(127.0.1.1)s/1/' [some file]


If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between the octets, or in the s or 1). What's the rule here? What about when I need to use ... or [...]? Is there a list of what I do and don't need to escape?










share|improve this question



















  • 1




    Here is a bash function for converting paths for use with SED: function sedPath sed 's/]/]/g')>&1) #Escape path for use with sed
    – user2428118
    May 10 '16 at 12:57











  • See also: Which characters need to be escaped in Bash? How do we know it?
    – codeforester
    Feb 28 '18 at 7:02










  • Dura lex, sed sed
    – Nemo
    May 11 '18 at 19:56















218














Take the following script:



#!/bin/sh
sed 's/(127.0.1.1)s/1/' [some file]


If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between the octets, or in the s or 1). What's the rule here? What about when I need to use ... or [...]? Is there a list of what I do and don't need to escape?










share|improve this question



















  • 1




    Here is a bash function for converting paths for use with SED: function sedPath sed 's/]/]/g')>&1) #Escape path for use with sed
    – user2428118
    May 10 '16 at 12:57











  • See also: Which characters need to be escaped in Bash? How do we know it?
    – codeforester
    Feb 28 '18 at 7:02










  • Dura lex, sed sed
    – Nemo
    May 11 '18 at 19:56













218












218








218


101





Take the following script:



#!/bin/sh
sed 's/(127.0.1.1)s/1/' [some file]


If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between the octets, or in the s or 1). What's the rule here? What about when I need to use ... or [...]? Is there a list of what I do and don't need to escape?










share|improve this question















Take the following script:



#!/bin/sh
sed 's/(127.0.1.1)s/1/' [some file]


If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between the octets, or in the s or 1). What's the rule here? What about when I need to use ... or [...]? Is there a list of what I do and don't need to escape?







shell-script sed quoting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 '12 at 23:55









Gilles

530k12810611588




530k12810611588










asked Feb 28 '12 at 4:42









detlydetly

1,40851525




1,40851525







  • 1




    Here is a bash function for converting paths for use with SED: function sedPath sed 's/]/]/g')>&1) #Escape path for use with sed
    – user2428118
    May 10 '16 at 12:57











  • See also: Which characters need to be escaped in Bash? How do we know it?
    – codeforester
    Feb 28 '18 at 7:02










  • Dura lex, sed sed
    – Nemo
    May 11 '18 at 19:56












  • 1




    Here is a bash function for converting paths for use with SED: function sedPath sed 's/]/]/g')>&1) #Escape path for use with sed
    – user2428118
    May 10 '16 at 12:57











  • See also: Which characters need to be escaped in Bash? How do we know it?
    – codeforester
    Feb 28 '18 at 7:02










  • Dura lex, sed sed
    – Nemo
    May 11 '18 at 19:56







1




1




Here is a bash function for converting paths for use with SED: function sedPath sed 's/]/]/g')>&1) #Escape path for use with sed
– user2428118
May 10 '16 at 12:57





Here is a bash function for converting paths for use with SED: function sedPath sed 's/]/]/g')>&1) #Escape path for use with sed
– user2428118
May 10 '16 at 12:57













See also: Which characters need to be escaped in Bash? How do we know it?
– codeforester
Feb 28 '18 at 7:02




See also: Which characters need to be escaped in Bash? How do we know it?
– codeforester
Feb 28 '18 at 7:02












Dura lex, sed sed
– Nemo
May 11 '18 at 19:56




Dura lex, sed sed
– Nemo
May 11 '18 at 19:56










3 Answers
3






active

oldest

votes


















247














There are two levels of interpretation here: the shell, and sed.



In the shell, everything between single quotes is interpreted literally, except for single quotes themselves. You can effectively have a single quote between single quotes by writing ''' (close single quote, one literal single quote, open single quote).



Sed uses basic regular expressions. In a BRE, in order to have them treated literally, the characters $.*[^ need to be quoted by preceding them by a backslash, except inside character sets ([…]). Letters, digits and ()+?| must not be quoted (you can get away with quoting some of these in some implementations). The sequences (, ), n, and in some implementations , , +, ?, | and other backslash+alphanumerics have special meanings. You can get away with not quoting $^ in some positions in some implementations.



Furthermore, you need a backslash before / if it is to appear in the regex outside of bracket expressions. You can choose an alternative character as the delimiter by writing, e.g., s~/dir~/replacement~ or ~/dir~p; you'll need a backslash before the delimiter if you want to include it in the BRE. If you choose a character that has a special meaning in a BRE and you want to include it literally, you'll need three backslashes; I do not recommend this, as it may behave differently in some implementations.



In a nutshell, for sed 's/…/…/':



  • Write the regex between single quotes.

  • Use ''' to end up with a single quote in the regex.

  • Put a backslash before $.*/[]^ and only those characters (but not inside bracket expressions). (Technically you shouldn't put a backslash before ] but I don't know of an implementation that treats ] and ] differently outside of bracket expressions.)

  • Inside a bracket expression, for - to be treated literally, make sure it is first or last ([abc-] or [-abc], not [a-bc]).

  • Inside a bracket expression, for ^ to be treated literally, make sure it is not first (use [abc^], not [^abc]).

  • To include ] in the list of characters matched by a bracket expression, make it the first character (or first after ^ for a negated set): abc] or [^]abc] (not [abc]] nor [abc]]).

In the replacement text:




  • & and need to be quoted by preceding them by a backslash,
    as do the delimiter (usually /) and newlines.


  • followed by a digit has a special meaning. followed by a letter has a special meaning (special characters) in some implementations, and followed by some other character means c or c depending on the implementation.

  • With single quotes around the argument (sed 's/…/…/'), use ''' to put a single quote in the replacement text.

If the regex or replacement text comes from a shell variable, remember that



  • The regex is a BRE, not a literal string.

  • In the regex, a newline needs to be expressed as n (which will never match unless you have other sed code adding newline characters to the pattern space). But note that it won't work inside bracket expressions with some sed implementations.

  • In the replacement text, &, and newlines need to be quoted.

  • The delimiter needs to be quoted (but not inside bracket expressions).

  • Use double quotes for interpolation: sed -e "s/$BRE/$REPL/".





share|improve this answer






























    41














    The problem you're experiencing isn't due to shell interpolating and escapes - it's because you're attempting to use extended regular expression syntax without passing sed the -r or --regexp-extended option.



    Change your sed line from



    sed 's/(127.0.1.1)s/1/' [some file]


    to



    sed -r 's/(127.0.1.1)s/1/' [some file]


    and it will work as I believe you intend.



    By default sed uses uses basic regular expressions (think grep style), which would require the following syntax:



    sed 's/(127.0.1.1)[ t]/1/' [some file]





    share|improve this answer




















    • I had this problem again, and forgot to scroll down to find the solution I upvoted last time. Thanks again.
      – isaaclw
      Apr 4 '14 at 20:17










    • Thanks a lot. Adding -r as an option was what was necessary in my case.
      – HelloGoodbye
      May 21 '15 at 8:23


















    14














    Unless you want to interpolate a shell variable into the sed expression, use single quotes for the whole expression because they cause everything between them to be interpreted as-is, including backslashes.



    So if you want sed to see s/(127.0.1.1)s/1/ put single quotes around it and the shell won't touch the parentheses or backslashes in it. If you need to interpolate a shell variable, put only that part in double quotes. E.g.



    sed 's/(127.0.1.1)/'"$ip"'/'


    This will save you the trouble of remembering which shell metacharacters are not escaped by double quotes.






    share|improve this answer




















    • I want sed to see s/(127.0.1.1)/..., but putting that in a shell script as-is doesn't work. What you're saying about the shell not touching the parentheses seems wrong. I've edited my question to elaborate.
      – detly
      Feb 28 '12 at 6:14






    • 3




      The shell isn't touching the parentheses. You need the backslases because sed needs to see them. sed 's/(127.0.1.1)/IP 1/' fails because sed needs to see ( and ) for group syntax, not ( and ).
      – Kyle Jones
      Feb 28 '12 at 6:31










    • facepalm It's not in the man page, but it IS in some online manual I found. Is this normal for regex, because I've never had to use it in regex libraries (in, eg. Python)?
      – detly
      Feb 28 '12 at 6:33







    • 3




      For traditional Unix commands, there are basic regular expressions and extended regular expressions. Details. sed uses basic regular expressions, so the backslashes are needed for group syntax. Perl and Python went beyond even extended regular expressions. While I was poking around I found an extremely informative chart that illustrates what a confusing bramble we conjure up when we glibly say "regular expression."
      – Kyle Jones
      Feb 28 '12 at 7:07






    • 1




      I would also add that the only character that cannot be used inside single quotes is a single quote.
      – enzotib
      Feb 28 '12 at 9:08










    Your Answer








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

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

    else
    createEditor();

    );

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



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f32907%2fwhat-characters-do-i-need-to-escape-when-using-sed-in-a-sh-script%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    247














    There are two levels of interpretation here: the shell, and sed.



    In the shell, everything between single quotes is interpreted literally, except for single quotes themselves. You can effectively have a single quote between single quotes by writing ''' (close single quote, one literal single quote, open single quote).



    Sed uses basic regular expressions. In a BRE, in order to have them treated literally, the characters $.*[^ need to be quoted by preceding them by a backslash, except inside character sets ([…]). Letters, digits and ()+?| must not be quoted (you can get away with quoting some of these in some implementations). The sequences (, ), n, and in some implementations , , +, ?, | and other backslash+alphanumerics have special meanings. You can get away with not quoting $^ in some positions in some implementations.



    Furthermore, you need a backslash before / if it is to appear in the regex outside of bracket expressions. You can choose an alternative character as the delimiter by writing, e.g., s~/dir~/replacement~ or ~/dir~p; you'll need a backslash before the delimiter if you want to include it in the BRE. If you choose a character that has a special meaning in a BRE and you want to include it literally, you'll need three backslashes; I do not recommend this, as it may behave differently in some implementations.



    In a nutshell, for sed 's/…/…/':



    • Write the regex between single quotes.

    • Use ''' to end up with a single quote in the regex.

    • Put a backslash before $.*/[]^ and only those characters (but not inside bracket expressions). (Technically you shouldn't put a backslash before ] but I don't know of an implementation that treats ] and ] differently outside of bracket expressions.)

    • Inside a bracket expression, for - to be treated literally, make sure it is first or last ([abc-] or [-abc], not [a-bc]).

    • Inside a bracket expression, for ^ to be treated literally, make sure it is not first (use [abc^], not [^abc]).

    • To include ] in the list of characters matched by a bracket expression, make it the first character (or first after ^ for a negated set): abc] or [^]abc] (not [abc]] nor [abc]]).

    In the replacement text:




    • & and need to be quoted by preceding them by a backslash,
      as do the delimiter (usually /) and newlines.


    • followed by a digit has a special meaning. followed by a letter has a special meaning (special characters) in some implementations, and followed by some other character means c or c depending on the implementation.

    • With single quotes around the argument (sed 's/…/…/'), use ''' to put a single quote in the replacement text.

    If the regex or replacement text comes from a shell variable, remember that



    • The regex is a BRE, not a literal string.

    • In the regex, a newline needs to be expressed as n (which will never match unless you have other sed code adding newline characters to the pattern space). But note that it won't work inside bracket expressions with some sed implementations.

    • In the replacement text, &, and newlines need to be quoted.

    • The delimiter needs to be quoted (but not inside bracket expressions).

    • Use double quotes for interpolation: sed -e "s/$BRE/$REPL/".





    share|improve this answer



























      247














      There are two levels of interpretation here: the shell, and sed.



      In the shell, everything between single quotes is interpreted literally, except for single quotes themselves. You can effectively have a single quote between single quotes by writing ''' (close single quote, one literal single quote, open single quote).



      Sed uses basic regular expressions. In a BRE, in order to have them treated literally, the characters $.*[^ need to be quoted by preceding them by a backslash, except inside character sets ([…]). Letters, digits and ()+?| must not be quoted (you can get away with quoting some of these in some implementations). The sequences (, ), n, and in some implementations , , +, ?, | and other backslash+alphanumerics have special meanings. You can get away with not quoting $^ in some positions in some implementations.



      Furthermore, you need a backslash before / if it is to appear in the regex outside of bracket expressions. You can choose an alternative character as the delimiter by writing, e.g., s~/dir~/replacement~ or ~/dir~p; you'll need a backslash before the delimiter if you want to include it in the BRE. If you choose a character that has a special meaning in a BRE and you want to include it literally, you'll need three backslashes; I do not recommend this, as it may behave differently in some implementations.



      In a nutshell, for sed 's/…/…/':



      • Write the regex between single quotes.

      • Use ''' to end up with a single quote in the regex.

      • Put a backslash before $.*/[]^ and only those characters (but not inside bracket expressions). (Technically you shouldn't put a backslash before ] but I don't know of an implementation that treats ] and ] differently outside of bracket expressions.)

      • Inside a bracket expression, for - to be treated literally, make sure it is first or last ([abc-] or [-abc], not [a-bc]).

      • Inside a bracket expression, for ^ to be treated literally, make sure it is not first (use [abc^], not [^abc]).

      • To include ] in the list of characters matched by a bracket expression, make it the first character (or first after ^ for a negated set): abc] or [^]abc] (not [abc]] nor [abc]]).

      In the replacement text:




      • & and need to be quoted by preceding them by a backslash,
        as do the delimiter (usually /) and newlines.


      • followed by a digit has a special meaning. followed by a letter has a special meaning (special characters) in some implementations, and followed by some other character means c or c depending on the implementation.

      • With single quotes around the argument (sed 's/…/…/'), use ''' to put a single quote in the replacement text.

      If the regex or replacement text comes from a shell variable, remember that



      • The regex is a BRE, not a literal string.

      • In the regex, a newline needs to be expressed as n (which will never match unless you have other sed code adding newline characters to the pattern space). But note that it won't work inside bracket expressions with some sed implementations.

      • In the replacement text, &, and newlines need to be quoted.

      • The delimiter needs to be quoted (but not inside bracket expressions).

      • Use double quotes for interpolation: sed -e "s/$BRE/$REPL/".





      share|improve this answer

























        247












        247








        247






        There are two levels of interpretation here: the shell, and sed.



        In the shell, everything between single quotes is interpreted literally, except for single quotes themselves. You can effectively have a single quote between single quotes by writing ''' (close single quote, one literal single quote, open single quote).



        Sed uses basic regular expressions. In a BRE, in order to have them treated literally, the characters $.*[^ need to be quoted by preceding them by a backslash, except inside character sets ([…]). Letters, digits and ()+?| must not be quoted (you can get away with quoting some of these in some implementations). The sequences (, ), n, and in some implementations , , +, ?, | and other backslash+alphanumerics have special meanings. You can get away with not quoting $^ in some positions in some implementations.



        Furthermore, you need a backslash before / if it is to appear in the regex outside of bracket expressions. You can choose an alternative character as the delimiter by writing, e.g., s~/dir~/replacement~ or ~/dir~p; you'll need a backslash before the delimiter if you want to include it in the BRE. If you choose a character that has a special meaning in a BRE and you want to include it literally, you'll need three backslashes; I do not recommend this, as it may behave differently in some implementations.



        In a nutshell, for sed 's/…/…/':



        • Write the regex between single quotes.

        • Use ''' to end up with a single quote in the regex.

        • Put a backslash before $.*/[]^ and only those characters (but not inside bracket expressions). (Technically you shouldn't put a backslash before ] but I don't know of an implementation that treats ] and ] differently outside of bracket expressions.)

        • Inside a bracket expression, for - to be treated literally, make sure it is first or last ([abc-] or [-abc], not [a-bc]).

        • Inside a bracket expression, for ^ to be treated literally, make sure it is not first (use [abc^], not [^abc]).

        • To include ] in the list of characters matched by a bracket expression, make it the first character (or first after ^ for a negated set): abc] or [^]abc] (not [abc]] nor [abc]]).

        In the replacement text:




        • & and need to be quoted by preceding them by a backslash,
          as do the delimiter (usually /) and newlines.


        • followed by a digit has a special meaning. followed by a letter has a special meaning (special characters) in some implementations, and followed by some other character means c or c depending on the implementation.

        • With single quotes around the argument (sed 's/…/…/'), use ''' to put a single quote in the replacement text.

        If the regex or replacement text comes from a shell variable, remember that



        • The regex is a BRE, not a literal string.

        • In the regex, a newline needs to be expressed as n (which will never match unless you have other sed code adding newline characters to the pattern space). But note that it won't work inside bracket expressions with some sed implementations.

        • In the replacement text, &, and newlines need to be quoted.

        • The delimiter needs to be quoted (but not inside bracket expressions).

        • Use double quotes for interpolation: sed -e "s/$BRE/$REPL/".





        share|improve this answer














        There are two levels of interpretation here: the shell, and sed.



        In the shell, everything between single quotes is interpreted literally, except for single quotes themselves. You can effectively have a single quote between single quotes by writing ''' (close single quote, one literal single quote, open single quote).



        Sed uses basic regular expressions. In a BRE, in order to have them treated literally, the characters $.*[^ need to be quoted by preceding them by a backslash, except inside character sets ([…]). Letters, digits and ()+?| must not be quoted (you can get away with quoting some of these in some implementations). The sequences (, ), n, and in some implementations , , +, ?, | and other backslash+alphanumerics have special meanings. You can get away with not quoting $^ in some positions in some implementations.



        Furthermore, you need a backslash before / if it is to appear in the regex outside of bracket expressions. You can choose an alternative character as the delimiter by writing, e.g., s~/dir~/replacement~ or ~/dir~p; you'll need a backslash before the delimiter if you want to include it in the BRE. If you choose a character that has a special meaning in a BRE and you want to include it literally, you'll need three backslashes; I do not recommend this, as it may behave differently in some implementations.



        In a nutshell, for sed 's/…/…/':



        • Write the regex between single quotes.

        • Use ''' to end up with a single quote in the regex.

        • Put a backslash before $.*/[]^ and only those characters (but not inside bracket expressions). (Technically you shouldn't put a backslash before ] but I don't know of an implementation that treats ] and ] differently outside of bracket expressions.)

        • Inside a bracket expression, for - to be treated literally, make sure it is first or last ([abc-] or [-abc], not [a-bc]).

        • Inside a bracket expression, for ^ to be treated literally, make sure it is not first (use [abc^], not [^abc]).

        • To include ] in the list of characters matched by a bracket expression, make it the first character (or first after ^ for a negated set): abc] or [^]abc] (not [abc]] nor [abc]]).

        In the replacement text:




        • & and need to be quoted by preceding them by a backslash,
          as do the delimiter (usually /) and newlines.


        • followed by a digit has a special meaning. followed by a letter has a special meaning (special characters) in some implementations, and followed by some other character means c or c depending on the implementation.

        • With single quotes around the argument (sed 's/…/…/'), use ''' to put a single quote in the replacement text.

        If the regex or replacement text comes from a shell variable, remember that



        • The regex is a BRE, not a literal string.

        • In the regex, a newline needs to be expressed as n (which will never match unless you have other sed code adding newline characters to the pattern space). But note that it won't work inside bracket expressions with some sed implementations.

        • In the replacement text, &, and newlines need to be quoted.

        • The delimiter needs to be quoted (but not inside bracket expressions).

        • Use double quotes for interpolation: sed -e "s/$BRE/$REPL/".






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 26 '18 at 10:41

























        answered Feb 29 '12 at 1:06









        GillesGilles

        530k12810611588




        530k12810611588























            41














            The problem you're experiencing isn't due to shell interpolating and escapes - it's because you're attempting to use extended regular expression syntax without passing sed the -r or --regexp-extended option.



            Change your sed line from



            sed 's/(127.0.1.1)s/1/' [some file]


            to



            sed -r 's/(127.0.1.1)s/1/' [some file]


            and it will work as I believe you intend.



            By default sed uses uses basic regular expressions (think grep style), which would require the following syntax:



            sed 's/(127.0.1.1)[ t]/1/' [some file]





            share|improve this answer




















            • I had this problem again, and forgot to scroll down to find the solution I upvoted last time. Thanks again.
              – isaaclw
              Apr 4 '14 at 20:17










            • Thanks a lot. Adding -r as an option was what was necessary in my case.
              – HelloGoodbye
              May 21 '15 at 8:23















            41














            The problem you're experiencing isn't due to shell interpolating and escapes - it's because you're attempting to use extended regular expression syntax without passing sed the -r or --regexp-extended option.



            Change your sed line from



            sed 's/(127.0.1.1)s/1/' [some file]


            to



            sed -r 's/(127.0.1.1)s/1/' [some file]


            and it will work as I believe you intend.



            By default sed uses uses basic regular expressions (think grep style), which would require the following syntax:



            sed 's/(127.0.1.1)[ t]/1/' [some file]





            share|improve this answer




















            • I had this problem again, and forgot to scroll down to find the solution I upvoted last time. Thanks again.
              – isaaclw
              Apr 4 '14 at 20:17










            • Thanks a lot. Adding -r as an option was what was necessary in my case.
              – HelloGoodbye
              May 21 '15 at 8:23













            41












            41








            41






            The problem you're experiencing isn't due to shell interpolating and escapes - it's because you're attempting to use extended regular expression syntax without passing sed the -r or --regexp-extended option.



            Change your sed line from



            sed 's/(127.0.1.1)s/1/' [some file]


            to



            sed -r 's/(127.0.1.1)s/1/' [some file]


            and it will work as I believe you intend.



            By default sed uses uses basic regular expressions (think grep style), which would require the following syntax:



            sed 's/(127.0.1.1)[ t]/1/' [some file]





            share|improve this answer












            The problem you're experiencing isn't due to shell interpolating and escapes - it's because you're attempting to use extended regular expression syntax without passing sed the -r or --regexp-extended option.



            Change your sed line from



            sed 's/(127.0.1.1)s/1/' [some file]


            to



            sed -r 's/(127.0.1.1)s/1/' [some file]


            and it will work as I believe you intend.



            By default sed uses uses basic regular expressions (think grep style), which would require the following syntax:



            sed 's/(127.0.1.1)[ t]/1/' [some file]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 29 '12 at 2:56









            R PerrinR Perrin

            2,051108




            2,051108











            • I had this problem again, and forgot to scroll down to find the solution I upvoted last time. Thanks again.
              – isaaclw
              Apr 4 '14 at 20:17










            • Thanks a lot. Adding -r as an option was what was necessary in my case.
              – HelloGoodbye
              May 21 '15 at 8:23
















            • I had this problem again, and forgot to scroll down to find the solution I upvoted last time. Thanks again.
              – isaaclw
              Apr 4 '14 at 20:17










            • Thanks a lot. Adding -r as an option was what was necessary in my case.
              – HelloGoodbye
              May 21 '15 at 8:23















            I had this problem again, and forgot to scroll down to find the solution I upvoted last time. Thanks again.
            – isaaclw
            Apr 4 '14 at 20:17




            I had this problem again, and forgot to scroll down to find the solution I upvoted last time. Thanks again.
            – isaaclw
            Apr 4 '14 at 20:17












            Thanks a lot. Adding -r as an option was what was necessary in my case.
            – HelloGoodbye
            May 21 '15 at 8:23




            Thanks a lot. Adding -r as an option was what was necessary in my case.
            – HelloGoodbye
            May 21 '15 at 8:23











            14














            Unless you want to interpolate a shell variable into the sed expression, use single quotes for the whole expression because they cause everything between them to be interpreted as-is, including backslashes.



            So if you want sed to see s/(127.0.1.1)s/1/ put single quotes around it and the shell won't touch the parentheses or backslashes in it. If you need to interpolate a shell variable, put only that part in double quotes. E.g.



            sed 's/(127.0.1.1)/'"$ip"'/'


            This will save you the trouble of remembering which shell metacharacters are not escaped by double quotes.






            share|improve this answer




















            • I want sed to see s/(127.0.1.1)/..., but putting that in a shell script as-is doesn't work. What you're saying about the shell not touching the parentheses seems wrong. I've edited my question to elaborate.
              – detly
              Feb 28 '12 at 6:14






            • 3




              The shell isn't touching the parentheses. You need the backslases because sed needs to see them. sed 's/(127.0.1.1)/IP 1/' fails because sed needs to see ( and ) for group syntax, not ( and ).
              – Kyle Jones
              Feb 28 '12 at 6:31










            • facepalm It's not in the man page, but it IS in some online manual I found. Is this normal for regex, because I've never had to use it in regex libraries (in, eg. Python)?
              – detly
              Feb 28 '12 at 6:33







            • 3




              For traditional Unix commands, there are basic regular expressions and extended regular expressions. Details. sed uses basic regular expressions, so the backslashes are needed for group syntax. Perl and Python went beyond even extended regular expressions. While I was poking around I found an extremely informative chart that illustrates what a confusing bramble we conjure up when we glibly say "regular expression."
              – Kyle Jones
              Feb 28 '12 at 7:07






            • 1




              I would also add that the only character that cannot be used inside single quotes is a single quote.
              – enzotib
              Feb 28 '12 at 9:08















            14














            Unless you want to interpolate a shell variable into the sed expression, use single quotes for the whole expression because they cause everything between them to be interpreted as-is, including backslashes.



            So if you want sed to see s/(127.0.1.1)s/1/ put single quotes around it and the shell won't touch the parentheses or backslashes in it. If you need to interpolate a shell variable, put only that part in double quotes. E.g.



            sed 's/(127.0.1.1)/'"$ip"'/'


            This will save you the trouble of remembering which shell metacharacters are not escaped by double quotes.






            share|improve this answer




















            • I want sed to see s/(127.0.1.1)/..., but putting that in a shell script as-is doesn't work. What you're saying about the shell not touching the parentheses seems wrong. I've edited my question to elaborate.
              – detly
              Feb 28 '12 at 6:14






            • 3




              The shell isn't touching the parentheses. You need the backslases because sed needs to see them. sed 's/(127.0.1.1)/IP 1/' fails because sed needs to see ( and ) for group syntax, not ( and ).
              – Kyle Jones
              Feb 28 '12 at 6:31










            • facepalm It's not in the man page, but it IS in some online manual I found. Is this normal for regex, because I've never had to use it in regex libraries (in, eg. Python)?
              – detly
              Feb 28 '12 at 6:33







            • 3




              For traditional Unix commands, there are basic regular expressions and extended regular expressions. Details. sed uses basic regular expressions, so the backslashes are needed for group syntax. Perl and Python went beyond even extended regular expressions. While I was poking around I found an extremely informative chart that illustrates what a confusing bramble we conjure up when we glibly say "regular expression."
              – Kyle Jones
              Feb 28 '12 at 7:07






            • 1




              I would also add that the only character that cannot be used inside single quotes is a single quote.
              – enzotib
              Feb 28 '12 at 9:08













            14












            14








            14






            Unless you want to interpolate a shell variable into the sed expression, use single quotes for the whole expression because they cause everything between them to be interpreted as-is, including backslashes.



            So if you want sed to see s/(127.0.1.1)s/1/ put single quotes around it and the shell won't touch the parentheses or backslashes in it. If you need to interpolate a shell variable, put only that part in double quotes. E.g.



            sed 's/(127.0.1.1)/'"$ip"'/'


            This will save you the trouble of remembering which shell metacharacters are not escaped by double quotes.






            share|improve this answer












            Unless you want to interpolate a shell variable into the sed expression, use single quotes for the whole expression because they cause everything between them to be interpreted as-is, including backslashes.



            So if you want sed to see s/(127.0.1.1)s/1/ put single quotes around it and the shell won't touch the parentheses or backslashes in it. If you need to interpolate a shell variable, put only that part in double quotes. E.g.



            sed 's/(127.0.1.1)/'"$ip"'/'


            This will save you the trouble of remembering which shell metacharacters are not escaped by double quotes.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 28 '12 at 5:58









            Kyle JonesKyle Jones

            11.6k13049




            11.6k13049











            • I want sed to see s/(127.0.1.1)/..., but putting that in a shell script as-is doesn't work. What you're saying about the shell not touching the parentheses seems wrong. I've edited my question to elaborate.
              – detly
              Feb 28 '12 at 6:14






            • 3




              The shell isn't touching the parentheses. You need the backslases because sed needs to see them. sed 's/(127.0.1.1)/IP 1/' fails because sed needs to see ( and ) for group syntax, not ( and ).
              – Kyle Jones
              Feb 28 '12 at 6:31










            • facepalm It's not in the man page, but it IS in some online manual I found. Is this normal for regex, because I've never had to use it in regex libraries (in, eg. Python)?
              – detly
              Feb 28 '12 at 6:33







            • 3




              For traditional Unix commands, there are basic regular expressions and extended regular expressions. Details. sed uses basic regular expressions, so the backslashes are needed for group syntax. Perl and Python went beyond even extended regular expressions. While I was poking around I found an extremely informative chart that illustrates what a confusing bramble we conjure up when we glibly say "regular expression."
              – Kyle Jones
              Feb 28 '12 at 7:07






            • 1




              I would also add that the only character that cannot be used inside single quotes is a single quote.
              – enzotib
              Feb 28 '12 at 9:08
















            • I want sed to see s/(127.0.1.1)/..., but putting that in a shell script as-is doesn't work. What you're saying about the shell not touching the parentheses seems wrong. I've edited my question to elaborate.
              – detly
              Feb 28 '12 at 6:14






            • 3




              The shell isn't touching the parentheses. You need the backslases because sed needs to see them. sed 's/(127.0.1.1)/IP 1/' fails because sed needs to see ( and ) for group syntax, not ( and ).
              – Kyle Jones
              Feb 28 '12 at 6:31










            • facepalm It's not in the man page, but it IS in some online manual I found. Is this normal for regex, because I've never had to use it in regex libraries (in, eg. Python)?
              – detly
              Feb 28 '12 at 6:33







            • 3




              For traditional Unix commands, there are basic regular expressions and extended regular expressions. Details. sed uses basic regular expressions, so the backslashes are needed for group syntax. Perl and Python went beyond even extended regular expressions. While I was poking around I found an extremely informative chart that illustrates what a confusing bramble we conjure up when we glibly say "regular expression."
              – Kyle Jones
              Feb 28 '12 at 7:07






            • 1




              I would also add that the only character that cannot be used inside single quotes is a single quote.
              – enzotib
              Feb 28 '12 at 9:08















            I want sed to see s/(127.0.1.1)/..., but putting that in a shell script as-is doesn't work. What you're saying about the shell not touching the parentheses seems wrong. I've edited my question to elaborate.
            – detly
            Feb 28 '12 at 6:14




            I want sed to see s/(127.0.1.1)/..., but putting that in a shell script as-is doesn't work. What you're saying about the shell not touching the parentheses seems wrong. I've edited my question to elaborate.
            – detly
            Feb 28 '12 at 6:14




            3




            3




            The shell isn't touching the parentheses. You need the backslases because sed needs to see them. sed 's/(127.0.1.1)/IP 1/' fails because sed needs to see ( and ) for group syntax, not ( and ).
            – Kyle Jones
            Feb 28 '12 at 6:31




            The shell isn't touching the parentheses. You need the backslases because sed needs to see them. sed 's/(127.0.1.1)/IP 1/' fails because sed needs to see ( and ) for group syntax, not ( and ).
            – Kyle Jones
            Feb 28 '12 at 6:31












            facepalm It's not in the man page, but it IS in some online manual I found. Is this normal for regex, because I've never had to use it in regex libraries (in, eg. Python)?
            – detly
            Feb 28 '12 at 6:33





            facepalm It's not in the man page, but it IS in some online manual I found. Is this normal for regex, because I've never had to use it in regex libraries (in, eg. Python)?
            – detly
            Feb 28 '12 at 6:33





            3




            3




            For traditional Unix commands, there are basic regular expressions and extended regular expressions. Details. sed uses basic regular expressions, so the backslashes are needed for group syntax. Perl and Python went beyond even extended regular expressions. While I was poking around I found an extremely informative chart that illustrates what a confusing bramble we conjure up when we glibly say "regular expression."
            – Kyle Jones
            Feb 28 '12 at 7:07




            For traditional Unix commands, there are basic regular expressions and extended regular expressions. Details. sed uses basic regular expressions, so the backslashes are needed for group syntax. Perl and Python went beyond even extended regular expressions. While I was poking around I found an extremely informative chart that illustrates what a confusing bramble we conjure up when we glibly say "regular expression."
            – Kyle Jones
            Feb 28 '12 at 7:07




            1




            1




            I would also add that the only character that cannot be used inside single quotes is a single quote.
            – enzotib
            Feb 28 '12 at 9:08




            I would also add that the only character that cannot be used inside single quotes is a single quote.
            – enzotib
            Feb 28 '12 at 9:08

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


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

            But avoid


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

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

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





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


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

            But avoid


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

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

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




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f32907%2fwhat-characters-do-i-need-to-escape-when-using-sed-in-a-sh-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay