shell script replace variable of linux path

sed is a popular tool to be used to replace a string in a file.

For example, give a file, we can use the following command to replace all the string XXX into YY

sed -i s/XXX/YY/g fileName

or sed s/XXX/YY/g fileName > newFile

but when you want to replace linux path, the above method doesn’t work. You can try it your self.

here is a workable solution: 



sed “s,$pathA,$pathB,g” fileName > newFileName

then all the path of /user/xx/zz/ will be replaced by /user/aa/zz/dd.

It turned out, you can also change , to other characters such as #, ctr-A, et al, 

as long as the variables don’t contain the separator, it will work. 


  • Aftab Ali

    useful information.