9

I typed it by mistake but bash didn't print any errors (but created an empty file) so I thought maybe it actually means something ? (e.g. date |> tmp.txt)

1
  • 1
    Are you sure the command isn’t of the form date |> tmp.txt cmd2? Because that changes the answer. – Konrad Rudolph Sep 25 '18 at 13:26
17

That seems to be just a pipeline where the second part is an empty command, only containing the redirection. Writing it as date | >file might make it easier to interpret. The empty command doesn't do anything but process the redirection, creating the file.

date >| file on the other hand would act as an override for the noclobber shell option, which prevents the regular > from overwriting existing files.

$ touch foo; set -o noclobber
$ date > foo
bash: foo: cannot overwrite existing file
$ date >| foo       # works
1

Yes, it will not throw error because for bash > file means redirect to a file named file. As in your case there is nothing to redirect to file, bash will just create a file name file with nothing in it.

[bd@centos-6.5 my-tests]$ date | > my_file
[bd@centos-6.5 my-tests]$ cat my_file
[bd@centos-6.5 my-tests]$ 
2
  • Funny. Zsh has a different behavior: after the command, my_file contains the output of date. – N.I. Sep 25 '18 at 9:28
  • 12
    @NajibIdrissi, in zsh, when there are only redirections and no command, zsh runs the $NULLCMD command (cat by default) or $READNULLCMD (a pager by default) if there are only input redirections. – Stéphane Chazelas Sep 25 '18 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.