Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

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

allows $'string' expansion. My man bash says:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
a alert (bell)
backspace
e
E an escape character
f form feed
new line
carriage return
horizontal tab
v vertical tab
backslash
' single quote
" double quote
nnn the eight-bit character whose value is the octal value nnn (one to three digits)
xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
cx a control-x character

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

But why does not convert $'' and $'x0' into a null character?
Is it documented? Is there a reason? (Is it a feature or a limitation or even a bug?)

$ hexdump -c <<< _$''$'x1x2x3x4_'
0000000   _ 001 002 003 004   _  

0000007

echo gives the expected result:

> hexdump -c < <( echo -e '_x0x1x2x3_' )
0000000   _   001 002 003   _  

0000007

My bash version

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

Why echo $'foobar' does not behave as echo -e 'foobar'?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a limitation. bash does not allow string values to contain interior NUL bytes.

Posix (and C) character strings cannot contain interior NULs. See, for example, the Posix definition of character string (emphasis added):

3.92 Character String

A contiguous sequence of characters terminated by and including the first null byte.

Similarly, standard C is reasonably explicit about the NUL character in character strings:

§5.2.1p2 …A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.

Posix explicitly forbids the use of NUL (and /) in filenames (XBD 3.170) or in environment variables (XBD 8.1 "... are considered to end with a null byte."

In this context, shell command languages, including bash, tend to use the same definition of a character string, as a sequence of non-NUL characters terminated by a single NUL.

You can pass NULs freely through bash pipes, of course, and nothing stops you from assigning a shell variable to the output of a program which outputs a NUL byte. However, the consequences are "unspecified" according to Posix (XSH 2.6.3 "If the output contains any null bytes, the behavior is unspecified."). In bash, the NULs are removed, unless you insert a NUL into a string using bash's C-escape syntax ($''), in which case the NUL will end up terminating the value.

On a practical note, consider the difference between the two following ways of attempting to insert a NUL into the stdin of a utility:

$ # Prefer printf to echo -n
$ printf $'foobar' | wc -c
3
$ printf 'foobar' | wc -c
7
$ # Bash extension which is better for strings which might contain %
$ printf %b 'foobar' | wc -c
7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...