11

I typed |> by mistake but bash didn't print any errors. (But it did create an empty file.) For example,

date |> tmp.txt

I thought maybe it actually means something ?

1
  • 1
    Are you sure the command isn’t of the form date |> tmp.txt cmd2? Because that changes the answer. Commented Sep 25, 2018 at 13:26

2 Answers 2

18

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
2

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.

[[email protected] my-tests]$ date | > my_file
[[email protected] my-tests]$ cat my_file
[[email protected] my-tests]$ 
2
  • Funny. Zsh has a different behavior: after the command, my_file contains the output of date.
    – N.I.
    Commented Sep 25, 2018 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. Commented Sep 25, 2018 at 9:34

You must log in to answer this question.

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