On 15/04/2021 21:36, Martijn Dekker via austin-group-l at The Open Group wrote:
My question: why is this? I would have thought that a script is a script and that it should make no essential difference whether a script is taken from a -c argument or loaded from a file. What makes the optimisation appropriate for one but not the other?

The parsing of script files and -c arguments is different and is meant to be different. With -c, the whole command string is known and parsed in advance. With script files, in general, only as much is read as necessary to parse the next command, that command is executed, and so on. With -c, all shells already know at the time that a command is executed whether it is the last command the shell will execute. When a script file is executed, in the general case, the shell not only does not know, it cannot know when a command is the last command in the script file. The command will almost always be followed by a newline, and determining whether that newline is the last byte of the file requires trying to read at least one more byte. Trying to read at least one more byte is problematic if the file is non-seekable so that the shell cannot restore the state to what it was before that read (needed when the shell is reading from standard input and the fd is shared with other processes), and is problematic if the read may block.

In specific cases, the shell may be able to detect that these issues cannot be a problem. In those specific cases, the optimisation should be perfectly valid and any reasons why it should or should not be performed are not of a technical nature.

Cheers,
Harald van Dijk

  • execve(2) optimisat... Martijn Dekker via austin-group-l at The Open Group
    • Re: execve(2) ... Chet Ramey via austin-group-l at The Open Group
    • Re: execve(2) ... Harald van Dijk via austin-group-l at The Open Group
      • Re: execve... Martijn Dekker via austin-group-l at The Open Group
        • Re: ex... Stephane Chazelas via austin-group-l at The Open Group

Reply via email to