user1934428
user1934428

Reputation: 22227

Backslash disappears in zsh when I echo a raw variable

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?

Upvotes: 0

Views: 649

Answers (1)

Adaephon
Adaephon

Reputation: 18339

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.

Upvotes: 2

Related Questions