Avoiding overwrites

You may have noticed in the output from the preceding commands that Bash does not prompt you about the destination file for an output redirection that doesn't exists  it creates it for you, without notifying you. Perhaps a bit more surprisingly, it won't prompt you if it replaces the contents of a file, either:

$ printf 'First command\n' > myfile
$ printf 'Second command\n' > myfile
$ cat myfile
Second command

The output from the first printf command is replaced with the output from the second printf command. If you don't want this to happen, and you only want to allow Bash to create new files, you can set the -C shell option with set:

$ set -C

For the running shell session, Bash will refuse to overwrite a file with a ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.