How to check if a shell is login/interactive/batch
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.
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
).
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