Treatment of backslashes across shells

zsh echo behaves the standard way, like bash in UNIX mode. That is it expands \b to the ASCII BS character as the UNIX specification requires.

Don't use echo to display arbitrary strings, use printf:

printf '%s\n' "$1"

print -r -- "$1" also works but is ksh/zsh specific.

echo -E - "$1" work with zsh and I believe some BSDs.

cat << EOF
$1
EOF

works in any Bourne-like shell even those from a few decades when there was no printf command but it spawn a new process, and is really not necessary nowadays as we now have printf everywhere.

And by the way, you need to escape backslashes on a shell command line as it's special to the shell (every shell but rc), so:

$ foo '\\foo\bar'

foo \\foo\bar would pass the "\foo\bar" string to foo which can't reinvent the lost backslash back.


new answer: read -r var

-r  raw input - disables interpretion of backslash escapes and line-continuation in the read data

and to display:

printf "%s" "$var"
echo "$var"

should work.

So for your foo function:

function foo
{
    read -r var
    echo -E "var is : ${var}"
}

$ foo 
\\here\is\some\path
var is : \\here\is\some\path

old answer below (not answering, but maybe usefull ^^)

just replace each \ by \\ to tell the shell "that it a literall \ I want". otherwise (for example in zsh, in your example) it could well be that \b means "1 backspace", or whatever.

you could for example use sed:

sed -e 's,\\,\\\\,g' < the_original > the_escaped_backslaches_version

(see how you also need to escape "\" there, to tell sed the same thing: "it is a literall "\" I want) (and notice I surround it by ' ' and not " " to avoid the shell to interpret most of it too)