Re: echo $'\0' >a does not write the nul byte

2021-01-17 Thread Martin Schulte
Hello Ilkka, hello *!

> Bash's echo is a builtin, so using it doesn't involve an execve().

Sure – but while passing a string (however it will be encoded) containing a 
null byte to builtins would be possible in principle (as zsh shows) this would 
lead to a large bunch of problems, e.g. another incompatibility between the 
builtin and the external echo.

Best regards

Martin



Re: echo $'\0' >a does not write the nul byte

2021-01-17 Thread Léa Gris

Le 17/01/2021 à 22:02, Chet Ramey écrivait :

On 1/17/21 3:05 PM, h...@artax.karlin.mff.cuni.cz wrote:


Description:
Command
    echo $'\0' |od -c
writes
    000  \n
    001
in contrast to
    echo $'\1' |od -c
    000 001  \n
    002
The nul byte is not echoed by $'\0'.

Repeat-By:
echo $'\0' |od -c
echo $'\1' |od -c


Shell builtin commands obey the same argv conventions as any other Unix
program: arguments are null-terminated strings. That means that

echo $'\0'
echo ''
echo ""

are all equivalent, and none of them will output a null byte.



The only way to output a null byte with shell built-in is:

printf '\0'
or non portable: echo -ne '\0'

This is because `\0' is not a null byte or a nulll string but 
interpreted internally to the command to print a null byte.


--
Léa Gris




Re: echo $'\0' >a does not write the nul byte

2021-01-17 Thread Ilkka Virta
On Mon, Jan 18, 2021 at 12:02 AM Martin Schulte 
wrote:

> To be exact, this is already caused by a Unix Kernel - strings passed by
> the exex* system calls are null terminated, too. As it is the default in
> the C programming language. Thus you can't pass a null byte in an argument
> when invoking a program.
>

Bash's echo is a builtin, so using it doesn't involve an execve(). Most
shells still don't allow passing NULs
to builtins. Zsh is the exception, it does allow NUL bytes in internal
variables and in arguments to builtins.
But, that's Zsh, and of course even it can't pass the NUL as an argument to
an external program, exactly
because of the execve() interface.


Re: echo $'\0' >a does not write the nul byte

2021-01-17 Thread Chet Ramey

On 1/17/21 3:05 PM, h...@artax.karlin.mff.cuni.cz wrote:


Description:
Command
echo $'\0' |od -c
writes
000  \n
001
in contrast to
echo $'\1' |od -c
000 001  \n
002
The nul byte is not echoed by $'\0'.

Repeat-By:
echo $'\0' |od -c
echo $'\1' |od -c


Shell builtin commands obey the same argv conventions as any other Unix
program: arguments are null-terminated strings. That means that

echo $'\0'
echo ''
echo ""

are all equivalent, and none of them will output a null byte.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: echo $'\0' >a does not write the nul byte

2021-01-17 Thread Martin Schulte
Hello Hans, hello Eduardo!

> On Sun, Jan 17, 2021 at 12:05 PM  wrote:
> > (...)
> > The nul byte is not echoed by $'\0'.
> 
> This is expected. Bash uses NUL-byte terminated character sequences to
> store strings, so it can't actually store NUL bytes themselves.

To be exact, this is already caused by a Unix Kernel - strings passed by the 
exex* system calls are null terminated, too. As it is the default in the C 
programming language. Thus you can't pass a null byte in an argument when 
invoking a program.

Best regards

Martin



echo $'\0' >a does not write the nul byte

2021-01-17 Thread hans
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I.  -I../bash -I../bash/include -I../bash/lib  
-D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat 
-Werror=format-security -Wall
uname output: Linux artax 3.2.0-4-amd64 #1 SMP Debian 3.2.96-2 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.2
Patch Level: 37
Release Status: release

Description:
Command
echo $'\0' |od -c
writes
000  \n
001
in contrast to
echo $'\1' |od -c
000 001  \n
002
The nul byte is not echoed by $'\0'.

Repeat-By:
echo $'\0' |od -c
echo $'\1' |od -c



Re: echo $'\0' >a does not write the nul byte

2021-01-17 Thread Eduardo Bustamante
On Sun, Jan 17, 2021 at 12:05 PM  wrote:
> (...)
> The nul byte is not echoed by $'\0'.

This is expected. Bash uses NUL-byte terminated character sequences to
store strings, so it can't actually store NUL bytes themselves.

$'\0'  is the same as '' (i.e. an empty string).

If you want to output an actual NUL byte, try using:

printf '\0'

Instead