Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active January 25, 2021 00:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilobatistaqueiroz/740e6f1445c03608b4afe82cffb00e42 to your computer and use it in GitHub Desktop.
Save danilobatistaqueiroz/740e6f1445c03608b4afe82cffb00e42 to your computer and use it in GitHub Desktop.
Startup scripts for bash shell

Startup scripts for bash shell

TL;DR

Interactive shell: When reads commands from user input on a tty (prompt).
--interactive sources --> /etc/bash.bashrc and ~/.bashrc
Login shell: When a login prompt is required.
--login sources --> /etc/profile and ~/.profile

Interactive non-login shell: Open a new terminal.
--it sources ~/.bashrc
Non-interactive non-login shell: Run a script.
--it didn't source any bashrc or profile files
Interactive login shell: Open a ssh connection or when a personal computer is started.
--it sources /etc/bash.bashrc,/etc/profile,~/.bashrc,~/.bashrc_profile,~/.profile
Non-interactive login shell: Remote Tools, for example, Git pull/push. Or when Graphical mode is started.
--it sources ~/.profile


.bashrc - executes/sources when is openned a terminal to user type commands.
.profile - executes/sources when a login occurs.


What is Interactive & Non Interactive Shell?

A shell process whose file descriptor 0, 1 and 2 are pointing to the the terminal device (/dev/tty, or /dev/pts/0 etc) is an interactive shell.
A shell where you can type commands(STDIN indicated by file descriptor 0) and see output (STDOUT indicated by file descriptor 1) and errors (STDERR indicated by 2) can be considered as an interactive shell.
Non interactive shells are basically shells launched by other programs.
For example, cron.
Or scripts that starts with SHEBANG (shebang is #!/bin/bash).
SHEBANG is the first line that majority of the scripts have in common.

What is login shell in Bash?

When you ssh to a remote server, the ssh program actually triggers the login program (which will intern read /etc/login.defs file and verifies password, shell name, home directory and things that are inside /etc/passwd file).
After the authentication succeeds, ssh program will then trigger the shell program.

/etc/bash.bashrc

System-wide .bashrc file for (interactive) bash shells

/etc/profile

System-wide .profile file for (login/session) Bourne shell sh and Bourne compatible shells bash, ksh, ash

~/.bash_profile

Things that are specific to your (interactive) + (login/session) bash shells

~/.profile

Executed by the command interpreter for (login/session) shells
This file is not read by bash, if ~/.bash_profile or ~/.bash_login exists

~/.bashrc

Things that is specific to (interactive) bash shell itself
When bash shell is started as a non login shell in interactive mode

When bash shell is started as a non login interactive shell
(for example, opening a new bash window).
Flow of configuration lookup is below:
It first searches for a file named /etc/bash.bashrc, if this file exist, then it executes it and proceed to the below.
It searches for a file named ~/.bashrc, and if found, it executes it.

Now let us see what bash does when it is triggered as a login shell
(interactive or non-interactive).
In other words, either while logging in using ssh or a terminal, or if you execute bash from an already existing shell with --login parameter.
It does the following.
First it tries to read /etc/profile
If it does not exist, it proceeds to the below
If it exist, then it will execute it and then proceed to below mentioned items
It searches for a file named ~/.bash_profile
If it finds that file, it is executed and then does not look for anything mentioned below
If ~/.bash_profile is not present, it will look for another file named ~/.bash_login
If ~/.bash_profile and ~/.bash_login files are absent, then it will look for a file named ~/.profile
This ~/.profile is actually read by almost all shells

Summary:

login session starts reading: /etc/profile, (~/.bash_profile or ~/.bash_login or ~/.profile)
non login interactive shell starts reading: /etc/bash.bashrc, ~/.bashrc

references:

https://www.slashroot.in/difference-between-bashrc-and-bashprofile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment