Why does a sed command work interactively but not in my script? [duplicate]

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











up vote
1
down vote

favorite
1













This question already has an answer here:



  • How can I use variables when doing a sed?

    11 answers



  • Why does my shell script choke on whitespace or other special characters?

    4 answers



  • Difference between ' and " on command line (bash)?

    2 answers



SED command not replacing in bash under Debian, but works in command line.



Works in command line:



sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml


Not working in bash script, does not replace the string:



from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/$from"
path_to="/dev/disk/by-label/$to"
echo "sed -i 's|$path_from|$path_to|g' $file"
sed -i 's|$path_from|$path_to|g' $file


Why doesn't replace string in file when using sed in bash?







share|improve this question














marked as duplicate by cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56


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.


















    up vote
    1
    down vote

    favorite
    1













    This question already has an answer here:



    • How can I use variables when doing a sed?

      11 answers



    • Why does my shell script choke on whitespace or other special characters?

      4 answers



    • Difference between ' and " on command line (bash)?

      2 answers



    SED command not replacing in bash under Debian, but works in command line.



    Works in command line:



    sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml


    Not working in bash script, does not replace the string:



    from="SR6D4"
    to="SR4D4"
    path_from="/dev/disk/by-label/$from"
    path_to="/dev/disk/by-label/$to"
    echo "sed -i 's|$path_from|$path_to|g' $file"
    sed -i 's|$path_from|$path_to|g' $file


    Why doesn't replace string in file when using sed in bash?







    share|improve this question














    marked as duplicate by cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56


    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.
















      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1






      This question already has an answer here:



      • How can I use variables when doing a sed?

        11 answers



      • Why does my shell script choke on whitespace or other special characters?

        4 answers



      • Difference between ' and " on command line (bash)?

        2 answers



      SED command not replacing in bash under Debian, but works in command line.



      Works in command line:



      sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml


      Not working in bash script, does not replace the string:



      from="SR6D4"
      to="SR4D4"
      path_from="/dev/disk/by-label/$from"
      path_to="/dev/disk/by-label/$to"
      echo "sed -i 's|$path_from|$path_to|g' $file"
      sed -i 's|$path_from|$path_to|g' $file


      Why doesn't replace string in file when using sed in bash?







      share|improve this question















      This question already has an answer here:



      • How can I use variables when doing a sed?

        11 answers



      • Why does my shell script choke on whitespace or other special characters?

        4 answers



      • Difference between ' and " on command line (bash)?

        2 answers



      SED command not replacing in bash under Debian, but works in command line.



      Works in command line:



      sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml


      Not working in bash script, does not replace the string:



      from="SR6D4"
      to="SR4D4"
      path_from="/dev/disk/by-label/$from"
      path_to="/dev/disk/by-label/$to"
      echo "sed -i 's|$path_from|$path_to|g' $file"
      sed -i 's|$path_from|$path_to|g' $file


      Why doesn't replace string in file when using sed in bash?





      This question already has an answer here:



      • How can I use variables when doing a sed?

        11 answers



      • Why does my shell script choke on whitespace or other special characters?

        4 answers



      • Difference between ' and " on command line (bash)?

        2 answers









      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 22 at 13:05









      JdeBP

      28.6k459134




      28.6k459134










      asked Jan 22 at 6:57









      klor

      142112




      142112




      marked as duplicate by cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56


      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 cas, Romeo Ninov, Rui F Ribeiro, don_crissti, andcoz Jan 22 at 14:56


      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.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)



          sed -i "s|$path_from|$path_to|g" "$file"


          should work better. (I've added double quotes around $file just in case your filename ever contains spaces.)






          share|improve this answer




















          • Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
            – klor
            Jan 22 at 7:08


















          up vote
          1
          down vote













          As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.



          It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.



          You could try to backup file before changing:



          cp /path/to/file,.backup
          sed -i 'place your pattern here' /path/to/file


          and you will get file with name file.backup.



          You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.



          The second safety method:



          mv /path/to/file,.backup; 
          cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file



          The next point. As bashFAQ thinks:




          Embedding shell variables in sed commands is never a good idea




          Thats why you need to use awk with -v options instead. Your script may look like this:



          from="SR6D4"
          to="SR4D4"
          path_from="/dev/disk/by-label/$from"
          path_to="/dev/disk/by-label/$to"
          sed -i 's|$path_from|$path_to|g' $file
          mv $file,.backup
          cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file





          share|improve this answer




















          • Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
            – klor
            Jan 22 at 11:00










          • @klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
            – Egor Vasilyev
            Jan 22 at 11:06










          • Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
            – klor
            Jan 22 at 11:10










          • BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
            – klor
            Jan 22 at 11:17










          • filenames can contain new line symbols or something else. This symbols must be handled safely.
            – Egor Vasilyev
            Jan 22 at 11:21

















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)



          sed -i "s|$path_from|$path_to|g" "$file"


          should work better. (I've added double quotes around $file just in case your filename ever contains spaces.)






          share|improve this answer




















          • Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
            – klor
            Jan 22 at 7:08















          up vote
          2
          down vote



          accepted










          Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)



          sed -i "s|$path_from|$path_to|g" "$file"


          should work better. (I've added double quotes around $file just in case your filename ever contains spaces.)






          share|improve this answer




















          • Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
            – klor
            Jan 22 at 7:08













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)



          sed -i "s|$path_from|$path_to|g" "$file"


          should work better. (I've added double quotes around $file just in case your filename ever contains spaces.)






          share|improve this answer












          Shell variables won't get expanded in single quotes. (It's kind of what single quotes are for.)



          sed -i "s|$path_from|$path_to|g" "$file"


          should work better. (I've added double quotes around $file just in case your filename ever contains spaces.)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 at 7:04









          Ulrich Schwarz

          8,88512643




          8,88512643











          • Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
            – klor
            Jan 22 at 7:08

















          • Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
            – klor
            Jan 22 at 7:08
















          Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
          – klor
          Jan 22 at 7:08





          Uh, very cheap error :) I did not notice, that I used simple quotes for sed :-( Of course double quotes are interpolated, while single are not. Thank you for pointing the error. Accepting as solution.
          – klor
          Jan 22 at 7:08













          up vote
          1
          down vote













          As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.



          It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.



          You could try to backup file before changing:



          cp /path/to/file,.backup
          sed -i 'place your pattern here' /path/to/file


          and you will get file with name file.backup.



          You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.



          The second safety method:



          mv /path/to/file,.backup; 
          cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file



          The next point. As bashFAQ thinks:




          Embedding shell variables in sed commands is never a good idea




          Thats why you need to use awk with -v options instead. Your script may look like this:



          from="SR6D4"
          to="SR4D4"
          path_from="/dev/disk/by-label/$from"
          path_to="/dev/disk/by-label/$to"
          sed -i 's|$path_from|$path_to|g' $file
          mv $file,.backup
          cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file





          share|improve this answer




















          • Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
            – klor
            Jan 22 at 11:00










          • @klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
            – Egor Vasilyev
            Jan 22 at 11:06










          • Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
            – klor
            Jan 22 at 11:10










          • BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
            – klor
            Jan 22 at 11:17










          • filenames can contain new line symbols or something else. This symbols must be handled safely.
            – Egor Vasilyev
            Jan 22 at 11:21














          up vote
          1
          down vote













          As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.



          It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.



          You could try to backup file before changing:



          cp /path/to/file,.backup
          sed -i 'place your pattern here' /path/to/file


          and you will get file with name file.backup.



          You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.



          The second safety method:



          mv /path/to/file,.backup; 
          cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file



          The next point. As bashFAQ thinks:




          Embedding shell variables in sed commands is never a good idea




          Thats why you need to use awk with -v options instead. Your script may look like this:



          from="SR6D4"
          to="SR4D4"
          path_from="/dev/disk/by-label/$from"
          path_to="/dev/disk/by-label/$to"
          sed -i 's|$path_from|$path_to|g' $file
          mv $file,.backup
          cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file





          share|improve this answer




















          • Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
            – klor
            Jan 22 at 11:00










          • @klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
            – Egor Vasilyev
            Jan 22 at 11:06










          • Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
            – klor
            Jan 22 at 11:10










          • BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
            – klor
            Jan 22 at 11:17










          • filenames can contain new line symbols or something else. This symbols must be handled safely.
            – Egor Vasilyev
            Jan 22 at 11:21












          up vote
          1
          down vote










          up vote
          1
          down vote









          As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.



          It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.



          You could try to backup file before changing:



          cp /path/to/file,.backup
          sed -i 'place your pattern here' /path/to/file


          and you will get file with name file.backup.



          You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.



          The second safety method:



          mv /path/to/file,.backup; 
          cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file



          The next point. As bashFAQ thinks:




          Embedding shell variables in sed commands is never a good idea




          Thats why you need to use awk with -v options instead. Your script may look like this:



          from="SR6D4"
          to="SR4D4"
          path_from="/dev/disk/by-label/$from"
          path_to="/dev/disk/by-label/$to"
          sed -i 's|$path_from|$path_to|g' $file
          mv $file,.backup
          cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file





          share|improve this answer












          As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.



          It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.



          You could try to backup file before changing:



          cp /path/to/file,.backup
          sed -i 'place your pattern here' /path/to/file


          and you will get file with name file.backup.



          You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.



          The second safety method:



          mv /path/to/file,.backup; 
          cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file



          The next point. As bashFAQ thinks:




          Embedding shell variables in sed commands is never a good idea




          Thats why you need to use awk with -v options instead. Your script may look like this:



          from="SR6D4"
          to="SR4D4"
          path_from="/dev/disk/by-label/$from"
          path_to="/dev/disk/by-label/$to"
          sed -i 's|$path_from|$path_to|g' $file
          mv $file,.backup
          cat "$file.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" 'gsub(awkfrom,awkto)' > $file






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 at 10:04









          Egor Vasilyev

          1,792129




          1,792129











          • Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
            – klor
            Jan 22 at 11:00










          • @klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
            – Egor Vasilyev
            Jan 22 at 11:06










          • Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
            – klor
            Jan 22 at 11:10










          • BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
            – klor
            Jan 22 at 11:17










          • filenames can contain new line symbols or something else. This symbols must be handled safely.
            – Egor Vasilyev
            Jan 22 at 11:21
















          • Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
            – klor
            Jan 22 at 11:00










          • @klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
            – Egor Vasilyev
            Jan 22 at 11:06










          • Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
            – klor
            Jan 22 at 11:10










          • BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
            – klor
            Jan 22 at 11:17










          • filenames can contain new line symbols or something else. This symbols must be handled safely.
            – Egor Vasilyev
            Jan 22 at 11:21















          Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
          – klor
          Jan 22 at 11:00




          Very detailed answer. But the OP was solved by the previous answer. So I select the other as solution.
          – klor
          Jan 22 at 11:00












          @klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
          – Egor Vasilyev
          Jan 22 at 11:06




          @klor, yes, of course. I am only supplemented the answer of Ulrich Schwarz
          – Egor Vasilyev
          Jan 22 at 11:06












          Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
          – klor
          Jan 22 at 11:10




          Your answer however gave me the idea to make backup of changed files. Thus I vote to your answer.
          – klor
          Jan 22 at 11:10












          BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
          – klor
          Jan 22 at 11:17




          BTW: where do I use shell variables? The script uses input arguments, but only locally, from command line. I don't see security risk here.
          – klor
          Jan 22 at 11:17












          filenames can contain new line symbols or something else. This symbols must be handled safely.
          – Egor Vasilyev
          Jan 22 at 11:21




          filenames can contain new line symbols or something else. This symbols must be handled safely.
          – Egor Vasilyev
          Jan 22 at 11:21


          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