How can I use variables in the LHS and RHS of a sed substitution?

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











up vote
56
down vote

favorite
13












I want to do:



cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh


in my program.



But I want to use variables, e.g.



old_run='old_name_952'
new_run='old_name_953'


I have tried using them but the substitution doesn't happen (no error).
I have tried:



cat update_via_sed.sh | sed 's/old_run/new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'









share|improve this question



















  • 2




    You can find the answer in Use a parameter in a command argument.
    – manatwork
    Mar 25 '13 at 16:00














up vote
56
down vote

favorite
13












I want to do:



cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh


in my program.



But I want to use variables, e.g.



old_run='old_name_952'
new_run='old_name_953'


I have tried using them but the substitution doesn't happen (no error).
I have tried:



cat update_via_sed.sh | sed 's/old_run/new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'









share|improve this question



















  • 2




    You can find the answer in Use a parameter in a command argument.
    – manatwork
    Mar 25 '13 at 16:00












up vote
56
down vote

favorite
13









up vote
56
down vote

favorite
13






13





I want to do:



cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh


in my program.



But I want to use variables, e.g.



old_run='old_name_952'
new_run='old_name_953'


I have tried using them but the substitution doesn't happen (no error).
I have tried:



cat update_via_sed.sh | sed 's/old_run/new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'









share|improve this question















I want to do:



cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh


in my program.



But I want to use variables, e.g.



old_run='old_name_952'
new_run='old_name_953'


I have tried using them but the substitution doesn't happen (no error).
I have tried:



cat update_via_sed.sh | sed 's/old_run/new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'
cat update_via_sed.sh | sed 's/$old_run/$new_run/'






sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 9 at 10:23









don_crissti

47.2k15125155




47.2k15125155










asked Mar 25 '13 at 15:57









Michael Durrant

15.1k39108176




15.1k39108176







  • 2




    You can find the answer in Use a parameter in a command argument.
    – manatwork
    Mar 25 '13 at 16:00












  • 2




    You can find the answer in Use a parameter in a command argument.
    – manatwork
    Mar 25 '13 at 16:00







2




2




You can find the answer in Use a parameter in a command argument.
– manatwork
Mar 25 '13 at 16:00




You can find the answer in Use a parameter in a command argument.
– manatwork
Mar 25 '13 at 16:00










11 Answers
11






active

oldest

votes

















up vote
40
down vote













You could do:



sed "s/$old_run/$new_run/" < infile > outfile


But beware that $old_run would be taken as a regular expression and so any special characters that the variable contains, such as / or . would have to be escaped. Similarly, in $new_run, the & and characters would need to be treated specially and you would have to escape the / and newline characters in it.



If the content of $old_run or $new_run is not under your control, then it's critical to perform that escaping, or otherwise that code amounts to a code injection vulnerability.






share|improve this answer


















  • 3




    I was using single quote in sed params, switched to double quotes and it worked file. Awesome.
    – Aakash
    Jul 28 '16 at 8:30










  • it isn't that sed alone will not use regex but sed -E would ?
    – Kiwy
    Apr 24 at 10:04











  • @Kiwy, no. -e is to specify a sed expression, so you can pass more than one (as in sed -e exp1 -e exp2) or combinations of files and expressions (sed -f file -e exp). You may be confusing with -E which with some implementations, tells sed to use EREs instead of BREs.
    – Stéphane Chazelas
    Apr 24 at 10:06











  • I mistyped, but OK, that explain why i have so many issues using sed without -E I only master the extended regex not the regular one. Thanks for the explanaiton.
    – Kiwy
    Apr 24 at 10:12










  • @Kiwy, see also the linked Q&A for more options supported by some sed implementations for yet more regular expression syntaxes.
    – Stéphane Chazelas
    Apr 24 at 10:18


















up vote
23
down vote













This worked:



cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/'


As I want to 'reuse' the same file I actually use this for anyone wishing a similar approach:



cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/' > new_update; mv -f new_update update_via_sed.sh


The above created a new file then deletes the current file than rename the new file to be the current file.






share|improve this answer


















  • 3




    You don't need cat, mv, or extra '' unless you're going to have special characters in them. So it's sed -i s/"$old"/"$new"/ update_via_sed.sh. However be aware that this does not work for any value of old and new. e.g. it must not contain / etc., as $old is still a search and $new a replacement pattern where some characters have special meanings.
    – frostschutz
    Mar 25 '13 at 16:25






  • 1




    sed -i "s/$old/$new/" is ok too (with the above precautions). sed usually considers the separator to be the first character after the s command - i.e. if your variables contain slashes / but not lets say at (@), you can use "s@$old@$new@".
    – peterph
    Mar 25 '13 at 17:23

















up vote
18
down vote













You can use variables in sed as long as it's in a double quote (not a single quote):



sed "s/$var/r_str/g" file_name >new_file


If you have a forward slash (/) in the variable then use different separator like below



sed "s|$var|r_str|g" file_name >new_file





share|improve this answer


















  • 1




    +1 for mentioning needing to use double quotes. That was my problem.
    – speckledcarp
    Nov 16 '16 at 17:01










  • Exactly what I was looking for, including the use of | as in my case, the variables contains a path so it has / in it
    – yorch
    Nov 17 '16 at 21:51

















up vote
16
down vote



accepted










'in-place' sed (usng the -i flag) was the answer. Thanks to peterph.



sed -i "s@$old@$new@" file





share|improve this answer


















  • 1




    You have the quotes in the wrong place. quotes must be around variables, not s@ (which is not special to the shell in any way). Best is to put everything inside quotes like sed -i "s@$old@$new@" file. Note that -i is not a standard sed option.
    – Stéphane Chazelas
    Mar 26 '13 at 14:20










  • how can I escape $ in this command. Like, I am going to change $xxx as a whole to the value of a variable $JAVA_HOME. I tried sed -i "s@$xxx@$JAVA_HOME@" file, but error says no previous regular expression
    – hakunami
    Nov 6 '14 at 8:07

















