bash - Why $'\0' or $'\x0' is an empty string? Should be the null-character, isn't it? -


allows $'string' expansion. man bash says:

words of form $'string' treated specially. word expands string, backslash-escaped characters replaced specified ansi c standard. backslash escape sequences, if present, decoded follows:
\a alert (bell)
\b backspace
\e
\e escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\ backslash
\' single quote
\" double quote
\nnn eight-bit character value octal value nnn (one 3 digits)
\xhh eight-bit character value hexadecimal value hh (one or 2 hex digits)
\cx control-x character

the expanded result single-quoted, if dollar sign had not been present.

but why not convert $'\0' , $'\x0' null character?
documented? there reason? (is feature or limitation or bug?)

$ hexdump -c <<< _$'\0'$'\x1\x2\x3\x4_' 0000000   _ 001 002 003 004   _  \n 0000007 

echo gives expected result:

> hexdump -c < <( echo -e '_\x0\x1\x2\x3_' ) 0000000   _  \0 001 002 003   _  \n 0000007 

my bash version

$ bash --version | head -n 1 gnu bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) 

why echo $'foo\0bar' not behave echo -e 'foo\0bar'?

it's limitation. bash not allow string values contain interior nul bytes.

posix (and c) character strings cannot contain interior nuls. see, example, posix definition of character string (emphasis added):

3.92 character string

a contiguous sequence of characters terminated by , including first null byte.

similarly, standard c reasonably explicit nul character in character strings:

§5.2.1p2 …a byte bits set 0, called null character, shall exist in basic execution character set; used terminate character string.

posix explicitly forbids use of nul (and /) in filenames (xbd 3.170) or in environment variables (xbd 8.1 "... considered end null byte."

in context, shell command languages, including bash, tend use same definition of character string, sequence of non-nul characters terminated single nul.

you can pass nuls freely through bash pipes, of course, , nothing stops assigning shell variable output of program outputs nul byte. however, consequences "unspecified" according posix (xsh 2.6.3 "if output contains null bytes, behavior unspecified."). in bash, nuls removed, unless insert nul string using bash's c-escape syntax ($'\0'), in case nul end terminating value.

on practical note, consider difference between 2 following ways of attempting insert nul stdin of utility:

$ # prefer printf echo -n $ printf $'foo\0bar' | wc -c 3 $ printf 'foo\0bar' | wc -c 7 $ # bash extension better strings might contain % $ printf %b 'foo\0bar' | wc -c 7 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -