The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: Chet Ramey <chet.ramey@case.edu>
To: Robert Clausecker <fuz@fuz.su>, tuhs@minnie.tuhs.org
Subject: Re: [TUHS] sh: cmd | >file
Date: Sat, 4 Jan 2020 17:31:31 -0500	[thread overview]
Message-ID: <ba1e897d-c474-2425-b5dd-29f4913abe1f@case.edu> (raw)
In-Reply-To: <20200104114750.GA30769@fuz.su>

On 1/4/20 6:47 AM, Robert Clausecker wrote:

>> This is what wonder about: Why make ``>file'' a simple command,
>> when -- as far as I can currently see -- there is no reason to do
>> so, as all uses could have been achieved in more regular ways as
>> well?

I'm going to answer in terms of the Bourne shell and its successors.

> I think the key reason is that the shell does not build an AST from
> the command you type, instead executing the phrases it encounters as it
> sees them.  

This is false.

> So when the shell sees ">file", it opens "file" for writing
> in preparation of the rest of the command.  

Also not correct, at least if you mean opening it before completing the
parse.

> If no command comes, the
> line is handled the same way as an empty one and the redirections are
> discarded.  

Not really. The shell parses the line into a list of words. Those words
make up a simple command, since none of the words were identified as a
reserved word, and so are not the first word of a compound command. It's
the default case, basically.

The POSIX standard gives a pretty good summary of what happens next:
the redirections get separated out and the rest of the words are
expanded.

You already have to deal with the expansions returning nothing, since the
words could all be null variable expansions, so the case where there are
no words besides redirections and assignment statements isn't that much
different.

> The shell could print an error here, 

It could, but Bourne chose not to.

> but avoiding the side
> effect of opening "file" for lines just consisting of redirects would be
> difficult.  But then, why should it print an error?  The behaviour is
> not harmful after all 

This is true. There aren't any other cases where the shell makes a null
expansion an error without being directly asked, so it would not have
made sense to make a command consisting entirely of words that expand to
null an error. There's no good reason to treat a null command differently
from that case.

> and I'd say that nobody really thought about this
> being a thing when the shell was originally written.

On the contrary, I think Bourne made a conscious choice to handle
redirections `separately' from simple command expansion and execution, and
a conscious choice to continue to process redirections (in a subshell, as
if the shell had forked to execute a non-builtin) when word expansion
resulted in no words.

It's the `exec' case with redirections that I think was the special case,
since Bourne's (v7) shell didn't allow redirections with builtins at all.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

  reply	other threads:[~2020-01-04 22:38 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-04  2:58 Doug McIlroy
2020-01-04 10:07 ` markus schnalke
2020-01-04 11:47   ` Robert Clausecker
2020-01-04 22:31     ` Chet Ramey [this message]
2020-01-04 21:02   ` Dave Horsfall
2020-01-04 21:06     ` Jon Steinhart
2020-01-05  0:03       ` Eric Allman
2020-01-05  1:49         ` Adam Thornton
2020-01-05  2:44           ` Chet Ramey
2020-01-05  8:15             ` Brantley Coile
2020-01-05 15:16               ` Chet Ramey
2020-01-04 21:11     ` Dave Horsfall
2020-01-04 21:39       ` Warner Losh
2020-01-04 22:19         ` Terry Jones
2020-01-04 22:33           ` Chet Ramey
2020-01-04 23:53             ` Dave Horsfall
2020-01-05  0:04               ` Andreas Kusalananda Kähäri
2020-01-05  2:41               ` Chet Ramey
2020-01-05 13:45                 ` Sven Mascheck via TUHS
2020-01-05 15:18                   ` Chet Ramey
2020-01-05 21:21                 ` Dave Horsfall
2020-01-06 13:53                   ` Chet Ramey
2020-01-06 15:42                     ` Brantley Coile
2020-01-06 15:46                       ` arnold
2020-01-06 16:13                         ` Clem Cole
2020-01-06 20:44                           ` arnold
2020-01-06 20:51                             ` Steve Nickolas
2020-01-06 21:32                             ` Clem Cole
2020-01-06 21:39                               ` Brad Spencer
2020-01-06 21:29                         ` Dave Horsfall
2020-01-06 21:55                           ` Chet Ramey
2020-01-06 22:22                             ` Dave Horsfall
2020-01-06 22:52                             ` Dan Cross
2020-01-07  0:50                             ` Adam Thornton
2020-01-06 22:10                           ` Bakul Shah
2020-01-04 22:44           ` markus schnalke
2020-01-04 23:01             ` Terry Jones
2020-01-04 22:22         ` Dave Horsfall
  -- strict thread matches above, loose matches on Subject: below --
2020-01-07  5:03 Brian Walden
2020-01-07  4:49 Brian Walden
2020-01-06 19:47 Doug McIlroy
2020-01-06 16:11 Brian Walden
2020-01-06 16:33 ` Clem Cole
2020-01-06  3:24 Brian Walden
2020-01-06 15:42 ` Richard Salz
2020-01-06 15:45   ` Brantley Coile
2020-01-03 12:45 markus schnalke
2020-01-03 14:00 ` Steffen Nurpmeso
2020-01-03 17:03   ` Brian Zick
2020-01-03 17:18     ` markus schnalke
2020-01-04  0:53       ` Sven Mascheck via TUHS
2020-01-04 20:41         ` Steffen Nurpmeso
2020-01-03 19:38 ` markus schnalke
2020-01-03 19:44   ` Warner Losh
2020-01-03 22:49     ` Michael Parson
2020-01-03 23:32   ` Dave Horsfall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ba1e897d-c474-2425-b5dd-29f4913abe1f@case.edu \
    --to=chet.ramey@case.edu \
    --cc=fuz@fuz.su \
    --cc=tuhs@minnie.tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).