A shell initialization file is a shell script that runs automatically each time the shell executes. The initialization file sets up the “work environment” and “customizes” the shell environment for the user. The main agenda of Shell initialization files are to persist common shell configuration, such as:
- $PATH and other environment variables
- shell prompt
- shell tab-completion
- aliases, functions
- key bindings
Before understanding shell initialization files, first understand shell initialization files are dependent on the “combination of shell modes” in which a particular shell process runs. The shell can be run in three possible modes:
- Interactive login
- Interactive non-login
Here are some operations which result in the execution of different combination shell modes
- Login to a remote system via SSH : Login, Interactive
- User successfully login into the system, using /bin/login, after reading credentials stored in the /etc/passwd file: Login, Interactive
- Execute a script remotely and request a terminal, e.g. ssh user@host -t ‘echo $PWD’ : Non-Login, Interactive
- Start a new shell process, e.g. bash : Non‑Login, Interactive
- Execute a script remotely, e.g. ssh user@host ‘echo $PWD : Non‑Login, Non‑Interactive
- Run a script, bash myscript.sh: Non‑Login, Non‑Interactive
- Run an executable with #!/usr/bin/env bash shebang : Non‑Login, Non‑Interactive
- Open a new graphical terminal window/tab: Non‑Login, Interactive
Now, back to our shell initialization files execution based on shell modes.
There are two types of shell initialization files
- System-wide startup files
- User-specific startup files
System-wide Startup Files
These are the initialization files that contain configurations that applied to the whole system irrespective of a specific user, which means all users can share the same configuration which applied in system-wide startup files. System-wide startup files are:
- The /etc/profile file – It stores system-wide environment configurations and startup programs for login setup. All configurations that you want to apply to all system users’ environments should be added in this file.
- The /etc/bashrc or /etc/bash.bashrc file – It contains system-wide functions and aliases including other configurations that apply to all system users.
User-specific startup files
These are the initialization files which contain configuration which applied to the specific user, means all users can have their own configuration which applied in user-specific startup files. User-specific startup files are located in home directory of the user and files are .profile, .bash_profile, .bashrc and .bash_login.
- ~/.bash_profile file – Stores user-specific environment and startup programs configurations.
- ~/.bashrc file – Stores user-specific aliases and functions.
- ~/.bash_login file – Contains specific configurations that are normally only executed when you log in to the system. When the ~/.bash_profile is absent, this file will be read by bash.
- ~/.profile file – It is read in the absence of ~/.bash_profile and ~/.bash_login; it can store the same configurations, which are can also be accessible by other shells on the system. Because we have mainly talked about bash here, take note that other shells might not understand the bash syntax.
- ~/.bash_history file – Bash maintains a history of commands that have been entered by a user on the system. This list of commands is kept in a user’s home directory in the ~/.bash_history file.
- ~/.bash_logout file – it’s not used for shell startup, but stores user specific instructions for the logout procedure. It is read and executed when a user exits from an interactive login shell.
Order of activation of system-wide startup files and user-specific startup files based on shell mode:
1. Non-interactive mode:
source file in $BASH_ENV
2. Interactive login mode:
- ~/.bash_profile, ~/.bash_login, ~/.profile (only first one that exist)
3. Interactive non-login mode:
Now we have a good idea of what are all the shell modes, why shell initialization files are important and how the execution of shell initialization files depends on the different combination shell modes.
With this knowledge, we can achieve modification to shell and access to users based on the shell mode.
Image – https://blog.flowblok.id.au/2013-02/shell-startup-scripts.html
Blog – https://www.tecmint.com/understanding-shell-initialization-files-and-user-profiles-linux/
Opstree is an End to End DevOps solution provider