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

How do I check in GNU/Bash if a shell is running in interactive mode or not while writing shell scripts?

A bash shell is considered as an interactive shell when it reads and writes data from a user’s terminal. Most startup scripts examine the shell variable called PS1. Usually, PS1 is set in interactive shells, and it is unset in non-interactive shells.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo


Find out if this shell interactive using PS1

The syntax is as follows:

// Is this Shell Interactive?
[ -z "$PS1" ] && echo "Noop" || echo "Yes"

Here is another shortcut for us:

[ -z "$PS1" ] && echo "This shell is not interactive" || echo "This shell is interactive"
## do some stuff or die ##
[ -z "$PS1" ] && die "This script is not designed to run from $SHELL" 1 ||  do_interacive_shell_stuff

You can use bash shell syntax as follows:

if [ -z "$PS1" ]; then
       die "This script is not designed to run from $SHELL" 1
       //call our function
Bash Check If Shell Is Interactive or Not on Linux and Unix

Is this shell interactive?

From the bash(1) reference manual:

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:

So we can use the (bash case statement)

 case "$-" in
 *i*)	echo This shell is interactive ;;
 *)	echo This shell is not interactive ;;

OR we can use the if command:

if [[ $- == *i* ]]
    echo "I will do interactive stuff here."
    echo "I will do non-interactive stuff here or simply exit with an error."

Check in bash if a shell is running in interactive mode using the tty command

You can also use tty command as follows:

tty -s && echo "This shell is interactive" || echo "This shell is not interactive" ;;
##OR ##
ssh tty -s && echo "This shell is interactive" || echo "This shell is not interactive" ;;

Use the test command

As per the comment, we can use the test command too:
-t FD file descriptor FD is opened on a terminal
Hence, we can use the following code snippet:

 if [ -t 0 ] ; then
    echo "Doing interactive stuff here in my bash script ..."
    echo "Error ..."


You leaned how to check in GNU/bash if a shell is running in interactive mode or not using various command line options. See GNU/bash man page by typing the following bash command or by visiting this url:
man bash

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

6 comments… add one
  • Shantanu Gadgil Nov 6, 2012 @ 8:40

    Whatever happened to the standard method of checking the fd ‘0’ ???

    if [ -t 0 ]; then
    # do stuff

    Don’t you think that’s a better check that some env variable?

    • jonus Jun 25, 2013 @ 13:00

      that will miss the action on a fresh login.

  • Jalal Hajigholamali Nov 27, 2012 @ 6:53


    Very useful article
    Thanks a lot..

  • Mike Feb 12, 2013 @ 16:36

    Full of holes and not reliable at all. PS1 can be defined by someone in their own .bashrc file for instance.
    You’d have to stat what /dev/fd/0 points to, and sift out the text that is different.

  • felipe1982 Nov 27, 2013 @ 0:09

    @ Mike – what other solution do you propose, if -t 0 isn’t enought, nor is [ -z “$PS1” ]

  • Rey Mar 16, 2016 @ 10:27

    How can I create a user with non-interactive shell?

    Is it just a matter of:

    useradd -s /sbin/nologin lisa


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.