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

Why would I enter "\\\ 0" to create the string "\ 0" in zsh?

> echo 0 0 > echo \0 0 slu@dev :~ > echo \\0 slu@dev :~ > echo "\\0" # <--- What!!? slu@dev :~ > echo \\\0 slu@dev :~ > echo "\\\0" \0 slu@dev :~ > bash Executing .bashrc $ echo "\0" \0 $ echo "\\0" \0 $ echo "\\\0" \\0 

I have to say that bash behavior makes much more sense to me.

More details:

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

The biggest problem is that there is no value that gives the desired result \0 for 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 '\\0' | hexdump -C 00000000 5c 30 0a |\0.| 00000003 

Using single quotes seems to help get consistent behavior. I would like someone to explain the behavior with double quotes.

+7
source share
2 answers

Use single quotes and disable echo control with echo with the -E flag.

echo -E '\0' should generate a \0 for both zsh and bash (and dashes).

+3
source

This is the standard (readable portable) way to do this:

 printf "\0" 

It should work regardless of the OS (like Unix), regardless of the shell.

+1
source

All Articles