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.
|Estimated completion time||N/A|
Find out if this shell interactive using PS1
The syntax is as follows:
// Is this Shell Interactive?
[ -z "$PS1" ] && echo "Noop" || echo "Yes"
[ -z "$PS1" ] && echo "This shell is not interactive" || echo "This shell is interactive"
[ -z "$PS1" ] && die "This script is not designed to run from $SHELL" 1 || do_interacive_shell_stuff
You can use bash shell if..else..fi syntax as follows:
if [ -z "$PS1" ]; then die "This script is not designed to run from $SHELL" 1 else //call our function do_interacive_shell_stuff fi
From the bash 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:
case "$-" in *i*) echo "This shell is interactive" ;; *) echo "This shell is not interactive" ;; esac
You can also use tty command as follows:
tty -s && echo "This shell is interactive" || echo "This shell is not interactive" ;;
ssh firstname.lastname@example.org tty -s && echo "This shell is interactive" || echo "This shell is not interactive" ;;
References:Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop