Backslash disappears in zsh when I echo a raw vari

2019-09-03 15:18发布

问题:

When I read in raw mode in bash:

read -r v

now typing 5 characters (quote, backslash, backslash, x, quote):

"\\x"

and I do a

echo $v

it displays

"\\x"

This is what I expect: Because of the -r switch, I get back what I had put in. When I do exactly the same in zsh, echo $v would display

"\x"

instead. From the manpage zshbuiltins:

-r : Raw mode: a `\' at the end of a line does not signify line continuation and backslashes in the line don't quote the following character and are not removed.

So zsh should behave the same, doesn't it? What is eating my backslash here?

回答1:

The backslash is removed during output not during input. zsh's builtin echo by default evalutes escape sequences. This can be prevented with the -E command line option or by setting the BSD_ECHO shell option:

% read -r v
"\\x"
% echo $v
"\x"
% echo -E $v
"\\x"
% setopt BSD_ECHO
% echo $v
"\\x"
% echo $#v
5

The last line prints the length of string in v showing that all 5 characters are actually there.



标签: zsh