How do I unset (undo) unix set -C in zsh?

This works as expected

$ echo "foo" > foo
$ cat foo                # foo
$ echo "updated" > foo
$ cat foo                # updated

From man set

-C      If  set, bash does not overwrite an existing file with the
        >, >&, and <> redirection operators.  This may be overridden
        when creating output files by using the redirection operator
        >| instead of >.

Ok, so let's set -C

$ set -C
$ echo "bar" > bar
$ cat bar                # bar
$ echo "updated" > bar
file exists: bar

This is all well and good, but now it's time to turn off set -C.

How do i disable set -C after it's been turned on?


I've tried:

$ unset -C
zsh: bad option: -C

Solution 1:

Blah. It's as simple as

$ set +C

Of course I find the answer immediately after posting the question.

The man pages don't really mention the difference between + and -.

Solution 2:

It seems you are using the Z shell, but quote the bash man page.

Just to clarify, the set -C and set +C commands work also in zsh, but more easily to memorize are IMHO shell options:

setopt no_clobber

This is equivalent to set -C. And, to disable this option, there is indeed a unsetopt builtin:

unsetopt no_clobber

All options are listed with man zshoptions. Options are kind of symmetric, so setopt no_clobber is equivalent to unsetopt clobber. That's why, in the man page the CLOBBER option is explained:

CLOBBER (+C, ksh: +C)

Allows > redirection to truncate existing files, and >> to create files. Otherwise >! or >| must be used to truncate a file, and >>! or >>| to create a file.

A related option (IMHO very handy) is

HIST_ALLOW_CLOBBER

Add | to output redirections in the history. This allows history references to clobber files even when CLOBBER is unset.

Demo:

zsh$ setopt no_clobber hist_allowclobber
zsh$ echo foo > baz
zsh$ echo bar > baz
zsh: file exists: baz
zsh$ [Arrow UP]
zsh$ echo bar >| baz
zsh$

And as a last remark, options are case-insensitive and underscores are ignored, hence NOCLOBBER, NO_clobber, noclobber are treated the same way.