关于tail -c的问题

问题描述 投票:0回答:1

当我打开终端并输入命令

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

shell sh tail
1个回答
0
投票

默认情况下,

echo
附加换行符 ( )到输出。当您将
echo 987654321
的输出通过管道传输到
tail -c3
时,它会处理字符串
987654321\n

如果您不希望

echo
命令附加换行符,您可以使用
-n
标志:

echo -n 987654321 | tail -c3
© www.soinside.com 2019 - 2024. All rights reserved.