up vote
3
down vote













man bash gives this about single quoting




Enclosing characters in single quotes preserves the literal
value of each character within the quotes. A single quote
may not occur between single quotes, even when preceded by a
backslash.




Whatever you type on the command line, bash interprets it and then it sends the result to the program it is supposed to be sent to.In this case, if you use sed 's/$old_run/$new_run/', bash first sees the sed, it recognises it as an executable present in $PATH variable. The sed executable requires an input. Bash looks for the input and finds 's/$old_run/$new_run/'. Single quotes say bash not to interpret the content in them and pass them as they are. So, bash then passes them to sed. Sed gives an error because $ can occur only at the end of line.



Instead if we use double quotes, i.e., "s/$old_run/$new_run/", then bash sees this and interprets $old_run as a variable name and makes a substitution (this phase is called variable expansion). This is indeed what we required.



But, you have to be careful using double quotes because, they are interpreted first by bash and then given to sed. So, some symbols like ` must be escaped before using them.






share|improve this answer



























    up vote
    1
    down vote













    At risk of being flippant - have you considered perl.



    Which - amongst other things - is quite good at doing 'sed style' operations, but also a more fully featured programming thingummy.



    It looks like in your example, what you're actually trying to do is increment a number.



    So how about:



    #!/usr/bin/env perl
    use strict;
    use warnings 'all';

    while ( <DATA> )
    s/old_name_(d+)/"old_name_".($1+1)/e;
    print;


    __DATA__
    old_name_932


    or as a one liner - sed style:



    perl -pe 's/old_name_(d+)/"old_name_".($1+1)/e'





    share|improve this answer



























      up vote
      1
      down vote













      $ echo $d1 | sed -i 's/zkserver1/'$d1'/g' deva_file
      sed: -e expression #1, char 26: unterminated `s' command


      In $d1 I am storing values



      I cannot replace the value from the variable, so I tried the following command it is working



      echo $d1 | sed -i 's/zkserver1/'"$d1"'/g' deva_file


      missing double quotation mark in "$d1", that was the error






      share|improve this answer





























        up vote
        -2
        down vote













        Assumed $old_run and $new_run are already set:



        cat update_via_sed.sh | eval $(print "sed 's/$old_run/$new_run/g'")


        This shows the change on screen. You can redirect output to file if needed.






        share|improve this answer





























          up vote
          -2
          down vote













          For using a variable in sed , use double quotes "" , to enclose the variable , in the pattern that you are using.
          For eg:
          a="Hi"
          If you want to append variable 'a' to a pattern, you can use the below:
          sed -n "1,/pattern"$a"pattern/p" filename






          share|improve this answer
















          • 1




            The expansion of $a is unquoted here, which means that any spaces in its value will invoke word splitting in the shell.
            – Kusalananda
            Jun 22 at 7:47

















          up vote
          -2
          down vote













          You can also use Perl:



          perl -pi -e ""s/$val1/$val2/g"" file





          share|improve this answer






















          • Consider variables that may have spaces in their values. The empty strings around the Perl expression ("") won't do anything.
            – Kusalananda
            Jun 22 at 7:25


















          up vote
          -2
          down vote













          break the string



          bad => linux see a string $old_run :



          sed 's/$old_run/$new_run/'


          good => linux see a variable $old_run:



          sed 's/'$old_run'/'$new_run'/'





          share|improve this answer


















          • 1




            And if $old_run or $new_run contains spaces?
            – Kusalananda
            Jun 22 at 7:20










          Your Answer







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

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

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f69112%2fhow-can-i-use-variables-in-the-lhs-and-rhs-of-a-sed-substitution%23new-answer', 'question_page');

          );

          Post as a guest






























          11 Answers
          11






          active

          oldest

          votes








          11 Answers
          11






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          40
          down vote













          You could do:



          sed "s/$old_run/$new_run/" < infile > outfile


          But beware that $old_run would be taken as a regular expression and so any special characters that the variable contains, such as / or . would have to be escaped. Similarly, in $new_run, the & and characters would need to be treated specially and you would have to escape the / and newline characters in it.



          If the content of $old_run or $new_run is not under your control, then it's critical to perform that escaping, or otherwise that code amounts to a code injection vulnerability.






          share|improve this answer


















          • 3




            I was using single quote in sed params, switched to double quotes and it worked file. Awesome.
            – Aakash
            Jul 28 '16 at 8:30










          • it isn't that sed alone will not use regex but sed -E would ?
            – Kiwy
            Apr 24 at 10:04











          • @Kiwy, no. -e is to specify a sed expression, so you can pass more than one (as in sed -e exp1 -e exp2) or combinations of files and expressions (sed -f file -e exp). You may be confusing with -E which with some implementations, tells sed to use EREs instead of BREs.
            – Stéphane Chazelas
            Apr 24 at 10:06











          • I mistyped, but OK, that explain why i have so many issues using sed without -E I only master the extended regex not the regular one. Thanks for the explanaiton.
            – Kiwy
            Apr 24 at 10:12










          • @Kiwy, see also the linked Q&A for more options supported by some sed implementations for yet more regular expression syntaxes.
            – Stéphane Chazelas
            Apr 24 at 10:18















          up vote
          40
          down vote













          You could do:



          sed "s/$old_run/$new_run/" < infile > outfile


          But beware that $old_run would be taken as a regular expression and so any special characters that the variable contains, such as / or . would have to be escaped. Similarly, in $new_run, the & and characters would need to be treated specially and you would have to escape the / and newline characters in it.



          If the content of $old_run or $new_run is not under your control, then it's critical to perform that escaping, or otherwise that code amounts to a code injection vulnerability.






          share|improve this answer


















          • 3




            I was using single quote in sed params, switched to double quotes and it worked file. Awesome.
            – Aakash
            Jul 28 '16 at 8:30










          • it isn't that sed alone will not use regex but sed -E would ?
            – Kiwy
            Apr 24 at 10:04











          • @Kiwy, no. -e is to specify a sed expression, so you can pass more than one (as in sed -e exp1 -e exp2) or combinations of files and expressions (sed -f file -e exp). You may be confusing with -E which with some implementations, tells sed to use EREs instead of BREs.
            – Stéphane Chazelas
            Apr 24 at 10:06











          • I mistyped, but OK, that explain why i have so many issues using sed without -E I only master the extended regex not the regular one. Thanks for the explanaiton.
            – Kiwy
            Apr 24 at 10:12










          • @Kiwy, see also the linked Q&A for more options supported by some sed implementations for yet more regular expression syntaxes.
            – Stéphane Chazelas
            Apr 24 at 10:18













          up vote
          40
          down vote










          up vote
          40
          down vote









          You could do:



          sed "s/$old_run/$new_run/" < infile > outfile


          But beware that $old_run would be taken as a regular expression and so any special characters that the variable contains, such as / or . would have to be escaped. Similarly, in $new_run, the & and characters would need to be treated specially and you would have to escape the / and newline characters in it.



          If the content of $old_run or $new_run is not under your control, then it's critical to perform that escaping, or otherwise that code amounts to a code injection vulnerability.






          share|improve this answer














          You could do:



          sed "s/$old_run/$new_run/" < infile > outfile


          But beware that $old_run would be taken as a regular expression and so any special characters that the variable contains, such as / or . would have to be escaped. Similarly, in $new_run, the & and characters would need to be treated specially and you would have to escape the / and newline characters in it.



          If the content of $old_run or $new_run is not under your control, then it's critical to perform that escaping, or otherwise that code amounts to a code injection vulnerability.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 21 '17 at 8:18

























          answered Mar 25 '13 at 20:09









          Stéphane Chazelas

          285k53525864




          285k53525864







          • 3




            I was using single quote in sed params, switched to double quotes and it worked file. Awesome.
            – Aakash
            Jul 28 '16 at 8:30










          • it isn't that sed alone will not use regex but sed -E would ?
            – Kiwy
            Apr 24 at 10:04











          • @Kiwy, no. -e is to specify a sed expression, so you can pass more than one (as in sed -e exp1 -e exp2) or combinations of files and expressions (sed -f file -e exp). You may be confusing with -E which with some implementations, tells sed to use EREs instead of BREs.
            – Stéphane Chazelas
            Apr 24 at 10:06











          • I mistyped, but OK, that explain why i have so many issues using sed without -E I only master the extended regex not the regular one. Thanks for the explanaiton.
            – Kiwy
            Apr 24 at 10:12










          • @Kiwy, see also the linked Q&A for more options supported by some sed implementations for yet more regular expression syntaxes.
            – Stéphane Chazelas
            Apr 24 at 10:18













          • 3




            I was using single quote in sed params, switched to double quotes and it worked file. Awesome.
            – Aakash
            Jul 28 '16 at 8:30










          • it isn't that sed alone will not use regex but sed -E would ?
            – Kiwy
            Apr 24 at 10:04











          • @Kiwy, no. -e is to specify a sed expression, so you can pass more than one (as in sed -e exp1 -e exp2) or combinations of files and expressions (sed -f file -e exp). You may be confusing with -E which with some implementations, tells sed to use EREs instead of BREs.
            – Stéphane Chazelas
            Apr 24 at 10:06











          • I mistyped, but OK, that explain why i have so many issues using sed without -E I only master the extended regex not the regular one. Thanks for the explanaiton.
            – Kiwy
            Apr 24 at 10:12










          • @Kiwy, see also the linked Q&A for more options supported by some sed implementations for yet more regular expression syntaxes.
            – Stéphane Chazelas
            Apr 24 at 10:18








          3




          3




          I was using single quote in sed params, switched to double quotes and it worked file. Awesome.
          – Aakash
          Jul 28 '16 at 8:30




          I was using single quote in sed params, switched to double quotes and it worked file. Awesome.
          – Aakash
          Jul 28 '16 at 8:30












          it isn't that sed alone will not use regex but sed -E would ?
          – Kiwy
          Apr 24 at 10:04





          it isn't that sed alone will not use regex but sed -E would ?
          – Kiwy
          Apr 24 at 10:04













          @Kiwy, no. -e is to specify a sed expression, so you can pass more than one (as in sed -e exp1 -e exp2) or combinations of files and expressions (sed -f file -e exp). You may be confusing with -E which with some implementations, tells sed to use EREs instead of BREs.
          – Stéphane Chazelas
          Apr 24 at 10:06





          @Kiwy, no. -e is to specify a sed expression, so you can pass more than one (as in sed -e exp1 -e exp2) or combinations of files and expressions (sed -f file -e exp). You may be confusing with -E which with some implementations, tells sed to use EREs instead of BREs.
          – Stéphane Chazelas
          Apr 24 at 10:06













          I mistyped, but OK, that explain why i have so many issues using sed without -E I only master the extended regex not the regular one. Thanks for the explanaiton.
          – Kiwy
          Apr 24 at 10:12




          I mistyped, but OK, that explain why i have so many issues using sed without -E I only master the extended regex not the regular one. Thanks for the explanaiton.
          – Kiwy
          Apr 24 at 10:12












          @Kiwy, see also the linked Q&A for more options supported by some sed implementations for yet more regular expression syntaxes.
          – Stéphane Chazelas
          Apr 24 at 10:18





          @Kiwy, see also the linked Q&A for more options supported by some sed implementations for yet more regular expression syntaxes.
          – Stéphane Chazelas
          Apr 24 at 10:18













          up vote
          23
          down vote













          This worked:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/'


          As I want to 'reuse' the same file I actually use this for anyone wishing a similar approach:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/' > new_update; mv -f new_update update_via_sed.sh


          The above created a new file then deletes the current file than rename the new file to be the current file.






          share|improve this answer


















          • 3




            You don't need cat, mv, or extra '' unless you're going to have special characters in them. So it's sed -i s/"$old"/"$new"/ update_via_sed.sh. However be aware that this does not work for any value of old and new. e.g. it must not contain / etc., as $old is still a search and $new a replacement pattern where some characters have special meanings.
            – frostschutz
            Mar 25 '13 at 16:25






          • 1




            sed -i "s/$old/$new/" is ok too (with the above precautions). sed usually considers the separator to be the first character after the s command - i.e. if your variables contain slashes / but not lets say at (@), you can use "s@$old@$new@".
            – peterph
            Mar 25 '13 at 17:23














          up vote
          23
          down vote













          This worked:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/'


          As I want to 'reuse' the same file I actually use this for anyone wishing a similar approach:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/' > new_update; mv -f new_update update_via_sed.sh


          The above created a new file then deletes the current file than rename the new file to be the current file.






          share|improve this answer


















          • 3




            You don't need cat, mv, or extra '' unless you're going to have special characters in them. So it's sed -i s/"$old"/"$new"/ update_via_sed.sh. However be aware that this does not work for any value of old and new. e.g. it must not contain / etc., as $old is still a search and $new a replacement pattern where some characters have special meanings.
            – frostschutz
            Mar 25 '13 at 16:25






          • 1




            sed -i "s/$old/$new/" is ok too (with the above precautions). sed usually considers the separator to be the first character after the s command - i.e. if your variables contain slashes / but not lets say at (@), you can use "s@$old@$new@".
            – peterph
            Mar 25 '13 at 17:23












          up vote
          23
          down vote










          up vote
          23
          down vote









          This worked:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/'


          As I want to 'reuse' the same file I actually use this for anyone wishing a similar approach:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/' > new_update; mv -f new_update update_via_sed.sh


          The above created a new file then deletes the current file than rename the new file to be the current file.






          share|improve this answer














          This worked:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/'


          As I want to 'reuse' the same file I actually use this for anyone wishing a similar approach:



          cat update_via_sed.sh | sed 's/'"$old_run"'/'"$new_run"'/' > new_update; mv -f new_update update_via_sed.sh


          The above created a new file then deletes the current file than rename the new file to be the current file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 '13 at 16:18

























          answered Mar 25 '13 at 16:01









          Michael Durrant

          15.1k39108176




          15.1k39108176







          • 3




            You don't need cat, mv, or extra '' unless you're going to have special characters in them. So it's sed -i s/"$old"/"$new"/ update_via_sed.sh. However be aware that this does not work for any value of old and new. e.g. it must not contain / etc., as $old is still a search and $new a replacement pattern where some characters have special meanings.
            – frostschutz
            Mar 25 '13 at 16:25






          • 1




            sed -i "s/$old/$new/" is ok too (with the above precautions). sed usually considers the separator to be the first character after the s command - i.e. if your variables contain slashes / but not lets say at (@), you can use "s@$old@$new@".
            – peterph
            Mar 25 '13 at 17:23












          • 3




            You don't need cat, mv, or extra '' unless you're going to have special characters in them. So it's sed -i s/"$old"/"$new"/ update_via_sed.sh. However be aware that this does not work for any value of old and new. e.g. it must not contain / etc., as $old is still a search and $new a replacement pattern where some characters have special meanings.
            – frostschutz
            Mar 25 '13 at 16:25






          • 1




            sed -i "s/$old/$new/" is ok too (with the above precautions). sed usually considers the separator to be the first character after the s command - i.e. if your variables contain slashes / but not lets say at (@), you can use "s@$old@$new@".
            – peterph
            Mar 25 '13 at 17:23







          3




          3




          You don't need cat, mv, or extra '' unless you're going to have special characters in them. So it's sed -i s/"$old"/"$new"/ update_via_sed.sh. However be aware that this does not work for any value of old and new. e.g. it must not contain / etc., as $old is still a search and $new a replacement pattern where some characters have special meanings.
          – frostschutz
          Mar 25 '13 at 16:25




          You don't need cat, mv, or extra '' unless you're going to have special characters in them. So it's sed -i s/"$old"/"$new"/ update_via_sed.sh. However be aware that this does not work for any value of old and new. e.g. it must not contain / etc., as $old is still a search and $new a replacement pattern where some characters have special meanings.
          – frostschutz
          Mar 25 '13 at 16:25




          1




          1




          sed -i "s/$old/$new/" is ok too (with the above precautions). sed usually considers the separator to be the first character after the s command - i.e. if your variables contain slashes / but not lets say at (@), you can use "s@$old@$new@".
          – peterph
          Mar 25 '13 at 17:23




          sed -i "s/$old/$new/" is ok too (with the above precautions). sed usually considers the separator to be the first character after the s command - i.e. if your variables contain slashes / but not lets say at (@), you can use "s@$old@$new@".
          – peterph
          Mar 25 '13 at 17:23










          up vote
          18
          down vote













          You can use variables in sed as long as it's in a double quote (not a single quote):



          sed "s/$var/r_str/g" file_name >new_file


          If you have a forward slash (/) in the variable then use different separator like below



          sed "s|$var|r_str|g" file_name >new_file





          share|improve this answer


















          • 1




            +1 for mentioning needing to use double quotes. That was my problem.
            – speckledcarp
            Nov 16 '16 at 17:01










          • Exactly what I was looking for, including the use of | as in my case, the variables contains a path so it has / in it
            – yorch
            Nov 17 '16 at 21:51














          up vote
          18
          down vote













          You can use variables in sed as long as it's in a double quote (not a single quote):



          sed "s/$var/r_str/g" file_name >new_file


          If you have a forward slash (/) in the variable then use different separator like below



          sed "s|$var|r_str|g" file_name >new_file





          share|improve this answer


















          • 1




            +1 for mentioning needing to use double quotes. That was my problem.
            – speckledcarp
            Nov 16 '16 at 17:01










          • Exactly what I was looking for, including the use of | as in my case, the variables contains a path so it has / in it
            – yorch
            Nov 17 '16 at 21:51












          up vote
          18
          down vote










          up vote
          18
          down vote









          You can use variables in sed as long as it's in a double quote (not a single quote):



          sed "s/$var/r_str/g" file_name >new_file


          If you have a forward slash (/) in the variable then use different separator like below



          sed "s|$var|r_str|g" file_name >new_file





          share|improve this answer














          You can use variables in sed as long as it's in a double quote (not a single quote):



          sed "s/$var/r_str/g" file_name >new_file


          If you have a forward slash (/) in the variable then use different separator like below



          sed "s|$var|r_str|g" file_name >new_file






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 7 '14 at 16:03









          EightBitTony

          15.6k34454




          15.6k34454










          answered Aug 7 '14 at 15:31









          mani

          18112




          18112







          • 1




            +1 for mentioning needing to use double quotes. That was my problem.
            – speckledcarp
            Nov 16 '16 at 17:01










          • Exactly what I was looking for, including the use of | as in my case, the variables contains a path so it has / in it
            – yorch
            Nov 17 '16 at 21:51












          • 1




            +1 for mentioning needing to use double quotes. That was my problem.
            – speckledcarp
            Nov 16 '16 at 17:01










          • Exactly what I was looking for, including the use of | as in my case, the variables contains a path so it has / in it
            – yorch
            Nov 17 '16 at 21:51







          1




          1




          +1 for mentioning needing to use double quotes. That was my problem.
          – speckledcarp
          Nov 16 '16 at 17:01




          +1 for mentioning needing to use double quotes. That was my problem.
          – speckledcarp
          Nov 16 '16 at 17:01












          Exactly what I was looking for, including the use of | as in my case, the variables contains a path so it has / in it
          – yorch
          Nov 17 '16 at 21:51




          Exactly what I was looking for, including the use of | as in my case, the variables contains a path so it has / in it
          – yorch
          Nov 17 '16 at 21:51










          up vote
          16
          down vote



          accepted










          'in-place' sed (usng the -i flag) was the answer. Thanks to peterph.



          sed -i "s@$old@$new@" file





          share|improve this answer


















          • 1




            You have the quotes in the wrong place. quotes must be around variables, not s@ (which is not special to the shell in any way). Best is to put everything inside quotes like sed -i "s@$old@$new@" file. Note that -i is not a standard sed option.
            – Stéphane Chazelas
            Mar 26 '13 at 14:20










          • how can I escape $ in this command. Like, I am going to change $xxx as a whole to the value of a variable $JAVA_HOME. I tried sed -i "s@$xxx@$JAVA_HOME@" file, but error says no previous regular expression
            – hakunami
            Nov 6 '14 at 8:07














          up vote
          16
          down vote



          accepted










          'in-place' sed (usng the -i flag) was the answer. Thanks to peterph.



          sed -i "s@$old@$new@" file





          share|improve this answer


















          • 1




            You have the quotes in the wrong place. quotes must be around variables, not s@ (which is not special to the shell in any way). Best is to put everything inside quotes like sed -i "s@$old@$new@" file. Note that -i is not a standard sed option.
            – Stéphane Chazelas
            Mar 26 '13 at 14:20










          • how can I escape $ in this command. Like, I am going to change $xxx as a whole to the value of a variable $JAVA_HOME. I tried sed -i "s@$xxx@$JAVA_HOME@" file, but error says no previous regular expression
            – hakunami
            Nov 6 '14 at 8:07












          up vote
          16
          down vote



          accepted







          up vote
          16
          down vote



          accepted






          'in-place' sed (usng the -i flag) was the answer. Thanks to peterph.



          sed -i "s@$old@$new@" file





          share|improve this answer














          'in-place' sed (usng the -i flag) was the answer. Thanks to peterph.



          sed -i "s@$old@$new@" file






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 '13 at 15:12

























          answered Mar 26 '13 at 1:12









          Michael Durrant

          15.1k39108176




          15.1k39108176







          • 1




            You have the quotes in the wrong place. quotes must be around variables, not s@ (which is not special to the shell in any way). Best is to put everything inside quotes like sed -i "s@$old@$new@" file. Note that -i is not a standard sed option.
            – Stéphane Chazelas
            Mar 26 '13 at 14:20










          • how can I escape $ in this command. Like, I am going to change $xxx as a whole to the value of a variable $JAVA_HOME. I tried sed -i "s@$xxx@$JAVA_HOME@" file, but error says no previous regular expression
            – hakunami
            Nov 6 '14 at 8:07












          • 1




            You have the quotes in the wrong place. quotes must be around variables, not s@ (which is not special to the shell in any way). Best is to put everything inside quotes like sed -i "s@$old@$new@" file. Note that -i is not a standard sed option.
            – Stéphane Chazelas
            Mar 26 '13 at 14:20










          • how can I escape $ in this command. Like, I am going to change $xxx as a whole to the value of a variable $JAVA_HOME. I tried sed -i "s@$xxx@$JAVA_HOME@" file, but error says no previous regular expression
            – hakunami
            Nov 6 '14 at 8:07







          1




          1




          You have the quotes in the wrong place. quotes must be around variables, not s@ (which is not special to the shell in any way). Best is to put everything inside quotes like sed -i "s@$old@$new@" file. Note that -i is not a standard sed option.
          – Stéphane Chazelas
          Mar 26 '13 at 14:20




          You have the quotes in the wrong place. quotes must be around variables, not s@ (which is not special to the shell in any way). Best is to put everything inside quotes like sed -i "s@$old@$new@" file. Note that -i is not a standard sed option.
          – Stéphane Chazelas
          Mar 26 '13 at 14:20












          how can I escape $ in this command. Like, I am going to change $xxx as a whole to the value of a variable $JAVA_HOME. I tried sed -i "s@$xxx@$JAVA_HOME@" file, but error says no previous regular expression
          – hakunami
          Nov 6 '14 at 8:07




          how can I escape $ in this command. Like, I am going to change $xxx as a whole to the value of a variable $JAVA_HOME. I tried sed -i "s@$xxx@$JAVA_HOME@" file, but error says no previous regular expression
          – hakunami
          Nov 6 '14 at 8:07










          up vote
          3
          down vote













          man bash gives this about single quoting




          Enclosing characters in single quotes preserves the literal
          value of each character within the quotes. A single quote
          may not occur between single quotes, even when preceded by a
          backslash.




          Whatever you type on the command line, bash interprets it and then it sends the result to the program it is supposed to be sent to.In this case, if you use sed 's/$old_run/$new_run/', bash first sees the sed, it recognises it as an executable present in $PATH variable. The sed executable requires an input. Bash looks for the input and finds 's/$old_run/$new_run/'. Single quotes say bash not to interpret the content in them and pass them as they are. So, bash then passes them to sed. Sed gives an error because $ can occur only at the end of line.



          Instead if we use double quotes, i.e., "s/$old_run/$new_run/", then bash sees this and interprets $old_run as a variable name and makes a substitution (this phase is called variable expansion). This is indeed what we required.



          But, you have to be careful using double quotes because, they are interpreted first by bash and then given to sed. So, some symbols like ` must be escaped before using them.






          share|improve this answer
























            up vote
            3
            down vote













            man bash gives this about single quoting




            Enclosing characters in single quotes preserves the literal
            value of each character within the quotes. A single quote
            may not occur between single quotes, even when preceded by a
            backslash.




            Whatever you type on the command line, bash interprets it and then it sends the result to the program it is supposed to be sent to.In this case, if you use sed 's/$old_run/$new_run/', bash first sees the sed, it recognises it as an executable present in $PATH variable. The sed executable requires an input. Bash looks for the input and finds 's/$old_run/$new_run/'. Single quotes say bash not to interpret the content in them and pass them as they are. So, bash then passes them to sed. Sed gives an error because $ can occur only at the end of line.



            Instead if we use double quotes, i.e., "s/$old_run/$new_run/", then bash sees this and interprets $old_run as a variable name and makes a substitution (this phase is called variable expansion). This is indeed what we required.



            But, you have to be careful using double quotes because, they are interpreted first by bash and then given to sed. So, some symbols like ` must be escaped before using them.






            share|improve this answer






















              up vote
              3
              down vote










              up vote
              3
              down vote









              man bash gives this about single quoting




              Enclosing characters in single quotes preserves the literal
              value of each character within the quotes. A single quote
              may not occur between single quotes, even when preceded by a
              backslash.




              Whatever you type on the command line, bash interprets it and then it sends the result to the program it is supposed to be sent to.In this case, if you use sed 's/$old_run/$new_run/', bash first sees the sed, it recognises it as an executable present in $PATH variable. The sed executable requires an input. Bash looks for the input and finds 's/$old_run/$new_run/'. Single quotes say bash not to interpret the content in them and pass them as they are. So, bash then passes them to sed. Sed gives an error because $ can occur only at the end of line.



              Instead if we use double quotes, i.e., "s/$old_run/$new_run/", then bash sees this and interprets $old_run as a variable name and makes a substitution (this phase is called variable expansion). This is indeed what we required.



              But, you have to be careful using double quotes because, they are interpreted first by bash and then given to sed. So, some symbols like ` must be escaped before using them.






              share|improve this answer












              man bash gives this about single quoting




              Enclosing characters in single quotes preserves the literal
              value of each character within the quotes. A single quote
              may not occur between single quotes, even when preceded by a
              backslash.




              Whatever you type on the command line, bash interprets it and then it sends the result to the program it is supposed to be sent to.In this case, if you use sed 's/$old_run/$new_run/', bash first sees the sed, it recognises it as an executable present in $PATH variable. The sed executable requires an input. Bash looks for the input and finds 's/$old_run/$new_run/'. Single quotes say bash not to interpret the content in them and pass them as they are. So, bash then passes them to sed. Sed gives an error because $ can occur only at the end of line.



              Instead if we use double quotes, i.e., "s/$old_run/$new_run/", then bash sees this and interprets $old_run as a variable name and makes a substitution (this phase is called variable expansion). This is indeed what we required.



              But, you have to be careful using double quotes because, they are interpreted first by bash and then given to sed. So, some symbols like ` must be escaped before using them.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 27 '14 at 19:18









              nitishch

              40629




              40629




















                  up vote
                  1
                  down vote













                  At risk of being flippant - have you considered perl.



                  Which - amongst other things - is quite good at doing 'sed style' operations, but also a more fully featured programming thingummy.



                  It looks like in your example, what you're actually trying to do is increment a number.



                  So how about:



                  #!/usr/bin/env perl
                  use strict;
                  use warnings 'all';

                  while ( <DATA> )
                  s/old_name_(d+)/"old_name_".($1+1)/e;
                  print;


                  __DATA__
                  old_name_932


                  or as a one liner - sed style:



                  perl -pe 's/old_name_(d+)/"old_name_".($1+1)/e'





                  share|improve this answer
























                    up vote
                    1
                    down vote













                    At risk of being flippant - have you considered perl.



                    Which - amongst other things - is quite good at doing 'sed style' operations, but also a more fully featured programming thingummy.



                    It looks like in your example, what you're actually trying to do is increment a number.



                    So how about:



                    #!/usr/bin/env perl
                    use strict;
                    use warnings 'all';

                    while ( <DATA> )
                    s/old_name_(d+)/"old_name_".($1+1)/e;
                    print;


                    __DATA__
                    old_name_932


                    or as a one liner - sed style:



                    perl -pe 's/old_name_(d+)/"old_name_".($1+1)/e'





                    share|improve this answer






















                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      At risk of being flippant - have you considered perl.



                      Which - amongst other things - is quite good at doing 'sed style' operations, but also a more fully featured programming thingummy.



                      It looks like in your example, what you're actually trying to do is increment a number.



                      So how about:



                      #!/usr/bin/env perl
                      use strict;
                      use warnings 'all';

                      while ( <DATA> )
                      s/old_name_(d+)/"old_name_".($1+1)/e;
                      print;


                      __DATA__
                      old_name_932


                      or as a one liner - sed style:



                      perl -pe 's/old_name_(d+)/"old_name_".($1+1)/e'





                      share|improve this answer












                      At risk of being flippant - have you considered perl.



                      Which - amongst other things - is quite good at doing 'sed style' operations, but also a more fully featured programming thingummy.



                      It looks like in your example, what you're actually trying to do is increment a number.



                      So how about:



                      #!/usr/bin/env perl
                      use strict;
                      use warnings 'all';

                      while ( <DATA> )
                      s/old_name_(d+)/"old_name_".($1+1)/e;
                      print;


                      __DATA__
                      old_name_932


                      or as a one liner - sed style:



                      perl -pe 's/old_name_(d+)/"old_name_".($1+1)/e'






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 28 '16 at 12:26









                      Sobrique

                      3,749517




                      3,749517




















                          up vote
                          1
                          down vote













                          $ echo $d1 | sed -i 's/zkserver1/'$d1'/g' deva_file
                          sed: -e expression #1, char 26: unterminated `s' command


                          In $d1 I am storing values



                          I cannot replace the value from the variable, so I tried the following command it is working



                          echo $d1 | sed -i 's/zkserver1/'"$d1"'/g' deva_file


                          missing double quotation mark in "$d1", that was the error






                          share|improve this answer


























                            up vote
                            1
                            down vote













                            $ echo $d1 | sed -i 's/zkserver1/'$d1'/g' deva_file
                            sed: -e expression #1, char 26: unterminated `s' command


                            In $d1 I am storing values



                            I cannot replace the value from the variable, so I tried the following command it is working



                            echo $d1 | sed -i 's/zkserver1/'"$d1"'/g' deva_file


                            missing double quotation mark in "$d1", that was the error






                            share|improve this answer
























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              $ echo $d1 | sed -i 's/zkserver1/'$d1'/g' deva_file
                              sed: -e expression #1, char 26: unterminated `s' command


                              In $d1 I am storing values



                              I cannot replace the value from the variable, so I tried the following command it is working



                              echo $d1 | sed -i 's/zkserver1/'"$d1"'/g' deva_file


                              missing double quotation mark in "$d1", that was the error






                              share|improve this answer














                              $ echo $d1 | sed -i 's/zkserver1/'$d1'/g' deva_file
                              sed: -e expression #1, char 26: unterminated `s' command


                              In $d1 I am storing values



                              I cannot replace the value from the variable, so I tried the following command it is working



                              echo $d1 | sed -i 's/zkserver1/'"$d1"'/g' deva_file


                              missing double quotation mark in "$d1", that was the error







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 9 '16 at 15:00









                              7ochem

                              141110




                              141110










                              answered Oct 20 '16 at 11:14









                              deva

                              112




                              112




















                                  up vote
                                  -2
                                  down vote













                                  Assumed $old_run and $new_run are already set:



                                  cat update_via_sed.sh | eval $(print "sed 's/$old_run/$new_run/g'")


                                  This shows the change on screen. You can redirect output to file if needed.






                                  share|improve this answer


























                                    up vote
                                    -2
                                    down vote













                                    Assumed $old_run and $new_run are already set:



                                    cat update_via_sed.sh | eval $(print "sed 's/$old_run/$new_run/g'")


                                    This shows the change on screen. You can redirect output to file if needed.






                                    share|improve this answer
























                                      up vote
                                      -2
                                      down vote










                                      up vote
                                      -2
                                      down vote









                                      Assumed $old_run and $new_run are already set:



                                      cat update_via_sed.sh | eval $(print "sed 's/$old_run/$new_run/g'")


                                      This shows the change on screen. You can redirect output to file if needed.






                                      share|improve this answer














                                      Assumed $old_run and $new_run are already set:



                                      cat update_via_sed.sh | eval $(print "sed 's/$old_run/$new_run/g'")


                                      This shows the change on screen. You can redirect output to file if needed.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Feb 27 '14 at 17:35









                                      jasonwryan

                                      47.3k14127178




                                      47.3k14127178










                                      answered Feb 27 '14 at 17:17









                                      Orlando

                                      1




                                      1




















                                          up vote
                                          -2
                                          down vote













                                          For using a variable in sed , use double quotes "" , to enclose the variable , in the pattern that you are using.
                                          For eg:
                                          a="Hi"
                                          If you want to append variable 'a' to a pattern, you can use the below:
                                          sed -n "1,/pattern"$a"pattern/p" filename






                                          share|improve this answer
















                                          • 1




                                            The expansion of $a is unquoted here, which means that any spaces in its value will invoke word splitting in the shell.
                                            – Kusalananda
                                            Jun 22 at 7:47














                                          up vote
                                          -2
                                          down vote













                                          For using a variable in sed , use double quotes "" , to enclose the variable , in the pattern that you are using.
                                          For eg:
                                          a="Hi"
                                          If you want to append variable 'a' to a pattern, you can use the below:
                                          sed -n "1,/pattern"$a"pattern/p" filename






                                          share|improve this answer
















                                          • 1




                                            The expansion of $a is unquoted here, which means that any spaces in its value will invoke word splitting in the shell.
                                            – Kusalananda
                                            Jun 22 at 7:47












                                          up vote
                                          -2
                                          down vote










                                          up vote
                                          -2
                                          down vote









                                          For using a variable in sed , use double quotes "" , to enclose the variable , in the pattern that you are using.
                                          For eg:
                                          a="Hi"
                                          If you want to append variable 'a' to a pattern, you can use the below:
                                          sed -n "1,/pattern"$a"pattern/p" filename






                                          share|improve this answer












                                          For using a variable in sed , use double quotes "" , to enclose the variable , in the pattern that you are using.
                                          For eg:
                                          a="Hi"
                                          If you want to append variable 'a' to a pattern, you can use the below:
                                          sed -n "1,/pattern"$a"pattern/p" filename







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jul 21 '17 at 7:03









                                          Sher

                                          1




                                          1







                                          • 1




                                            The expansion of $a is unquoted here, which means that any spaces in its value will invoke word splitting in the shell.
                                            – Kusalananda
                                            Jun 22 at 7:47












                                          • 1




                                            The expansion of $a is unquoted here, which means that any spaces in its value will invoke word splitting in the shell.
                                            – Kusalananda
                                            Jun 22 at 7:47







                                          1




                                          1




                                          The expansion of $a is unquoted here, which means that any spaces in its value will invoke word splitting in the shell.
                                          – Kusalananda
                                          Jun 22 at 7:47




                                          The expansion of $a is unquoted here, which means that any spaces in its value will invoke word splitting in the shell.
                                          – Kusalananda
                                          Jun 22 at 7:47










                                          up vote
                                          -2
                                          down vote













                                          You can also use Perl:



                                          perl -pi -e ""s/$val1/$val2/g"" file





                                          share|improve this answer






















                                          • Consider variables that may have spaces in their values. The empty strings around the Perl expression ("") won't do anything.
                                            – Kusalananda
                                            Jun 22 at 7:25















                                          up vote
                                          -2
                                          down vote













                                          You can also use Perl:



                                          perl -pi -e ""s/$val1/$val2/g"" file





                                          share|improve this answer






















                                          • Consider variables that may have spaces in their values. The empty strings around the Perl expression ("") won't do anything.
                                            – Kusalananda
                                            Jun 22 at 7:25













                                          up vote
                                          -2
                                          down vote










                                          up vote
                                          -2
                                          down vote









                                          You can also use Perl:



                                          perl -pi -e ""s/$val1/$val2/g"" file





                                          share|improve this answer














                                          You can also use Perl:



                                          perl -pi -e ""s/$val1/$val2/g"" file






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jun 22 at 7:25









                                          Kusalananda

                                          106k14209329




                                          106k14209329










                                          answered Jun 22 at 7:09









                                          Samba

                                          1




                                          1











                                          • Consider variables that may have spaces in their values. The empty strings around the Perl expression ("") won't do anything.
                                            – Kusalananda
                                            Jun 22 at 7:25

















                                          • Consider variables that may have spaces in their values. The empty strings around the Perl expression ("") won't do anything.
                                            – Kusalananda
                                            Jun 22 at 7:25
















                                          Consider variables that may have spaces in their values. The empty strings around the Perl expression ("") won't do anything.
                                          – Kusalananda
                                          Jun 22 at 7:25





                                          Consider variables that may have spaces in their values. The empty strings around the Perl expression ("") won't do anything.
                                          – Kusalananda
                                          Jun 22 at 7:25











                                          up vote
                                          -2
                                          down vote













                                          break the string



                                          bad => linux see a string $old_run :



                                          sed 's/$old_run/$new_run/'


                                          good => linux see a variable $old_run:



                                          sed 's/'$old_run'/'$new_run'/'





                                          share|improve this answer


















                                          • 1




                                            And if $old_run or $new_run contains spaces?
                                            – Kusalananda
                                            Jun 22 at 7:20














                                          up vote
                                          -2
                                          down vote













                                          break the string



                                          bad => linux see a string $old_run :



                                          sed 's/$old_run/$new_run/'


                                          good => linux see a variable $old_run:



                                          sed 's/'$old_run'/'$new_run'/'





                                          share|improve this answer


















                                          • 1




                                            And if $old_run or $new_run contains spaces?
                                            – Kusalananda
                                            Jun 22 at 7:20












                                          up vote
                                          -2
                                          down vote










                                          up vote
                                          -2
                                          down vote









                                          break the string



                                          bad => linux see a string $old_run :



                                          sed 's/$old_run/$new_run/'


                                          good => linux see a variable $old_run:



                                          sed 's/'$old_run'/'$new_run'/'





                                          share|improve this answer














                                          break the string



                                          bad => linux see a string $old_run :



                                          sed 's/$old_run/$new_run/'


                                          good => linux see a variable $old_run:



                                          sed 's/'$old_run'/'$new_run'/'






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Aug 17 at 10:03

























                                          answered Sep 11 '17 at 22:38









                                          marcdahan

                                          992




                                          992







                                          • 1




                                            And if $old_run or $new_run contains spaces?
                                            – Kusalananda
                                            Jun 22 at 7:20












                                          • 1




                                            And if $old_run or $new_run contains spaces?
                                            – Kusalananda
                                            Jun 22 at 7:20







                                          1




                                          1




                                          And if $old_run or $new_run contains spaces?
                                          – Kusalananda
                                          Jun 22 at 7:20




                                          And if $old_run or $new_run contains spaces?
                                          – Kusalananda
                                          Jun 22 at 7:20

















                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f69112%2fhow-can-i-use-variables-in-the-lhs-and-rhs-of-a-sed-substitution%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          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