bash – Write to file, but overwrite it if it exists

bash – Write to file, but overwrite it if it exists

The >> redirection operator will append lines to the end of the specified file, where-as the single greater than > will empty and overwrite the file.

echo text > Users/Name/Desktop/TheAccount.txt

In Bash, if you have set noclobber a la set -o noclobber, then you use the syntax >|

For example:

echo some text >| existing_file

This also works if the file doesnt exist yet


  • Check if noclobber is set with: set -o | grep noclobber

  • For a more detailed explanation on this special type of operator, see this post

  • For a more exhaustive list of redirection operators, refer to this post

bash – Write to file, but overwrite it if it exists

Despite NylonSmiles answer, which is sort of correct.. I was unable to overwrite files, in this manner..

echo i know about Pipes, girlfriend > thatAnswer

zsh: file exists: thatAnswer

to solve my issues.. I had to use… >!, รก la..

[[ $FORCE_IT == YES ]] && echo [email protected] >! $X || echo [email protected] > $X

Obviously, be careful with this…

Leave a Reply

Your email address will not be published.