Shell Login – How to Check if a Shell is Login, Interactive, or Batch

shell

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

Best Answer

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.