Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash & ZSH: Interactive and Login Shells
#!/usr/bin/env bash
[[ $- == *i* ]] && echo "This Bash Shell is Interactive Shell" || echo "This Bash Shell is Not a Interactive Shell"
shopt -q login_shell && echo "This Bash Shell is a Login Shell" || echo "This Bash Shell is Not a Login Shell"

Interactive and Login Shells

It doesn't seem like it's possible for there to be a login shell but not interactive.

Cron shell scripts are always non-login and non-interactive.

SSH shells are always login and interactive.

Subshells are always interactive but not login.

Shell scripts are always non-login and non-interactive.

Initial TTY shell should be login and interactive.

Swtching users inside Linux involves creating subshells, can you elect to switch without logging in, or switch while simulate the logging in process. Note that logging in doesn't necessarily mean you need to enter passwords, nor that any password to be entered is possessed by the user you're logging into.

Since .bashrc is read on interactive but not login, and .bash_profile is read on all login shells. People source the .bashrc in the .bash_profile, allowing you to set general configuration (that applies to all interactive shells) inside .bashrc but login specific configuration in .bash_profile.

#!/usr/bin/env zsh
[[ -o interactive ]] && echo "This ZSH Shell is a Interactive Shell" || echo "This ZSH Shell is Not a Interactive Shell"
[[ -o login ]] && echo "This ZSH Shell is a Login Shell" || echo "This ZSH Shell is Not a Login Shell"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment