我正在学习linux的cat命令,我发现了这个命令:
$ echo 'Text through stdin' | cat - file.txt
这里的“-”是什么意思?如果我不键入它,则不会显示“通过标准输入的文本”。
通常写成stdin破折号 ( -)。
甚至man cat提到:
没有 FILE,或者当 FILE 为 - 时,读取标准输入。
手册页甚至有一个示例说明了破折号和普通文件名的使用(这与您的原始问题非常接近,但包括答案):
cat f - g
Output f's contents, then standard input, then g's contents.
-告诉 cat 从stdin. 这很常见,如果你传递-给它们,很多应用程序都会从标准输入读取。
一些应用程序-使用stdout.
这是一个下载 blender 的示例,我们没有将其写入文件,而是直接将其写入并将其通过stdout管道传输到 tar,从而在下载过程中动态扩展它。
wget -c https://download.blender.org/source/blender-2.90.1.tar.xz -O - | tar -xzv
这里-O -告诉 wget 直接写入stdout
$ echo 'Text through stdin' | cat - file.txt
-告诉cat从标准输入读取,在这种情况下,从管道读取,即echo 'Text through stdin'输出什么。