[TUHS] sh: cmd | >file

Steffen Nurpmeso steffen at sdaoden.eu
Sat Jan 4 00:00:55 AEST 2020


markus schnalke wrote in <1inMKv-0Km-00 at marmaro.de>:
 |Hoi,

Morsche.

 |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:
 ...
 |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. ;-)
 ...
 |- Syntax ok in these shells:
 |
 |SVR2 sh (Ultrix), SVR4 sh (Heirloom)

Be aware of spurious :> errors in Heirloom shell, and maybe its
origin (never had those).  It is why the unit test of my little
web site builder thing has been switched to "printf '' > FILE".

 |ksh93
 |bash-1.05, bash-aktuell
 |pdksh-5.2.14
 |ash-0.4.26, dash-0.5.6.1
 |posh-0.3.7, posh-0.12.3
 |mksh-R24, mksh-R52b
 |yash-2.29
 |zsh-3.0.8, zsh-4.3.17
 |
 |- Exception to the rule:
 |
 |7thEd sh:
 |
 |    # pwd|>>file
 |    # echo $?
 |    141
 |
 |On first sight ok, but with a silent error ... SIGPIPE (128+13).
 |
 |I'd be interested in any stories and information around this
 |topic.
 |
 |What about 7thEd sh?

  Beware of bugs in the above code; I have only proved it correct,
  not tried it.

 |meillo
 --End of <1inMKv-0Km-00 at marmaro.de>

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)


More information about the TUHS mailing list