【发布时间】:2025-12-29 12:20:27
【问题描述】:
我对某些 bash 命令中发生的一些0a(即NL ASCII 字节)感到困惑。关于以下内容:
$ echo | sha1sum $1 | awk '{print $1;}' | xxd -r -ps > test.bin
$ echo | sha1sum $1 | awk '{print $1;}' > test.hex
$ xxd test.bin
00000000: adc8 3b19 e793 491b 1c6e a0fd 8b46 cd9f ..;...I..n...F..
00000010: 32e5 92fc 2...
$ xxd test.hex
00000000: 6164 6338 3362 3139 6537 3933 3439 3162 adc83b19e793491b
00000010: 3163 3665 6130 6664 3862 3436 6364 3966 1c6ea0fd8b46cd9f
00000020: 3332 6535 3932 6663 0a 32e592fc.
是什么导致0a 字节出现在test.hex 而不是test.bin?
注意 1:这是我按照那里使用的解决方案一直在问自己的一个问题:
Dump a ```sha``` checksum output to disk in binary format instead of plaintext hex in bash
注意 2:我能够抑制 0a 字节,这不是问题所在,我只是好奇为什么它在一种情况下存在,而在另一种情况下不存在:
$ echo | sha1sum $1 | awk '{print $1;}' | head -c-1 > test_2.hex
$ xxd test_2.hex
00000000: 6164 6338 3362 3139 6537 3933 3439 3162 adc83b19e793491b
00000010: 3163 3665 6130 6664 3862 3436 6364 3966 1c6ea0fd8b46cd9f
00000020: 3332 6535 3932 6663 32e592fc
【问题讨论】:
-
那个 awk 命令将在每行输出的末尾生成一个换行符,所以你真正的问题只是关于 xxd,特别是“为什么
xxd -r -ps删除换行符”。您是否查看了手册页以查看其内容? -
好的,谢谢,我很困惑。所以确实
xxd删除了这个东西。谢谢。
标签: bash binary hex end-of-line xxd