Escaping a literal string for sed

How can escape the literal string #!/bin/bash in a sed expression? Curently I have a command in a bash script of the form

sed 's/Parametersettings:/HERE/g' outpad1 > outpad2

I would like the escaped replacement to go where HERE is written in the above.

I have tried all sorts of / \ and $variables with no luck so far.

2 Answers

$ echo 'Parametersettings:' | sed 's/Parametersettings:/#!\/bin\/bash/g'
#!/bin/bash

or use a different delimiter, avoiding the need to escape the / characters:

$ echo 'Parametersettings:' | sed 's%Parametersettings:%#!/bin/bash%g'
#!/bin/bash
0

By quoting your string with single quotes ("''"), you PREVENT interpolation by the shell. Use double quotes ("") and the shell will substitute variables, etc.

sed "s/Parametersettings:/$HERE/g" outpad1 > outpad2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like