Shell script: Cut / (slash) character in string

I have a string "\/scratch\/databases\". I want
to have a new string "\/scratch\/databases" by cutting last '\' character using shell script. I can't do this
Please help me.
Thanks in advance

[Edited by ThuongTranVN on 01-26-2001 at 01:35 PM]

You must use a \ in front of the \ to make it a \\ in your search and replace functions because of the special nature (meta) of the \ char. Example in vi editor:


The fragment above says "search for the \ at the end of a line and replace with nothing" :slight_smile:

Note. that I've not tested the fragment above and it might need some more tweaking to work.

[Edited by Neo on 01-29-2001 at 05:03 PM]

Thank you so much for your reply.
I tried
:%s/\\$// don't replcae '\' character at the end of line
:%s/\\// replace only first '\' character of line
:%s/\\//g replace all '\' characters of line.

I need help.
Thanks in advance

If you want to do it in vi, I would use this:


Your original post said you are trying to write a shell script. In which case, you could do:

echo $FOO | sed 's/\\$//'

Thank you so much for your help.
I've got it as follows:
echo $FOO | sed 's/\\$//' > tmp.log
DAEHOME=`cat tmp.log`
I can not use
DAEHOME=`echo $FOO | sed 's/\\$//'`