shell 关于tail -c的问题[已关闭]

ddrv8njm  于 11个月前  发布在  Shell
关注(0)|答案(1)|浏览(121)

**已关闭。**此问题为not about programming or software development。目前不接受回答。

此问题似乎与a specific programming problem, a software algorithm, or software tools primarily used by programmers无关。如果您认为此问题与another Stack Exchange site的主题相关,可以发表评论,说明在何处可以回答此问题。
12天前关门了。
Improve this question
当我打开终端并输入命令echo 987654321 | tail -c3时,输出为21
但是,当我创建一个名为test_tail.sh的文件时,将987654321写入该文件,命令tail -c3 test_tail.sh将打印321
为什么会有这些不同的结果呢?根据手册,tail -cNUM shows the last NUM bytes of a file。所以,我很好奇为什么终端中的echo 987654321 | tail -c3会打印21

uinbv5nw

uinbv5nw1#

默认情况下,echo会在输出后追加一个换行符(\n)。当您将echo 987654321的输出通过管道传输到tail -c3时,它会处理字符串987654321\n
如果你不想让echo命令附加一个换行符,你可以使用-n标志:

echo -n 987654321 | tail -c3 # outputs 321

字符串

**注意:**echo命令中的-n标志不是POSIX标准的一部分,即echo的某些实现可能不支持-n标志。因此,正如 @Biffen 所提到的,建议使用printf命令,它更易于移植,并且符合POSIX标准。例如:

printf "987654321" | tail -c3

相关问题