escape certain characters in a shell variable [duplicate]

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





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








0
















This question already has an answer here:



  • How to ensure that string interpolated into `sed` substitution escapes all metachars

    1 answer



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

    3 answers



I have a shell variable which will be used within the replacement string of a sed regex.
Therefore, all characters &, / and need to be escaped by a preceeding .



I managed to do it for & and / by



newvar=`echo "$oldvar" | sed -e 's|&|\&|g' -e 's|/|\/|g'`


1st question: Is this fine (piping content to sed via echo etc.)?



2nd question: Somehow I cannot get it to work for the backslash. Can you show me how to do it?










share|improve this question













marked as duplicate by Jeff Schaller, Stéphane Chazelas sed
Users with the  sed badge can single-handedly close sed questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 13 at 13:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























    0
















    This question already has an answer here:



    • How to ensure that string interpolated into `sed` substitution escapes all metachars

      1 answer



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

      3 answers



    I have a shell variable which will be used within the replacement string of a sed regex.
    Therefore, all characters &, / and need to be escaped by a preceeding .



    I managed to do it for & and / by



    newvar=`echo "$oldvar" | sed -e 's|&|\&|g' -e 's|/|\/|g'`


    1st question: Is this fine (piping content to sed via echo etc.)?



    2nd question: Somehow I cannot get it to work for the backslash. Can you show me how to do it?










    share|improve this question













    marked as duplicate by Jeff Schaller, Stéphane Chazelas sed
    Users with the  sed badge can single-handedly close sed questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Mar 13 at 13:55


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      0












      0








      0









      This question already has an answer here:



      • How to ensure that string interpolated into `sed` substitution escapes all metachars

        1 answer



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

        3 answers



      I have a shell variable which will be used within the replacement string of a sed regex.
      Therefore, all characters &, / and need to be escaped by a preceeding .



      I managed to do it for & and / by



      newvar=`echo "$oldvar" | sed -e 's|&|\&|g' -e 's|/|\/|g'`


      1st question: Is this fine (piping content to sed via echo etc.)?



      2nd question: Somehow I cannot get it to work for the backslash. Can you show me how to do it?










      share|improve this question















      This question already has an answer here:



      • How to ensure that string interpolated into `sed` substitution escapes all metachars

        1 answer



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

        3 answers



      I have a shell variable which will be used within the replacement string of a sed regex.
      Therefore, all characters &, / and need to be escaped by a preceeding .



      I managed to do it for & and / by



      newvar=`echo "$oldvar" | sed -e 's|&|\&|g' -e 's|/|\/|g'`


      1st question: Is this fine (piping content to sed via echo etc.)?



      2nd question: Somehow I cannot get it to work for the backslash. Can you show me how to do it?





      This question already has an answer here:



      • How to ensure that string interpolated into `sed` substitution escapes all metachars

        1 answer



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

        3 answers







      shell sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 13 at 13:43









      azimutazimut

      1032




      1032




      marked as duplicate by Jeff Schaller, Stéphane Chazelas sed
      Users with the  sed badge can single-handedly close sed questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 13 at 13:55


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Jeff Schaller, Stéphane Chazelas sed
      Users with the  sed badge can single-handedly close sed questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 13 at 13:55


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          0






          active

          oldest

          votes

















          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          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