Alexis Huxley <ahuxley@gmx.net> wrote:
> However, adding a pipe to the command like this:
>
> rm -f fred
> ls fred | cat > fred
> cat fred
>
> gives an unexpected failure:
>
> ls: fred: No such file or directory
>
> Surely the output file should *still* be created
> before the command runs, and so the 'ls' should
> find it when it is executed, and so there should
> not be an error?
The redirection is bound to the command "cat", not the command
"ls fred | cat". The redirection is done before the command it is
bound to, but all the elements of the pipeline run concurrently.
Sometimes fred might be created before ls looks for it; sometimes not.
You can make sure it's created first like this:
{ ls fred | cat; } > fred
paul