Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to keep dollar sign within sed in the bash script?

I am trying to find a way to escape the dollar sign within the sed command in a bash script. I have found here tons of answers that say that you need to put four backslashes in a row in order to escape the sign. I have also tried the version with two backslashes, but for some reason, I can’t get it to work. Can someone please tell me what I’m doing wrong?

Let’s say that you have a file called aaa.txt located in /home/Documents. The file has just only one line of text, which says aaa. I am trying to replace it using this command (please don’t tell me that I can reference it in a different way than with line numbers because this is just a reduced example of something else I am doing):

sed -i "1ccd /home/userr/$PARAMETER/$METHOD" "/home/userr/Documents/aaa.txt"

The output that I get is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cd /home/userr/\/\

Which is not what I want. I want to have exactly this output, with dollar signs in the string:

cd /home/userr/$PARAMETER/$METHOD

What is the proper form of the string passed to the sed command to achieve this?

>Solution :

You can escape the dollar sign with a backslash:

sed -i "1ccd /home/userr/\$PARAMETER/\$METHOD" "/home/userr/Documents/aaa.txt"

Documentation here: https://www.gnu.org/software/bash/manual/html_node/Escape-Character.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading