是这样的:
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 > output.txt <<EOF
some text
some lines
EOF
对于PHP文件:
cat > test.php <<PHP
<?php
echo "Test";
echo \$var;
?>
PHP
其他回答
使用echo编写带有环境变量的多行文本:
echo -e "
Home Directory: $HOME \n
hello world 1 \n
hello world 2 \n
line n... \n
" > file.txt
解决你的问题的方法是:
echo " Some Text Goes Here " > filename.txt
但是如果你想将一个文件的输出重定向到其他文件,或者想将一个文件的输出附加到另一个文件,你可以使用cat命令:
cat filename > newfile——将filename的输出重定向到newfile
cat filename >> newfile——将filename的输出附加到newfile
这就是echo所做的:
echo "Some text here." > myfile.txt
Cat也可以用在|后面写入文件,即管道向Cat提供数据流
你也可以这样做:
user@host: $ cat<<EOF > file.txt
$ > 1 line
$ > other line
$ > n line
$ > EOF
user@host: $ _
我相信有很多方法可以使用它。