是这样的:
cat "Some text here." > myfile.txt
可能吗?这样myfile.txt的内容现在将被重写为:
Some text here.
这对我不起作用,但也不会抛出任何错误。
对基于cat的解决方案特别感兴趣(不是vim/vi/emacs等)。所有在线的例子都显示cat用于文件输入,而不是原始文本…
是这样的:
cat "Some text here." > myfile.txt
可能吗?这样myfile.txt的内容现在将被重写为:
Some text here.
这对我不起作用,但也不会抛出任何错误。
对基于cat的解决方案特别感兴趣(不是vim/vi/emacs等)。所有在线的例子都显示cat用于文件输入,而不是原始文本…
当前回答
因为没人回答最初的问题。是的。Cat可以用来在没有here doc的情况下将文本写入文件。
cat - >file.txt
This is the world's worst
multiline text editor.
^d
其他回答
这就是echo所做的:
echo "Some text here." > myfile.txt
你也可以这样做:
user@host: $ cat<<EOF > file.txt
$ > 1 line
$ > other line
$ > n line
$ > EOF
user@host: $ _
我相信有很多方法可以使用它。
因为没人回答最初的问题。是的。Cat可以用来在没有here doc的情况下将文本写入文件。
cat - >file.txt
This is the world's worst
multiline text editor.
^d
MacOS 11.4上的Jenkins
{
echo 'line 1' $var1
echo 'line 2'
echo 'result' $var5
} > ${destination_file}
我已经用cat语法试过很多次了,例如:cat > ${dest_file} <<EOF…EOF,并得到错误语法错误:文件意外结束。因此,我使用echo语法将多行文本输出到文件中。
引用:
如何追加多行文件 github.com/koalaman/shellcheck/wiki/SC2129
简单地用cat管道echo
例如
echo write something to file.txt | cat > file.txt