Why must I enter "\\\0" to create a string "\0" in zsh?

> echo 0
0
> echo 
0
slu@dev:~
> echo 

slu@dev:~    
> echo ""
                   # <--- What!!?
slu@dev:~
> echo 

slu@dev:~
> echo ""

slu@dev:~
> bash
Executing .bashrc
$ echo ""

$ echo ""

$ echo ""

I gotta say, bash's behavior makes a lot more sense to me.

More details:

slu@dev:~
> echo "0" | hexdump -C
00000000  30 0a                                             |0.|
00000002
slu@dev:~
> echo "" | hexdump -C
00000000  00 0a                                             |..|
00000002
slu@dev:~
> echo "" | hexdump -C
00000000  00 0a                                             |..|
00000002
slu@dev:~
> echo "" | hexdump -C
00000000  5c 30 0a                                          |.|
00000003
slu@dev:~
> echo "\0" | hexdump -C
00000000  5c 30 0a                                          |.|
00000003
slu@dev:~
> echo "\" | hexdump -C
00000000  5c 00 0a                                          |..|
00000003
slu@dev:~
> echo "\" | hexdump -C
00000000  5c 00 0a                                          |..|
00000003
slu@dev:~
> echo "\" | hexdump -C
00000000  5c 5c 30 0a                                       |.|
00000004

The biggest issue is that there is no value that produces the desired result on both bash and zsh.

Update:

slu@dev:~
> echo '0' | hexdump -C
00000000  30 0a                                             |0.|
00000002
slu@dev:~
> echo '' | hexdump -C
00000000  5c 0a                                             |.|
00000002
slu@dev:~
> echo '' | hexdump -C
00000000  5c 0a                                             |.|
00000002
slu@dev:~
> echo  | hexdump -C
00000000  5c 0a                                             |.|
00000002
slu@dev:~
> echo '' | hexdump -C
00000000  5c 30 0a                                          |.|
00000003

Looks like using single quotes helps get consistent behavior. I'd love for somebody to explain the behavior with the double-quotes.


Use single quotes and turn off escape handling in echo with the -E flag.

echo -E '' should produce a on both zsh and bash (and dash).


This is the standard (read portable) way to do it:

printf ""

Should work whatever the (Unix like) OS, whatever the shell.

链接地址: http://www.djcxy.com/p/70868.html

上一篇: Java FileLock阻止无例外; 等待锁定

下一篇: 为什么我必须输入“\\\ 0”才能在zsh中创建一个字符串“\ 0”?