I wrote my script using the the directory ~/deleted (it's a long script). after using ~/deleted for the whole script. I want to let the user choose the directory by putting the path in ~/.rm.cfg.My file starts like this:#!/bin/bashdefaultpath=~/deletedif [ -s ~/.rm.cfg ]then    defaultpath=$(head -1 ~/.rm.cfg)fiI need to replace every ~/deleted  with $defaultpath in my script. Is there a way to do it in command line such that it saves and replaces the original file? I can't get either perl or sed to work.
Replace a string in a file and overwrite the file
bash;files;text processing
sed -i 's|~/deleted|$defaultpath|g' file.shExplanation:-i tells sed to modify the file in place.s|~/deleted|$defaultpath|g tells sed to replace ~/deleted with $defaultpath whereever it finds it.Extra feature: preserve ~/deleted on the third lineYou did not ask for this but, in your example script, it would be nice to leave ~/deleted unchanged on the third line where it is used to define defaultpath.  To change all instances of ~/deleted except the one on the third line, use:sed -i -e '3n' -e 's|~/deleted|$defaultpath|g' file.shThis adds the single sed command 3n which tells sed to skip the third line.