当我回显原始变量时,反斜杠在 zsh 中消失 [英] Backslash disappears in zsh when I echo a raw variable
问题描述
当我在 bash 中以原始模式阅读时:
When I read in raw mode in bash:
read -r v
现在输入 5 个字符(引号、反斜杠、反斜杠、x、引号):
now typing 5 characters (quote, backslash, backslash, x, quote):
"\\x"
我做了一个
echo $v
显示
"\\x"
这就是我所期望的:由于 -r 开关,我得到了我输入的内容.当我在 zsh 中做完全相同的事情时,echo $v 会显示
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"
相反.来自手册页 zshbuiltins:
-r : 原始模式:行尾的\"不表示行继续,行中的反斜杠不引用后面的字符,不会被删除.
-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.
所以 zsh 应该表现得一样,不是吗?什么在吃我的反斜杠?
So zsh should behave the same, doesn't it? What is eating my backslash here?
推荐答案
反斜杠在输出期间而不是在输入期间被移除.zsh 的 内置 echo
默认情况下评估转义序列.这可以通过 -E
命令行选项或通过设置 BSD_ECHO
shell 选项来防止:
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
最后一行打印 v
中字符串的长度,表明所有 5 个字符实际上都在那里.
The last line prints the length of string in v
showing that all 5 characters are actually there.
这篇关于当我回显原始变量时,反斜杠在 zsh 中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!