in a computer forum I came across a very long command line,
including `xargs' and `sh -c'. Anyways, throughout the thread
it was modified several times, when accidently a pipe symbol
appeared between the command and the output redirection. The
command line did nothing; it ran successful. I was confused,
because I expected to see a syntax error in case of
``cmd|>file''. This made me wonder ...
With help of Sven Mascheck, I was able to clear my understanding.
The POSIX shell grammer provided the answer:
pipeline : pipe_sequence
pipe_sequence : command
| pipe_sequence '|' linebreak command
command : simple_command
simple_command : cmd_prefix cmd_word cmd_suffix
| cmd_prefix cmd_word
| cmd_prefix <--- HIER!
| cmd_name cmd_suffix
cmd_prefix : io_redirect
io_redirect : io_file
io_file : '<' filename
| LESSAND filename
| '>' filename
A redirection is a (full) simple_command ... and because
``simple_command | simple_command'' is allowed, so is
``io_file | io_file''. This can lead to such strange (but
valid) command lines like:
<a | >b
b | <a
Sven liked this one:
Here some further fun variants:
They would provide nice puzzles. ;-)
My understanding was helped most by detaching from the
semantics and focussing on syntax. This one is obviously
valid, no matter it has no effect:
From there it was easier to grasp:
a | >a | >a
Which is valid, because ``>a'' is a (complete) simple_command.
Thus, no bug but consistent grammer. ;-)
If one would have liked to forbid such a corner case,
additional special case handling would have been necessary
... which is in contrast to the Unix way.
Sven checked the syntax against various shells with these
- Syntax ok in these shells:
SVR2 sh (Ultrix), SVR4 sh (Heirloom)
- Exception to the rule:
# echo $?
On first sight ok, but with a silent error ... SIGPIPE (128+13).
I'd be interested in any stories and information around this
What about 7thEd sh?