How do echo
and printf
treat backslashes in zsh
, bash
and other shells?
Under zsh I get the following behavior:
$ echo "foo\bar\baz"
foaaz
$ echo "foo\\bar\\baz"
foaaz
$ echo 'foo\bar\baz'
foaaz
$ echo 'foo\\bar\\baz'
foo\bar\baz
Under bash, things seem a bit more consistent:
bash$ echo "foo\bar\baz"
foo\bar\baz
bash$ echo 'foo\bar\baz'
foo\bar\baz
bash$
But more concretely: How can I pass a string containing backslashes such as \\foo\bar\something
to:
echo
printf
print
and get exactly the same string? (in zsh
and bash
)?
Here is another experiment with functions in zsh:
function foo
{
echo -E '$1'
}
$ foo \here\is\some\path
$1
How can I have it just print \\here\is\some\path
?
Update (Note: this has now been answered in Stephane's comment)
I have tried the following in zsh 5.0.2:
function foo
{
printf '$s\n' $1
}
foo '\some\path'
But this prints $s
?
printf
,echo
isn't portable in the regard. – jordanm Mar 28 '13 at 18:54'
with"
, and invoke it with : foo '\here\is\some\path' (otherwise the invoking shell gets a chance to interpret the '' before they get to the function) – Olivier Dulac Mar 28 '13 at 19:11'$s\n'
when it should have used'%s\n'
. – cjm Mar 29 '13 at 06:25