echo "text" >> 'Users/Name/Desktop/TheAccount.txt'
Run Code Online (Sandbox Code Playgroud)
如何创建文件(如果文件不存在),如果它已经存在则覆盖它.现在这个脚本只是附加.
Nyl*_*ile 374
对*nix管道如何工作有一点了解会有所帮助.
简而言之,>>
重定向操作符会将行附加到指定文件的末尾,其中 - 大于大于的>
将清空并覆盖文件.
echo "text" > 'Users/Name/Desktop/TheAccount.txt'
Run Code Online (Sandbox Code Playgroud)
BrD*_*aHa 74
在Bash中,如果你已经设置了noclobber set -o noclobber
,则使用语法>|
例如:
echo "some text" >| existing_file
如果文件尚不存在,这也适用
Ale*_*ray 38
尽管NylonSmile
的答案,这是'之类的’正确的..我无法改写文件,以这种方式..
echo "i know about Pipes, girlfriend" > thatAnswer
zsh: file exists: thatAnswer
解决我的问题..我不得不使用... >!
,ála ..
[[ $FORCE_IT == 'YES' ]] && echo "$@" >! "$X" || echo "$@" > "$X"
Run Code Online (Sandbox Code Playgroud)
显然,要小心这个......
小智 9
如果您的环境不允许覆盖>
,请使用管道|
,tee
而不是如下:
echo "text" | tee 'Users/Name/Desktop/TheAccount.txt'
Run Code Online (Sandbox Code Playgroud)
请注意,这也将打印到标准输出.如果这是不需要的,您可以将输出重定向到/dev/null
如下:
echo "text" | tee 'Users/Name/Desktop/TheAccount.txt' > /dev/null
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
cat <<EOF > SampleFile
Put Some text here
Put some text here
Put some text here
EOF
Run Code Online (Sandbox Code Playgroud)