Reputation: 105
path=/tmp/a1
file=/tmp/a1/a.txt
file_without_path=`echo $file | sed s/'\/tmp\/a1'/''/g`
echo $file_without_path
In the code above, I have a question
If variable "path" is a dynamic veriable which allows users to self-define
e.g., user defines "path" as /usr/local/
Then I pass this value to do search and replace
However, if I pass this value without any intervention
the program may treat it as echo $file | sed s//usr/local/''/g
I tried to use tr -s '/' '\/'
but it is not successful
I would like to ask how can you solve such case
Thanks.
Upvotes: 0
Views: 111
Reputation: 38265
The separator for the sed
"s" command is not required to be /
; sed will use as separator whatever comes after "s":
sed s%'/tmp/a1'%''%g ## no need to worry about escaping slashes any more
Upvotes: 1