Bash Reference Manual. Node: Is This Shell Interactive?

PREVBash Startup Files UPBash Features NEXTBash Builtins

5.3: Is This Shell Interactive?

As defined in Invoking Bash, an interactive shell is one whose input and output are both connected to terminals (as determined by isatty(3)), or one started with the `-i' option.

To determine within a startup script whether Bash is running interactively or not, 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

Alternatively, startup scripts may 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
PREVBash Startup Files UPBash Features NEXTBash Builtins