3

What is the order of commands executed which have both pipeline and output redirection in it?

Say we do the following:

Charles@myzone:/tmp$ mkdir /tmp/testdir      
Charles@myzone:/tmp$ cd /tmp/testdir   
Charles@myzone:/tmp/testdir$ touch file1 file2  
Charles@myzone:/tmp/testdir$ ls | wc -l
2
Charles@myzone:/tmp/testdir$ ls | wc -l > ls_result
Charles@myzone:/tmp/testdir$ cat ls_result
3

I know that if you do ls > result then result will contain the name of itself because the shell will do something like

1) create/open file named result 2) set the fd of result to be stdout 3) exec ls

I was expecting ls_result to have value 2, but it's 3.

Question

How is the command ls | wc -w > ls_result above executed ?

Is it equivalent to (ls | wc -w ) > ls_result?

Some links with concerning information ? (I've looked up the bash manual)

14
utility1 | utility2 >output

is not equivalent to

( utility1 | utility2 ) >output

but to

utility1 | { utility2 >output; }

The two utilities are started pretty much the same time, which means you would expect your command to sometimes return 3 and sometimes 2.

Example:

$ { [ -f test ] && echo exists >&2; } | { echo >test; }; rm test
$ { [ -f test ] && echo exists >&2; } | { echo >test; }; rm test
$ { [ -f test ] && echo exists >&2; } | { echo >test; }; rm test
exists
$ { [ -f test ] && echo exists >&2; } | { echo >test; }; rm test
exists
$ { [ -f test ] && echo exists >&2; } | { echo >test; }; rm test
$ { [ -f test ] && echo exists >&2; } | { echo >test; }; rm test

The above shows that the file created by the right hand side of the pipeline is sometimes detected by the left hand side of the pipeline.

3

man bash

REDIRECTION
       Before  a  command is executed, its input and output may be redirected using a special notation interpreted by the shell.  Redirection
       may also be used to open and close files for the current shell execution environment.  The following redirection operators may precede
       or appear anywhere within a simple command or may follow a command.  Redirections are processed in the order they appear, from left to
       right.

so, when you execute command the command, ls_result is being created and then it executes the ls command. That's why the output comes as 3.

ls | wc -l > ls_result

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.