交互式shell和非交互式shell

1050阅读 0评论2014-02-20 three_body
分类:LINUX




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:

点击(此处)折叠或打开

  1. case "$-" in
  2. *i*) echo This shell is interactive ;;
  3. *) echo This shell is not interactive ;;
  4. esac

Alternatively, startup scripts may examine the variable PS1; it is unset in non-interactive shells, and set in interactive shells.  Thus:

点击(此处)折叠或打开

  1. if [ -z "$PS1" ]; then
  2.         echo This shell is not interactive
  3. else
  4.         echo This shell is interactive
  5. fi

上一篇:Login Shells, Interactive Shells
下一篇:In Unix, what startup and termination files do the various shells use?