Scyphus Draft — Zsh tips

Zsh is a powerful shell that has almost backward compatibility to other Bourne shells (B-shells). This page provides zsh specific tips. For B-shell tips, please refer here.

Search from this site:
Custom Search

Zsh tips

Overwriting existing file by redirection

When we do something via CLI interfaces, we sometimes execute a high-load command repeatedly by an accident of history. Especially commands with file output make things worse. For example, assuming the following command,

$ ./analyze-large-dataset > result.txt

if we repeat this command, we have to wait until the second operation finishes because the result of the first operation has been truncated. To avoid this, we can use an option on zsh as follows.

$ setopt no_clobber

If you want to enable this on startup, please add it to your .zshrc.

However, this option bothers us about overwriting existing files. In such cases, we can use a useful redirect operator >!.

$ ./trivial-command >! result.txt

This operation equals to the next operation.

$ if [ -f result.txt ]; then rm -f result.txt; fi; ./trivial-command > result.txt