Bash startup environment

Session

Every time the user uses the Shell, a session with the Shell will be opened.

There are two types of sessions: login session and non-login session, which can also be called login shell and non-login shell.

Login Session

The login session is the original session opened for the user after the user logs in to the system, and usually requires the user to enter a user name and password to log in.

Logging in to Session generally initializes the entire system environment, and the startup initialization scripts are as follows.

  • /etc/profile: Global configuration script for all users.
  • All .sh files in the /etc/profile.d directory
  • ~/.bash_profile: The user's personal profile script. If the script exists, it will not be executed after it is executed.
  • ~/.bash_login: If ~/.bash_profile is not found, try to execute this script (the initialization script of C shell). If the script exists, it will not be executed after it is executed.
  • ~/.profile: If both ~/.bash_profile and ~/.bash_login are not found, try to read this script (initialization script of Bourne shell and Korn shell).

When the Linux distribution is updated, the files in /etc will be updated, such as /etc/profile, so do not modify this file directly. If you want to modify the login environment of all users, create a .sh script in the /etc/profile.d directory.

If you want to modify your personal login environment, it is usually written in ~/.bash_profile. The following is a typical .bash_profile file.

# .bash_profile
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
PATH=$PATH:$HOME/bin

SHELL=/bin/bash
MANPATH=/usr/man:/usr/X11/man
EDITOR = / usr / bin / vi
PS1 = '\ h: \ w \ $'
PS2='> '

if [ -f ~/.bashrc ]; then
. ~/.bashrc
be

export PATH
export EDITOR

As you can see, this script defines some of the most basic environment variables, and then executes ~/.bashrc.

The --login parameter of the bash command will force the execution of the script that will be executed by the login session.

$ bash --login

The --noprofile parameter of the bash command will skip the above profile scripts.

$ bash --noprofile

Non-login Session

A non-login session is a manually created session after the user enters the system, and the environment will not be initialized at this time. For example, executing the bash command on the command line will create a new non-login Session.

The initialization script of the non-login Session is as follows.

  • /etc/bash.bashrc: valid for all users.
  • ~/.bashrc: Only valid for the current user.

For users, ~/.bashrc is usually the most important script. Non-login Session will execute it by default, and login Session will usually execute it by calling. Every time a new Bash window is created, it is equivalent to a new non-login Session, so ~/.bashrc will be executed every time. Note that executing the script is equivalent to creating a non-interactive Bash environment, but in this case, ~/.bashrc will not be called.

The --norc parameter of the bash command can prohibit the execution of the ~/.bashrc script in a non-login session.

$ bash --norc

The --rcfile parameter of the bash command specifies another script instead of .bashrc.

$ bash --rcfile testrc

.bash_logout

The ~/.bash_logout script is executed every time you exit the Session. It is usually used to do some cleaning and recording work, such as deleting temporary files and recording the time the user spends in this Session.

If there is no command to be executed when exiting, this file does not need to exist.

Startup options

In order to facilitate debugging, sometimes when starting Bash, you can add startup parameters.

  • -n: Do not run the script, only check for syntax errors.
  • -v: Before outputting the execution result of each statement, the statement will be output first.
  • -x: Before each command is processed, output the command first, and then execute the command.
$ bash -n scriptname
$ bash -v scriptname
$ bash -x scriptname

Keyboard binding

Bash allows users to define their own shortcut keys. The global keyboard binding file defaults to /etc/inputrc, you can create your own keyboard binding file .inputrc in the main directory. If this file is defined, the following line needs to be added to it to ensure that the global binding will not be missed.

$include /etc/inputrc

The shortcut keys in the .inputrc file can be defined like this, "\Ct":"pwd\n" means to bind Ctrl + t to run the pwd command.