Linux configuration file load order and modify Shell command prompt and color

Environment variables are initialized with the entry into force of the order corresponding to the file

Linux system when you log in and start a bash shell, bash by default looks set environment variables in several files. These files are collectively referred to as the file system environment. Check the situation bash environment variables file depends on the shell of the way the system is running. The system is generally run mode shell 3:

  • The default login shell to run through the system user
  • Non-login interactive shell running
  • Execute scripts to run non-interactive shell

How to determine whether the Interactive Shell? There are two ways

1, view special variables - if the value contains i, is an interactive, non-interactive or

Echo $ $ -

2, view the PS1 variable is empty, if not empty, it is interactive, or non-interactive

$ Echo $ PS1

to determine whether the login type Shell?

Bash obtain complete login process is needed, is called login shell, the method need not be repeated to obtain sign bash interface operation is called non-login shell.
Excuting an order

shopt login_shell

If the value represented by log on formula (login shell), is represented by a non-log off of formula (non-login shell).

At the same time judge and an interactive logon type

$ echo $PS1; shopt login_shell
————————————————

 

When a user logs on linux, shell will start as a login shell. Sequentially loading the login shell environment variables at this time is shown in FIG.


 

First, the user logs system loads /etc/profilethe global environment variable file, which is the default shell on Linux system environment variables master file. Each user login will load this file on your system.

When finished loading /etc/profilethe file, will execute /etc/profile.dthe script file in the directory, there are a lot of scripts in this directory.

After the start of operation $HOME/.bash_profile(user environment variables file), in this file, you will find $HOME/.bashrc(user environment variables file), if any, is executed, and if not, is not performed. In the $HOME/.bashrcdocument will go to /etc/bashrc(global environment variable file), if any, is executed, and if not, is not performed.

If the user starts when the shell is not a login (such as manual Qiaoxia bash start to enter a password or other unwanted ssh remote login and link the case), then this will only non-login shell is loaded $HOME/.bashrc(user environment configuration file), and will went to /etc/bashrc(global environment variable file). So if you want a non-login shell also can read the contents of setting environment variables, you need to set the variables and other written $HOME/.bashrcor /etc/bashrcinstead of $HOME/.bash_profile, or /etc/profile.

 

Linux Shell command prompt and modify colors

Shell command prompt and color is configured by PS1:

1 [root@localhost ~]$ echo $PS1
2 \ [\ e [32; 40m \] [\ u @ \ h \ W] $ \ e [m

Wherein PS1 common parameters are as follows:

\ d: # represents the date format weekday month date, for example: "Mon-Aug. 1"
\ H: # full host name
\ h: # take only the first host name
\ t: # display time format 24 hours such as: HH: MM: SS
\ T: # display for 12-hour format
\ a: # show time is 24 hour format: HH: MM
\ U: # current user account name
\ v: #BASH version information
\ w: # full name of the working directory
\ W: # use basename get working directory name, it lists only the last directory
\ #: # the first several commands issued
\ $: # prompt character, if a root, tips operators are: #, compared with the average user: $

3. Color value

PS1, the character color setting format: \ [\ e [F; Bm \], where "F" is a font color, No. 30-37, "B" is the background color, numbered 40-47. With \ e [m ending color settings, color table as follows:
 

B F.
30 Black 40
31 41 Red
32 42 Green
3343 Yellow
3444 Blue
3545 purple
3646 cyan
3747 White 

Code Meaning
-------------------------
0 OFF
. 1 Highlight
. 4 underline
. 5 scintillator
7 Highlight
8 invisible

The color table, the character color setting applies the format, it can command the terminal linux personalize the colors. For example, to set the format of the command line for the green on black, shows the current user's account name,
The first name of the host, complete the current working directory name, 24-hour time format, you can use the following command:
# PS1 = '[\ [\ e [32; 40m \] \ u @ \ h \ w \ t] \ $'
~ / .Bashrc configuration
 1 [root@localhost ~]$ cat ~/.bashrc
 2 # .bashrc
 3 
 4 # User specific aliases and functions
 5 
 6 alias rm = 'rm -i'
 7 alias cp = 'cp -i'
 8 alias etc. = 'etc. -i'
 9 
10 PS1="\[\e[32;40m\][\u@\h \W]\$\e[m "
11 
12 # Source global definitions
13 if [ -f /etc/bashrc ]; then
14     . /etc/bashrc
15 be

Under reload ~ / .bashrc to take effect:

[root@localhost ~]$ source ~/.bashrc
 
 
 

Guess you like

Origin www.cnblogs.com/bz310/p/12006596.html