Reputation: 1309
I am just using simple sed substitution to replace some strings in a file by reading its value from a variable
cat file.txt
root="/content/drive/Shared drives/Media Library"
# some other lines
# ...
In the above file txt , I am intending to substitute /content/drive/Shared drives/Media Library
in line 1 with the value of a variable path
being read through a bash script .
For example - If i enter /home/user
as value of variable ,the line 1 in file.txt should be modified as follows :
root="/home/user"
Here's my bash script with the sed expression
#!/usr/bin/env bash
read -e -p "Enter the path: " path
sed -i "1s/\/content\/drive\/Shared drives\/Media Library/${path}/" file.txt
When i execute the bash script and enter path as /home/user
, it returns the following error -
sed: -e expression #1, char 52: unknown option to `s'
Upvotes: 0
Views: 2989
Reputation: 59
When processing file paths with sed
, you can make things easier by using an alternate delimiter that is not a part of the values to be processed. The delimiter can be any character that follows the s
command. The forward slash /
is often seen in examples, but can be any other character like |
or #
. This makes it unnecessary to escape the /
in the values. For example:
sed -i "1s|/content/drive/Shared drives/Media Library|${path}|" file.txt
Upvotes: 0