What is the difference between a login, interactive and non-interactive shell, and which startup files run?
IntermediateConceptShells, scripts and execution
Answer
- Login shell (SSH login,
bash -l): reads/etc/profile, then the first of~/.bash_profile,~/.bash_login,~/.profile. - Interactive non-login (new terminal tab): reads
~/.bashrc. - Non-interactive (scripts, cron,
ssh host cmdmostly): reads nothing, except the file named in$BASH_ENV.
This is why a script works in your terminal but fails in cron: the PATH, aliases and variables from .bashrc are not there. Scripts must set everything they need themselves.
More on Shells, scripts and execution
What is a shell, and how is Bash different from sh, dash and zsh?BeginnerWhat does the shebang line do, and why prefer `#!/usr/bin/env bash` over `#!/bin/bash`?BeginnerWhat is the difference between `./script.sh`, `bash script.sh` and `source script.sh`?BeginnerWhat happens when you type a command name? How does Bash decide what to run?AdvancedWhy must `cd`, `export` and `read` be builtins rather than external programs?IntermediateWhat does `exec` do in a script? Give two uses.Advanced