简体   繁体   English

打印“ \\ x09”,打印0x20,如何将0x09打印到STDOUT?

[英]Print “\x09”, print 0x20, how to print 0x09 to STDOUT?

I try to print to STDOUT the 0x09 (horizontal TAB) value, but in perl, python or bash 0x09 is replaced by 0x20 (a space). 我尝试将0x09(水平TAB)值打印到STDOUT,但在perl,python或bash中,将0x09替换为0x20(空格)。

$ hexdump -C <<< $(perl -e 'print "A\x09B" ')
00000000  41 20 42 0a                                       |A B.|
00000004

same problem in bash: bash中的相同问题:

$ hexdump -C <<< $(printf "A\x09B")
00000000  41 20 42 0a                                       |A B.|
00000004

It's possible to print the 0x09 value to STDOUT? 可以将0x09值打印到STDOUT吗?

At issue here is Bash expansion ; 这里讨论的是Bash 扩展 you get the same issue with putting the command in backticks: 将命令放在反引号中会遇到相同的问题:

$ echo `python -c 'print "A\x09B"'`
A B

Avoid expansion; 避免扩展; it splits your input on whitespace and rejoins for the next command; 它在空白处分割您的输入,并重新加入下一个命令; you see the same with multiple spaces: 您会看到多个空格相同:

$ hexdump -C <<< $(python -c 'print "A\x20\x20\x20B"')
00000000  41 20 42 0a                                       |A B.|
00000004

Use a pipe instead: 使用管道代替:

$ python -c 'print "A\x09B"' | hexdump
0000000 41 09 42 0a
0000004

or quote the command (miraculously, the extra quotes don't clash with those used in the command line itself!): 或引用命令(奇迹般地,多余的引号与命令行本身中使用的那些不冲突!):

hexdump -C <<< "$(python -c 'print "A\x20\x20\x20B"')"

The subprocess output being expanded is a bug in bash, to be fixed in bash 4.4. 扩展的子流程输出是bash中的bug,将在bash 4.4中修复。

It's entirely a bash issue. 这完全是一个bash问题。 Specifically, it's a bash bug that can be worked around as follows: 具体来说,这是一个bash错误,可以通过以下方法解决:

                 +-------- Add these ---------+
                 |                            |
                 v                            v
$ hexdump -C <<< "$(perl -e 'print "A\x09B"' )"
00000000  41 09 42 0a                                       |A.B.|
00000004

Alternatively, 或者,

# Passed via STDIN as the original.
$ perl -e 'print "A\x09B"' | hexdump -C
00000000  41 09 42                                          |A.B|
00000003

# Passed via a file name.
$ hexdump -C <( perl -e 'print "A\x09B"' )
00000000  41 09 42                                          |A.B|
00000003

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM