为什么$'\\ 0'或$'\\ X0'是一个空字符串?应该是空的性格,是不是? [英] Why $'\0' or $'\x0' is an empty string? Should be the null-character, isn't it?

查看:164
本文介绍了为什么$'\\ 0'或$'\\ X0'是一个空字符串?应该是空的性格,是不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

庆典允许 $'字符串 扩张。我的男人庆典说:

形式的字 $ 字符串 被特殊处理。
  这个词扩展到 字符串 ,与由ANSI C标准中规定的取代反斜杠转义字符。
  反斜杠转义序列,如果present,是德codeD如下:结果
             \\ A 警报(钟)结果
             \\ b 退格键结果
             \\ E 结果
             \\ E 转义字符结果
             \\ F 换结果
             \\ n 新行结果
             \\ r 回车结果
             \\ t 水平制表结果
            符\\ v 垂直标签结果
             \\ 反斜线结果
             \\ 单引号结果
             \\双引号结果
             \\ NNN 8位字符,其值是八进制值 NNN (一到三个数字)结果
             \\点¯x HH 8位字符,它的值是十六进制值 HH (一个或两个十六进制数字)结果
             \\ C X 一个控制的 X 字符

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)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t 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

扩大的结果是单引号,好像美元符号一直没present。

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

为什么庆典不转换 $'\\ 0' $'\\ X0'成空字符?结果
它是记录?是否有一个原因吗? (它是一个功能或限制,甚至是错误吗?)

But why does bash not convert $'\0' 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 <<< _$'\0'$'\x1\x2\x3\x4_'
0000000   _ 001 002 003 004   _  \n
0000007

回声给出了预期的结果:

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

我的bash的版本

My bash version

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

为什么回声$'富\\ 0bar ​​不表现为回声-e'富\\ 0bar ​​

Why echo $'foo\0bar' does not behave as echo -e 'foo\0bar'?

推荐答案

这是一个限制。 庆典不允许字符串值,包含内部NUL字节。

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

POSIX(可和C)字符串不能包含内部完全无效。见,例如,字符串的的Posix定义(重点添加):

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

3.92字符串

通过终止字符的连续序列并包括在第一空字节

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

同样,标准C是相当明确,关于字符串NUL字符:

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

&教派; 5.2.1p2&hellip;设置为0,所有位字节,称为空字符,应在基本执行字符集存在;它是用来终止的字符串。

§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的明确禁止使用NUL(以及 / )的文件名(XBD 3.170),或在环境变量(XBD 8.1......被认为是一个结束空字节。

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."

在这种情况下,外壳命令语言,包括bash中,往往使用的字符串的相同的定义,如由单一NUL结尾的非NUL字符的序列。

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.

您可以通过的bash管道自由通过完全无效的,当然,并没有什么阻止你到输出NUL字节程序的输出分配一个shell变量。然而,其后果是未指定根据POSIX(可XSH 2.6.3如果输出中包含任何空字节,其行为是不确定的。)。在bash中,完全无效被删除,除非你使用bash的C-转义语法插入NUL为一个字符串( $'\\ 0'),在这种情况下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 ($'\0'), in which case the NUL will end up terminating the value.

在实际的音符,可以考虑尝试插入一个NUL到标准输入 A效用的以下两种方式的区别是:

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 $'foo\0bar' | wc -c
3
$ printf 'foo\0bar' | wc -c
7
$ # Bash extension which is better for strings which might contain %
$ printf %b 'foo\0bar' | wc -c
7

这篇关于为什么$'\\ 0'或$'\\ X0'是一个空字符串?应该是空的性格,是不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