Shell – Piping paths with different types of quotes for slash substitution

quotingsedshelltrzsh

I would like to use sed to convert a path with backslashes to the same path with forward slashes:

E.g.
I would like to pipe \\path\to\file\ and obtain /path/to/file

None of the following commands work, and I can't tell why:

First attempt:

> echo '\\path\to\file\' | sed 's/\\/\//g'
/path   o
         ile/

Second attempt:

echo \\path\to\file\ | sed 's/\\/\//g' 
/pathtofile

Third attempt:

 echo "\\path\to\file\" | sed 's/\\/\//g'
dbquote>

I get a similar behavior if I try piping to | tr '\' '/'

I am looking for the correct answer and, if possible, for an explanation of why none of the attempts above worked. Not sure if it matters, but this is all on zsh 4.2.6 (x86_64-redhat-linux-gnu)

Thanks!

Best Answer

file='\\path\to\file\'
printf '%s\n' "$file" | tr -s '\\' /

zsh:

setopt extendedglob
print -r -- ${file//\\##/\/}
Related Question