Bash automatically executes the contents of files called Bash configuration files on startup (and exit). Bash configuration files are used to change Bash’s default behavior, set environment variables, change terminal interaction, add aliases or functions, and add self-start commands.

Before introducing Bash’s configuration file, you need to understand some basic concepts.

Shell startup type

Login Shell and non-login Shell

When a user logs in to Linux, the operating system automatically assigns a Shell to the user. This Shell is the login Shell.

The Shell that we launch at the terminal by typing the bash command, and the Shell that we launch by shebang when executing the script, are non-login shells.

In short, the first Shell we log into the system is the login Shell, and the following shells are all non-login shells. However, you can force a login Shell using the –login option.

Logout is displayed when the login Shell exits, while exit is displayed when the non-login Shell exits.

Interactive Shell and non-interactive Shell

The Shell that we log in to the system, and the Shell that we start with the bash command, both wait for us to type the command and display the output on the screen. This is the interactive Shell. Interactive shells typically have command prompts, such as [user@host ~]$.

If we call a Shell to execute a script, we don’t wait for commands to be typed, we don’t get a command prompt, and we automatically exit when the script is finished, that’s a non-interactive Shell. You can use the -i option to become an interactive Shell, but the Shell behavior does not change and remains non-interactive.

The interactive Shell’s $- variable contains the letter I, while the non-interactive Shell does not.

The configuration file

Different startup types of Bash read different configuration files.

Login Shell

When Bash is started as a login Shell, the commands in the file /etc/profile are first read and executed.

Bash then looks for the files “~/.bash_profile”, “~/.bash_login”, and “~/.profile”, reading and executing the commands in the first file it finds. That is, if “~/.bash_profile” exists, the commands in “~/.bash_login” and “~/.profile” are not executed.

After the interactive login Shell exits, or the non-interactive login Shell executes the exit command, Bash reads and executes the commands in the ~/.bash_logout file. Of course, execute the command before actually exiting.

Interactive non-login Shell

When Bash starts as an interactive non-login Shell, the commands in the file “~/.bashrc” are read and executed. Some distributions, such as the Debian and Arch Linux systems, first read and execute the “/etc/bash.bashrc” command, but this is not part of the bash specification.

The non-interactive Shell

When Bash is started as a non-interactive Shell, the environment variable “BASH_ENV” is read, and if the value of the variable is a file, the commands in that file are read and executed. The “PATH” PATH is not used to find this file.

Configuration method

In general, there is only one login Shell in a user process tree, which starts before all the other shells. So login Shell configuration files are good for commands that need to be executed only once or can be inherited. General login Shell configuration files are used to: set umask, set environment variables, and add self-boot programs.

It is not recommended to modify the /etc/profile file. Any custom configuration should be modified in user space.

/etc/profile searches for and executes files in the /etc/profile.d directory. If you need to add login Shell configurations for software, you need to create configuration files in the /etc/profile.d directory.

In addition, some software does not read configuration files according to Bash specifications, such as Gnome only reads “~/.profile” in user space, not in the order mentioned earlier. Be careful to modify configuration files when using these software.


Bash, which we opened manually, was essentially non-login and interactive. The configuration files of the interactive non-login Shell are read and executed by the multiple terminal emulators that we open in the graphical interface, or by each layer of nested interactive subshells. The Settings in ~/. Bashrc are mainly related to user interaction, such as setting command prompt and terminal color. In addition, since each layer of subshells executes the contents of this configuration file, the commands in “~/.bashrc” should not be inherited and are required for every interactive Shell.

Aliases and functions are not inherited by the Shell, so they are good for “~/.bashrc” configuration.

It should be noted that interactive non-login shells only have configuration files in user space, not in the “/etc” directory. System administrators should not interfere with how users interact.

If you really want all users to follow some common interaction specifications, you can create the /etc/bashrc configuration file and add the following commands to the ~/. Bashrc file of the user.

[ -f /etc/bashrc ] && source /etc/bashrc
Copy the code

Source can also be abbreviated as.

We also note that the login Shell does not read “~/.bashrc”, but we need the login Shell to have these useful interactions as well. Typically we read the interaction profile by adding the following command to “~/.bash_profile” (or the login Shell profile in the other two userspaces).

[ -f ~/.bashrc ] && source ~/.bashrc
Copy the code

Only ~/.bash_logout can be logged out of the login Shell in user space. If you want to log in to all users, see ~/.bashrc.


We generally don’t create configuration files for non-interactive shells. For non-interactive shells, two things are important.

  • Non-interactive shells inherit environment variables from the parent Shell, so when executing a script, for examplePATHThese environment variables are available.
  • Non-interactive shells do not read “~/.bashrc”, so aliases and functions cannot be used in scripts.

Finally, I found the Bash website for reference and credential for this article. You can also find this in bash’s man manual.

6.2 Bash Startup Files

======================

This section describes how Bash executes its startup files. If any of the files exist but cannot be read, Bash reports an error. Tildes are expanded in filenames as described above under Tilde Expansion (*note Tilde Expansion::).

Interactive shells are described in *note Interactive Shells::.

Invoked as an interactive login shell, or with ‘–login’

………………………………………………..

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the ‘–login’ option, it first reads and executes commands from the file ‘/etc/profile’, if that file exists. After reading that file, it looks for ‘~/.bash_profile’, ‘~/.bash_login’, and ‘~/.profile’, in that order, and reads and executes commands from the first one that exists and is readable. The ‘–noprofile’ option may be used when the shell is started to inhibit this behavior.

When an interactive login shell exits, or a non-interactive login shell executes the ‘exit’ builtin command, Bash reads and executes commands from the file ‘~/.bash_logout’, if it exists.

Invoked as an interactive non-login shell

…………………………………..

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ‘~/.bashrc’, if that file exists. This may be inhibited by using the ‘–norc’ option. The ‘–rcfile FILE’ option will force Bash to read and execute commands from FILE instead of ‘~/.bashrc’.

So, typically, your ‘~/.bash_profile’ contains the line

     if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
Copy the code

after (or before) any login-specific initializations.

Invoked non-interactively

…………………….

When Bash is started non-interactively, to run a shell script, for example, it looks for the variable ‘BASH_ENV’ in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:

     if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
Copy the code

but the value of the ‘PATH’ variable is not used to search for the filename.

As noted above, if a non-interactive shell is invoked with the ‘–login’ option, Bash attempts to read and execute commands from the login shell startup files.