How do you check in GNU/Bash if a shell interacts when writing shell scripts or not?
The bash shell is considered an interactive shell when it reads and writes data from the user’s terminal. Most startup scripts control a shell variable called PS1. The PS1 is normally installed in interactive shells and not in non-interactive shells.
Check if this shell is interactive with the PS1
The syntax is as follows:
|// Is the shell interactive?
[ -z $PS1 ] && echo Noop [ echo Yes.
// Is this Shell Interactive?
[ -z “$PS1” ] && echo “Noop” || echo “Yes”
Here’s another shortcut for us:
|This shell is not interactive. This shell is interactive
## do stuff or die ##
[ -z $PS1 ] && die This script is not designed to run from $SHELL 1 | do_interacive_shell_stuff.
[ -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
They could use the turret as a… if you don’t… …the syntax is like this:
|if [ -z $PS1 ] ; then
the This script is not designed to run $SHELL 1
// call our function
if [ -z “$PS1” ]; then
die “This script is not designed to run from $SHELL” 1
//call our function
Is this shell interactive?
Extract from the Bass Reference Manual(1) :
To determine in the startup script whether the bash is interactive or not, check the value of the special parameter -. It contains i if it is an interactive shell. For example:
So we can close the case… в… Esake (baseball coach)
|register $- in
*i*) echo This shell is interactive;;;;;
*) echo This shell is not interactive;;;;;
case “$-” in
*i*) echo This shell is interactive ;;
*) echo This shell is not interactive ;;
Or we could use the if-team:
|if [… $- ]
echo, I’ll do interactive things here.
echo I will do non-interactive things here or just go out with an error.
if [[ $- == *i* ]]
echo “I will do interactive stuff here.”
echo “I will do non-interactive stuff here or simply exit with an error.”
In bash, check that the shell interacts with the ttycommand.
You can also use the command tty as follows:
|tty -s && echo This shell is interactive | echo This shell is not interactive ;;;
ssh [email protected] tty -s && echo This shell is interactive | echo This shell is not interactive ;
tty -s && echo “This shell is interactive” || echo “This shell is not interactive” ;;
ssh [email protected] tty -s && echo “This shell is interactive” || echo “This shell is not interactive” ;;
Use test command
Depending on the remark, we can also use the following verification command:
-t FD file descriptor is opened on terminal
|if [ -t 0 ] ; then
echo does interactive things here in my bash script…
another echo error
if [ -t 0 ] ; then
echo “Doing interactive stuff here in my bash script …”
echo “Error …”
They looked at GNU/bash to check if the shell is interactive or not using different command line options. Look, look, look, look, look, look. The GNU/bash man page by typing the following bash command or by visiting this URL:
Published: Vivek gears
The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and Linux/Unix shell script trainer. Receive the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly by email.non interactive shell example,interactive shell script,bash,tty