是这样的:

cat "Some text here." > myfile.txt

可能吗?这样myfile.txt的内容现在将被重写为:

Some text here.

这对我不起作用,但也不会抛出任何错误。

对基于cat的解决方案特别感兴趣(不是vim/vi/emacs等)。所有在线的例子都显示cat用于文件输入,而不是原始文本…


当前回答

使用cat将文本写入文件的另一种方法是这样的

cat >file.txt <<< Write something here

其他回答

这就是echo所做的:

echo "Some text here." > myfile.txt

听起来你在找这里的文档

cat > outfile.txt <<EOF
>some text
>to save
>EOF

还有另一种方法

cat > outfile.txt
>Enter text
>to save press ctrl-d

使用echo编写带有环境变量的多行文本:

echo -e "
Home Directory: $HOME \n
hello world 1 \n
hello world 2 \n
line n... \n
" > file.txt 

因为没人回答最初的问题。是的。Cat可以用来在没有here doc的情况下将文本写入文件。

cat - >file.txt
This is the world's worst
multiline text editor.
^d