Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping to a process when the process doesn't exist?

Tags:

bash

unix

pipe

Say I start with the following statement, which echo-s a string into the ether:

$ echo "foo" 1>/dev/null

I then submit the following pipeline:

$ echo "foo" | cat -e - 1>/dev/null

I then leave the process out:

$ echo "foo" | 1>/dev/null

Why is this not returning an error message? The documentation on bash and piping doesn't seem to make direct mention of may be the cause. Is there an EOF sent before the first read from echo (or whatever the process is, which is running upstream of the pipe)?

like image 710
Alex Reynolds Avatar asked May 13 '17 01:05

Alex Reynolds


1 Answers

A shell simple command is not required to have a command name. For a command without a command-name:

  • variable assignments apply to the current execution environment. The following will set two variables to argument values:

    arg1=$1 arg3=$3
    
  • redirections occur in a subshell, but the subshell doesn't do anything other than initialize the redirect. The following will truncate or create the indicated file (if you have appropriate permissions):

    >/file/to/empty
    

However, a command must have at least one word. A completely empty command is a syntax error (which is why it is occasionally necessary to use :).

Answer summarized from Posix XCU§2.9.1

like image 84
rici Avatar answered Sep 17 '22 21:09

rici