1

I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?

Here is my script currently:

currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"

sed -i -e "s/filepath/$filepathvar/g" aimparmstest

When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:

sed: -e expression #1, char 13: unknown option to `s'

Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.

Drew Pesall
  • 179
  • 3
  • 12

1 Answers1

4

You can use any character as the separator (the first character). For example:

echo "a/b/c" | sed -e 's|/|_|g'

In your case:

sed -i -e "s|filepath|$filepathvar|g" aimparmstest
Eric Ross
  • 202
  • 2
  • 5
  • Thank you! All the examples I read up on using sed had forward slash as the separator so I had no idea this was flexible. – Drew Pesall Jul 31 '20 at 19:46