As mentioned earlier you have problem with quoting. But there are some opinions about string replacement in file. See below.
It's bad idea to replace string in file because it's unsafety. There is small probability to lost file's data while changing.
You could try to backup file before changing:
cp /path/to/file{,.backup}
sed -i 'place your pattern here' /path/to/file
and you will get file with name file.backup
.
You need to remember: if your file will be damaged and you will delete and replace it - file will have different inode and will lost all hard links.
The second safety method:
mv /path/to/file{,.backup};
cat /path/to/file.backup | sed 'place your pattern here' > /path/to/file
The next point. As bashFAQ thinks:
Embedding shell variables in sed commands is never a good idea
Thats why you need to use awk
with -v
options instead. Your script may look like this:
from="SR6D4"
to="SR4D4"
path_from="/dev/disk/by-label/${from}"
path_to="/dev/disk/by-label/${to}"
sed -i 's|${path_from}|${path_to}|g' $file
mv $file{,.backup}
cat "${file}.backup" | awk -v awkfrom="$path_from" -v awkto="$path_to" '{gsub(awkfrom,awkto)}' > $file