Re: In the POSIX shell standard, why are redirections mixed in with variable assignments and positional parameters?

2016-08-12 Thread Geoff Clare
Mark Galeck  wrote, on 12 Aug 2016:
>
> the formal POSIX shell grammar for yacc symbol "simple_command" accepts 
> commands which can be loosely summarized as:
> three parts, each optional, in order:
> variable assignments and redirections mixed together
> command name
> command arguments and redirections mixed together
> 
> It seems to me, the grammar would be simpler and semantics exactly the same, 
> if we moved all redirections to the end (keeping the same order):
> variable assignmentscommand nameargumentsredirections
> 
> So, why are redirections mixed in and not moved to the end, which would make 
> everything simpler, and commands easier to understand for a casual user?
> Is there a situation where doing so would change how the command behaves?

Bourne-type shells have always allowed redirections to appear anywhere
in a simple command, and POSIX just standardised this existing practice.

Personally if I want to write an error message to standard error I always
put the redirection after the command name.  I have a vague memory that
I started doing it so that when there are multiple commands, the
redirections line up, as in:

echo >&2 "first part of the error message"
echo >&2 "error message part 2"
echo >&2 "third and final part of the error message"

instead of:

echo "first part of the error message" >&2
echo "error message part 2" >&2
echo "third and final part of the error message" >&2

I have also seen pipelines written like this:

< infile filter1 | filter2 | filter3 > outfile

which gives a better "picture" of the data flow than this does:

filter1 < infile | filter2 | filter3 > outfile

-- 
Geoff Clare 
The Open Group, Apex Plaza, Forbury Road, Reading, RG1 1AX, England



In the POSIX shell standard, why are redirections mixed in with variable assignments and positional parameters?

2016-08-12 Thread Mark Galeck
Hello,
the formal POSIX shell grammar for yacc symbol "simple_command" accepts 
commands which can be loosely summarized as:
three parts, each optional, in order:
variable assignments and redirections mixed togethercommand namecommand 
arguments and  redirections mixed together

It seems to me, the grammar would be simpler and semantics exactly the same, if 
we moved all redirections to the end (keeping the same order):
variable assignmentscommand nameargumentsredirections

So, why are redirections mixed in and not moved to the end, which would make 
everything simpler, and commands easier to understand for a casual user?
Is there a situation where doing so would change how the command behaves?
I am sorry if I don't see an obvious reason because of my inexperience with the 
subject matter.
Mark