Two questions, four combinations
Bash asks two separate questions when it starts. The first is whether it is a login shell, meaning it was started as the first shell of a session, usually because you signed in on a console or over SSH. The second is whether it is interactive, meaning it is connected to a person who types commands and sees a prompt. Each question has a yes or no answer, so the combinations are login interactive, login non-interactive, non-login interactive, and non-login non-interactive. Most of what you do in a terminal tab is non-login interactive. Most of what a script does is non-login non-interactive.
How to tell which shell you are in
Bash stores its option flags in the special variable $-. When the shell is interactive, the letter i appears in that string. The login_shell option tells you whether the shell is a login shell. The shopt builtin reads it with the -q flag, which means quiet, so the command only sets a status.
bash -c 'case $- in *i*) echo interactive;; *) echo batch;; esac'
bash -c 'shopt -q login_shell && echo login || echo not-login'The first command prints batch, because bash -c runs a command string and is not interactive. The second prints not-login. The -l option asks bash to act as a login shell, and bash -l -c with the same test prints login.
What each kind of shell reads
Login shells read the system profile and then one of the user profile files. Interactive non-login shells read ~/.bashrc. Non-interactive shells read nothing by default, unless the BASH_ENV variable names a file, in which case bash reads that file before it runs the script. The next lesson lists the files in order. The key idea is that a file that is read only at login will not be read again when you open a new terminal tab.
A terminal application may start your shell as a login shell or as a plain interactive shell. Settings are often placed in the wrong file because the behavior depends on that choice. When a setting works in one tab and not another, check which kind of shell each tab is.
Why scripts should not force interactivity
When you need to put interactive-only code in a startup file, guard it with a test on the flags instead of a test on the terminal. The idiom [[ $- == *i* ]] || return near the top of ~/.bashrc stops the rest of the file from running in a non-interactive shell. It is a reliable guard because it asks the shell about itself, and it works the same way in every setup that sources the file.
Scripts usually should not be interactive. If a script has no terminal on its input, an interactive shell prints warnings about job control and waits for input that never arrives. Only add -i when you have a specific reason, such as testing how your startup files behave. Scripts that need a setting should export it explicitly, so they do not depend on whether a particular file happened to be read.
bash -i -c 'echo "$-"' 2>/dev/null < /dev/nullThe command prints the flag string for an interactive shell, which contains i. Redirecting standard error hides the job control warnings that appear when no terminal is attached. Use this pattern for quick tests and never as a model for a real script.
Where each kind shows up
A tab opened from a desktop terminal is usually a non-login interactive shell. A console login, an SSH session, or a shell started with a login flag usually starts a login shell. Scripts started by cron, by a build tool, or by a service manager are usually non-interactive and often non-login. The same user can therefore run bash three different ways, and each way reads different files. That is why a setting that works in your terminal can be missing in a scheduled job, and why a deploy script can behave differently when a person runs it by hand.
The interactive flag also changes behavior inside the shell itself. Job control is on, so Ctrl-Z stops a job and the bg and fg builtins work. Aliases expand. Prompts are displayed, and history is saved. A script that depends on any of these will run differently in a batch context, so check the flags before you assume the behavior.
A quick way to see the consequences is to compare two tabs. Start a command that depends on a variable defined only in the login file, and run it in each tab. The tab that loads the login file has the variable, and the other tab does not. Nothing is broken in either tab. The two shells were simply started in different ways, and the difference is visible only when you ask each shell what kind it is.
The login flag matters less day to day, but it decides which profile file runs. If you change your default shell with chsh, remember that a new login session will start the shell as a login shell, while a terminal that a window manager opens often starts a plain interactive shell. Test both paths when a setting matters.
Practical test for a new environment
When you configure a new machine, check the shell kind before you check anything else. The three questions are simple. Did this shell start as a login shell? Is it interactive? Did the file you expected get read? Answering these first prevents long detours into the wrong file. A login shell is also the place where variables meant for every program should go, because every program started from that session inherits them.