1

I am trying to replace a word in a text file with the current username. I am trying to do this with "sed" using $USER, but it keeps actually writing "$USER" in the text file instead of my username.

sed -i 's/test/$USER/g' ~/.gtkrc-2.0

Can anyone help? Thank you in advance.

heemayl
  • 91,753
ovine
  • 109

1 Answers1

1

Variables are not expanded when put inside single quotes. Use double quotes instead:

sed -i "s/test/"$USER"/g" ~/.gtkrc-2.0

Also it is a good idea to take a backup while modifying file in place:

sed -i.bak "s/test/"$USER"/g" ~/.gtkrc-2.0

The original file will be kept as ~/.gtkrc-2.0.bak, the modified one will be ~/.gtkrc-2.0.

heemayl
  • 91,753
  • Silly me! I tried it with double quotes earlier, but I didn't realize I needed quotes around the variable as well. Thank you for this answer, I hope it helps others too. – ovine Dec 29 '15 at 01:21