Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: exec - interactive vs non-interactive shell

On 01/06/2011 22:42, Peter Stephenson wrote:
On Wed, 01 Jun 2011 21:55:04 +0200
"Radoulov, Dimitre"<cichomitiko@xxxxxxxxx>  wrote:
zsh-4.3.11[t]% cat a_script
exec cat

zsh-4.3.11[t]% zsh<   a_script
So cat has taken over stdin, as expected.

ash, dash and bash, for example, behave differently (ksh and variants seem to behave like zsh).
Do you know if this behavior is defined by the POSIX standard?

zsh-4.3.11[t]% zsh -i<   a_script

No output with -i.
You're confusing the effects of "zsh -i<a_script" with "zsh -i
a_script".  The first one starts up zsh as an interactive shell, with a
line editor, but with standard input (not commands, which are
interactive) coming from a_script.  You'll find that prompt on the line
after is inside the new interactive shell.  Try typing at this point:

print $REPLY

You should see "exec ok".

The rules for how zsh finds the commands and standard input are roughly:

- Standard input is only redirected if you explicitly use a "<".
(So "zsh a_script" leaves stdin where it is and cat hangs waiting for
input.)  It's independent of interaction with the terminal for reading
commands (though there's the more complicated issue of how the terminal
gets set up in the first place).

- If there's a script name, use that for commands, regardless of whether
the shell is interactive or not.  (So "zsh -i a_script" behaves
basically like without the -i, although you could have interactive shell
commands such as vared in the script.)

- If there's no script name and the shell is interactive, use the
terminal for commands.

- If there's no script name and the shell is not interactive, fall back
to reading commands from standard input.  This is the only case (without
making special arrangements) where commands and standard input come from
the same channel.

If the shell is interactive, there's no script name nor explicit input redirection (<) ,
command and standard input come from the same channel too (the terminal),
or I'm missing something?

Thank you for the explanation!


Messages sorted by: Reverse Date, Date, Thread, Author