Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I think I understand the differences between an interactive, a login and a batch shell. See the following links for more help:

My question is, how can I test with a command/condition if I am on an interactive, a login or a batch shell?

I am looking for a command or condition (that returns true or false) and that I could also place on an if statement. For example:

if [[ condition ]]
   echo "This is a login shell"
fi
share|improve this question
    
There is yet another question: Are STDIN and/or STDOUT connected to a tty (terminal) or a pipe (file or process)? This is a related but distinct test as described in some of the below comments. –  Mark Hudson Oct 10 '14 at 18:04

7 Answers 7

up vote 60 down vote accepted

I'm assuming a bash shell, or similar, since there is no shell listed in the tags.

To check if you are in an interactive shell:

[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'

To check if you are in a login shell:

shopt -q login_shell && echo 'Login shell' || echo 'Not login shell'

By "batch", I assume you mean "not interactive", so the check for an interactive shell should suffice.

share|improve this answer
2  
For zsh users, checking for a login shell can be done with: if [[ -o login ]] ... –  chb Jun 27 '13 at 5:05
1  
If you want to know if a "user" ran your program versus "cron". [[ TERM=="dumb" ]] && echo "Running in cron. –  Erik Aronesty Dec 3 '13 at 17:49
5  
@ErikAronesty Not all dumb terminals are cron sessions. –  Chris Down Dec 4 '13 at 22:11
    
Also for zsh users, to check for interactive sessions: [[ -o interactive ]] –  Frank Terbeck Aug 10 at 16:50

In any Bourne-style shell, the i option indicates whether the shell is interactive:

case $- in
  *i*) echo "This shell is interactive";;
  *) echo "This is a script";;
esac

There's no portable and fully reliable way to test for a login shell. Ksh and zsh add l to $-. Bash sets the login_shell option, which you can query with shopt -q login_shell. Portably, test whether $0 starts with a -: shells normally know that they're login shells because the caller added a - prefix to argument zero (normally the name or path of the executable). This fails to detect shell-specific ways of invoking a login shell (e.g. ash -l).

share|improve this answer
    
(...) because the caller – I think there's something missing in this fragment. –  Piotr Dobrogost Jul 20 at 12:22
    
@PiotrDobrogost fixed, thanks –  Gilles Jul 20 at 12:30

csh / tcsh

For csh and tcsh I have the following in my .cshrc file:

if($?prompt) then               # Only interactive shells set $prompt
    ...
endif

Specifically for tcsh, the variable loginsh is set for a login shell:

if($?loginsh) then              # A login shell..
    ...
endif

(tcsh also has a variable shlvl which is set to the number of nested shells, where the login shell has a value of 1.)

share|improve this answer
2  
PS1 does not work to test for an interactive shell. It's almost always set in an interactive, but you can unset it. It's very often set in a noninteractive shell, because many systems ship with export PS in /etc/profile. –  Gilles Dec 14 '11 at 11:35
    
@Gilles Thank you for the correction and edit –  Andrew Stein Dec 14 '11 at 15:26

Another way is to check the result of tty

if [ "`tty`" != "not a tty" ]; then
share|improve this answer
6  
... or use [ -t 0 ] to test if STDIN is a tty. You can also use 1 (STDOUT) or 2 (STDERR) depending on your needs. –  derobert Dec 13 '11 at 23:17
    
@derobert - thanks for showing me something new –  Adrian Cornish Dec 14 '11 at 3:12
7  
This is a different test. It is possible to have a noninteractive shell whose input is a terminal (anytime you run a script in a terminal!), and it is possible (albeit rare) to have an interactive shell taking input not from a terminal. –  Gilles Dec 14 '11 at 11:34
    
