我们知道,将文本文件file.txt
的内容发送到从标准输入读取的脚本很简单:
the_script < file.txt
假设我想做与上面相同的事情,除了我有一行额外的文本要发送到脚本,然后是文件的内容?肯定有比这更好的方法:
echo "Here is an extra line of text" > temp1
cat temp1 file.txt > temp2
the_script < temp2
这能在不创建任何临时文件的情况下完成吗?
基于cdarke的答案使其从左到右可读:
echo "Extra line" | cat - file.txt | the_script
有几种方法可以做到这一点。现代shell(Bash和ksh93)具有支持从stdin读取单个值的功能,称为此处字符串:
cat - file.txt <<< "Extra line"|the_script
cat
的第一个自变量是从标准输入读取的连字符-
。here字符串遵循<<<
表示法。
这应该在bash
:中完成
{ echo "Here is an extra line of text"; cat file.txt; } < the_script