简体   繁体   中英

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

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

same problem in bash:

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

It's possible to print the 0x09 value to STDOUT?

At issue here is Bash expansion ; 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.

It's entirely a bash issue. Specifically, it's a bash bug that can be worked around as follows:

                 +-------- 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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