Backslash disappears in zsh when I echo a raw variable

孤者浪人 提交于 2019-12-13 07:41:44

问题


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.



来源:https://stackoverflow.com/questions/28170253/backslash-disappears-in-zsh-when-i-echo-a-raw-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!