Bash Check If Shell Is Interactive or Not Under Linux / Unix Oses

Bash Check If Shell Is Interactive or Not Under Linux / Unix Oses

To check if the shell is interactive or not in Bash, you can use the -i option of the case statement.

The -i option of the case statement matches the current shell mode, which is either interactive or non-interactive.

Here is an example of how to use the -i option of the case statement to check if the shell is interactive:

# Check the current shell mode
case $- in
  *i*)
    # The shell is interactive
    echo "The shell is interactive"
    ;;
  *)
    # The shell is non-interactive
    echo "The shell is non-interactive"
    ;;
esac
Sour‮w:ec‬ww.lautturi.com

This script will print "The shell is interactive" if the shell is running in interactive mode, and "The shell is non-interactive" if the shell is running in non-interactive mode.

You can also use the $- special parameter to check if the shell is interactive or not. The $- special parameter is a string that contains a list of the current options set for the shell. If the i option is set, the shell is interactive. If the i option is not set, the shell is non-interactive.

Here is an example of how to use the $- special parameter to check if the shell is interactive:

if [[ $- == *i* ]]; then
  # The shell is interactive
  echo "The shell is interactive"
else
  # The shell is non-interactive
  echo "The shell is non-interactive"
fi

This script will print "The shell is interactive" if the shell is running in interactive mode, and "The shell is non-interactive" if the shell is running in non-interactive mode.

Created Time:2017-10-27 14:56:34  Author:lautturi