6.3.1 What is an Interactive Shell?
An interactive shell
is one started without non-option arguments, unless -s is
specified, without specifying the -c option, and
whose input and error output are both
connected to terminals (as determined by isatty(3)),
or one started with the -i option.
An interactive shell generally reads from and writes to a user’s
terminal.
The -s invocation option may be used to set the positional parameters
when an interactive shell is started.
6.3.2 Is this Shell Interactive?
To determine within a startup script whether or not Bash is
running interactively,
test the value of the ‘-’ special parameter.
It contains i when the shell is interactive. For example:
点击(此处)折叠或打开
case "$-" in
*i*) echo This shell is interactive ;;
*) echo This shell is not interactive ;;
esac
Alternatively, startup scripts may examine the variable
PS1; it is unset in non-interactive shells, and set in
interactive shells. Thus:
点击(此处)折叠或打开
if [ -z "$PS1" ]; then
echo This shell is not interactive
else
echo This shell is interactive
fi