@Gilles if the shell was interactive, and closed leaving a child disown and alive, tty worked best to know it is not interactive anymore, while $- did not change; I am still puzzled about what is the best approach. –  Aquarius Power Jul 25 '14 at 6:14
5  
@AquariusPower Then what you want to test is not for an interactive shell, but whether standard input is a terminal. Use [ -t 0 ]. P.S. In my previous comment, I wrote that “there's a strong correlation” — I forgot “apart from the extremely common case of a script started with #!/bin/sh or the like”, which is non-interactive but can be connected to a terminal. –  Gilles Jul 25 '14 at 7:55

fish shell

Here's the answer for fish in case any other users stumble upon this page.

if status --is-interactive
    # ...
end

if status --is-login
    # ...
end

echo "darn, I really wanted to have to use globs or at least a case statement"

Fish documentation: initialization

share|improve this answer
    
-1 for unnecessary editorializing. –  Nick Bastin Aug 20 at 17:22
1  
If anyone wonders at @NickBastin's comment, he had a fair point so I made an edit. The original amount of snarkiness has now been cut in half. –  ohspite Aug 21 at 3:56
1  
+1 back for usefulness.. :-) –  Nick Bastin Aug 23 at 17:11

Take a look at the shopt command (at least for Bash). That can definitely tell you if you're in a login shell. I don't know about interactive/batch.

Reference: http://www.linuxquestions.org/questions/programming-9/how-to-check-in-a-script-whether-the-shell-is-login-or-non-login-360629/

Look in the Bash man page for more info: http://linux.die.net/man/1/bash

Note: I am giving you Bash, since that's what I know. Presumably other shells have similar functionality.

share|improve this answer

"i" is not the correct option to look for. -i is to force an otherwise non-interactive shell to become interactive. The correct auto-enabled option is -s, but bash unfortunately does not handle this correctly.

You need to check whether $- contains a 's' (this is granted to be auto-activated) or whether it contains a 'i' (this is not granted to be auto-activated but officially only coupled to the -i command line option of the shell).

share|improve this answer
    
s would be if the shell reads commands from stdin, not whether it's interactive. An interactive shell does not necessarily read commands from stdin (try zsh -i < /dev/null, though zsh seems to be the exception here). And a shell may be reading commands from stdin and not be interactive (like sh < file or echo 'echo "$1"' | sh -s foo bar). –  Stéphane Chazelas Aug 18 at 13:59
    
What I wanted to point out is that the original Bourne Shell does not have 'i' in $-, even when it is intended to be interactive. –  schily Aug 18 at 14:07
    
OK, but on U&L, "shell" tend to refer to modern shells. The Bourne shell is generally considered a relique and your own variant is not mainstream enough to expect people to know what you're talking about unless you make it explicit. The question mentioned [[...]] which implies ksh/bash/zsh. You've got a point as a history note that checking for i in $- won't work in the Bourne shell. But then checking for s won't work there reliably either. You'd want to also check for [ -t 0 ] or i; even then that'd be fooled in corner cases like echo 'exec < /dev/tty; that-check' | sh' –  Stéphane Chazelas Aug 18 at 14:24
    
Solaris up to Solaris 10 come with the original Bourne Shell and even sill includes aprox. 10 bugs known to be in the shell since SVr4. So adding a hint on the Bourne Shell is not hat deviously as you might belive. zsh on the other side is not sufficient compatible, it e.g. fails when you try to run "configure" with zsh, so beware to set up /bin/sh to point to zsh. BTW: my Bourne Shell sets -i by default in case it decides to be interactive. –  schily Aug 18 at 14:30
    
What configure does it fail on? What version of zsh? Note that none of the POSIX shells are compatible with the Bourne shell, POSIX broke backward compatibility with the Bourne shell (to the better in most of the cases IMO). Now I would not recommend using zsh as /bin/sh, that would not make sense when you can have leaner, faster implementations like dash/mksh, but it's good zsh has a "sh emulation" mode (historically being Bourne emulation, now converging to POSIX sh) so I can source a "library" file using POSIX syntax. –  Stéphane Chazelas Aug 18 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.