Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active August 5, 2023 09:54
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CMCDragonkai/33735c7fa6a2706462f2 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/33735c7fa6a2706462f2 to your computer and use it in GitHub Desktop.
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