41

Are the concepts of login/non-login shells the same as the concepts of non-interactive/interactive shells (respectively)?

Or are the concepts orthogonal yielding four different combinations?

I am trying to get a clear picture of which .bashrc, .bash_profile scripts get sourced under various circumstances and I find that articles sometimes use these concepts interchangeably.

Marcus Junius Brutus
  • 4,587
  • 11
  • 44
  • 65
  • 2
    Specifically focusing on which scripts are sourced makes this a worthwhile question.. could the title be edited? – KCD Jul 19 '18 at 01:51

1 Answers1

73
  • login shell: A login shell logs you into the system as a specific user. Necessary for this is a username and password. When you hit ctrl+alt+F1 to login into a virtual terminal you get after successful login: a login shell (that is interactive). Sourced files:

    • /etc/profile and ~/.profile for Bourne compatible shells (and /etc/profile.d/*)
    • ~/.bash_profile for bash
    • /etc/zprofile and ~/.zprofile for zsh
    • /etc/csh.login and ~/.login for csh
  • non-login shell: A shell that is executed without logging in. Necessary for this is a current logged in user. When you open a graphic terminal in gnome, it is a non-login (interactive) shell. Sourced files:

    • /etc/bashrc and ~/.bashrc for bash
  • interactive shell: A shell (login or non-login) where you can interactively type or interrupt commands, for example, a gnome terminal (non-login) or a virtual terminal (login). In an interactive shell the prompt variable must be set ($PS1). Sourced files:

    • /etc/profile and ~/.profile
    • /etc/bashrc or /etc/bash.bashrc for bash
  • non-interactive shell: A (sub)shell that is probably run from an automated process. You will see neither input nor output when the calling process doesn't handle it. That shell is normally a non-login shell, because the calling user has logged in already. A shell running a script is always a non-interactive shell, but the script can emulate an interactive shell by prompting the user to input values. Sourced files:

    • /etc/bashrc or /etc/bash.bashrc for bash (but, mostly you see this at the beginning of the script: [ -z "$PS1" ] && return. That means don't do anything if it's a non-interactive shell).
    • depending on shell; some of them read the file in the $ENV variable.
chaos
  • 48,171
  • Nice explanation, but I still don't know why "That shell is normally a non-login shell, because the calling user has logged in already"? – L_K Jun 20 '17 at 02:41
  • Is there an easy way to add scripts to be run in non-interactive shells? – ceztko Jun 18 '18 at 10:36
  • Nothing clean, but see https://superuser.com/q/1205681/81584 – KCD Jul 19 '18 at 01:54
  • 1
    [ -z "$PS1" ] is an unsafe way of checking for interactive mode, because it could be part of the environment. [[ $- != *i* ]] is more secure. – Bachsau Jul 18 '20 at 09:52
  • @Bachsau "/etc/bash.bashrc and ~/.bashrc are also run in non-interative shells." -- I'm confused. man bash says /etc/bash.bashrc and ~/.bashrc are only executed for interactive (non-login) shells. The comments in /etc/bash.bashrc (on Ubuntu 22.04) say the same. – balu Jan 13 '24 at 02:05
  • @Bachsau Yeah, that's been confusing me, too. More generally, every source I've consulted seems to be saying something slightly different about when exactly .profile and .bashrc get sourced. Anyway, thanks for looking into this! I appreciate it! – balu Jan 14 '24 at 21:52