防止使用重定向覆盖文件

Ale*_*lls 5 shell bash shell-script

如果我做:

$ node foo.js > output.d.ts
Run Code Online (Sandbox Code Playgroud)

有没有办法将外壳配置为不覆盖已存在的文件?也许是一个无破坏的选择?

Mic*_*mer 7

有没有办法将外壳配置为不覆盖已存在的文件?也许是一个无破坏的选择?

是的,有一个noclobber选项:

使用>、>&和防止输出重定向<>覆盖现有文件。

$ echo foo > bar
$ echo foo > bar
$ set -o noclobber
$ echo foo > bar
bash: bar: cannot overwrite existing file
Run Code Online (Sandbox Code Playgroud)