C.W.K.
Stream
Lesson 03 of 10 · published

Login vs Non-Login, Interactive vs Non-Interactive

~10 min · login-shell, interactive, non-interactive

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Two orthogonal axes

Every shell has two flags:

  • Login or not — login shells are the first shell of a session: Terminal.app on open, ssh login, su -.
  • Interactive or not — interactive shells have a prompt; non-interactive shells run scripts.

That gives 2x2 = four shell types, each loading a different set of config files.

The matrix (zsh)

InteractiveNon-interactive
Login.zshenv → .zprofile → .zshrc → .zlogin.zshenv → .zprofile → .zlogin
Non-login.zshenv → .zshrc.zshenv only

Real-world cases

  • Open Terminal.app → login + interactive (full reload).
  • ssh user@host → login + interactive.
  • ssh user@host 'echo hi' → login + non-interactive.
  • Run ./script.sh from a session → non-login + non-interactive.
  • Type zsh inside an existing zsh → non-login + interactive.
  • cron / launchd jobs → non-login + non-interactive (.zshenv only).

Why this trips people up

You add export OPENAI_API_KEY=... to ~/.zshrc and it works in your terminal. Cron picks up your script and the variable is empty. Cron's shell is non-login + non-interactive, so .zshrc is skipped. Move the line to .zshenv (or have your script source the file explicitly).

Confirm at runtime

[[ -o login ]] && echo login. [[ -o interactive ]] && echo interactive. The flags are introspectable from inside the shell.

Code

Detect what you're running in·zsh
[[ -o login ]]       && echo 'login'       || echo 'non-login'
[[ -o interactive ]] && echo 'interactive' || echo 'non-interactive'

External links

Exercise

From your interactive zsh, run [[ -o interactive ]] && echo yes. Then zsh -c '[[ -o interactive ]] && echo yes || echo no' — the inner shell is non-interactive. Then bash -l -c 'echo $0' to see a login bash.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.