I have this bash code:

  if [[ ! -f "$HOME/.oresoftware/package.json" ]]; then
       cat "node_modules/@oresoftware/package.json" > "$HOME/.oresoftware/package.json";

but is there a way to only redirect to a file if it doesn't exist in just one line of code? something like this:

   cat "node_modules/@oresoftware/package.json" ?> "$HOME/.oresoftware/package.json";

that would be fun I guess

1 Answer 1


If you set the noclobber option, bash will refuse to overwrite an existing file:

$ set -C    # or set -o noclobber
$ touch foo
$ cat bar > foo
bash: foo: cannot overwrite existing file


set -C
cat "node_modules/@oresoftware/package.json" > "$HOME/.oresoftware/package.json"

If you want to ignore the exit status, you can add a ! before the cat command

set -C; ! cat "node_modules/@oresoftware/package.json" > "$HOME/.oresoftware/package.json"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .