Variables and environment variables
The shell environment usually has many variables, and the variables can be viewed in the form of echo $VAR or ${var}. The SET command can view all variables in the current environment (including general custom variables and environment variables)
The setting of the variable is performed by a simple assignment command. For example, TESTVAR=ABC, that is, set the value of TestVar is ABC (note that there are no spaces on both sides of the equal sign, the value can not have spaces, there are spaces to enclose in quotation marks).
The setting of the undo variable can be done through the unset command, such as Unset TestVar.
There are some special variables in the variables that are called environment variables. The meaning of an environment variable is that it can be inherited by the quilt process and then shared by multiple processes. Because the child process generation mechanism of Linux is fork-and-exec, the environment variables of the child processes are actually a copy of the parent process, and the child processes are independent of the parent process when they start running. The Export command allows you to convert generic custom variables to environment variables for interprocess communication. The ENV command can view all environment variables.
Shell configuration files and environment variables
Connecting to CentOS via SSH is a common scenario. When logging in, the SSHD process opens a virtual terminal subprocess, and then a bash subprocess is opened by the virtual terminal process, which is the origin of all instructions we run through this connection.
Incidentally, there is a welcome message when you open bash, which is configured at/etc/issue (when you log in Via terminal) and/ETC/MOTD (when you log in by using an analog terminal).
Before you formally look at Bash's configuration file, it's important to get to know Login-shell and Non-login-shell first. At its most superficial level, the shell that needs to enter the account password when you open bash is Login-shell, or vice versa. The former example has a login to the Linux system through the terminal or SSH telnet to the analog terminal, the latter example has a graphical interface to open the terminal, or in the terminal to run bash and then open a sub-process shell and so on.
When a bash shell is opened under Cenots, some configuration files are read, and some basic, commonly used variables are loaded into the shell. There are typically four configuration files involved:
A./etc/profile
B. ~/.bash_profile
C. ~/.BASHRC
D./ETC/BASHRC
Typically, the configuration files that are loaded when you open a Login-shell are A and B. (say B is only one.) Bash_profile is also not very rigorous. If the home. Bash_profile does not exist then it will look for. Bash_login, if it does not exist then look for. Profile. Three in the order of priority to find one to load, the other is not the case) but usually, B's concrete execution content has the step of loading C, and C's execution content has the step of loading D, so generally four will be loaded. Since B and C are files of their own in each user's home directory, this may be different and should be noted. The relative A is a file that is shared by all users.
If you open a Non-login-shell, then only C and D will be loaded. It is worth mentioning that D this file is a Redhat system-specific file, in which redhat some of the symbolic configuration (such as the PS1 style). C This file has a backup in the/etc/skel, if accidentally deleted can go there to get a copy back.
In addition to the top four, there are also some configuration files that are closely related to bash's operation.
~/.bash_history records the current bash's command execution history, and its size is related to the environment variable histsize. The history command reads the file. The history command does not write synchronously with our operation, we can imagine this: read this file at login to load the histsize command into memory, because Histsize is usually 1000, with 1000 for example. Then as we continue to enter commands, the memory of these 1000 is constantly updated, the total is always maintained at 1000. The updated 1000 is written to this file again when we log off. Because command logging is associated with the bash process, however. Bash_history a user has only one, so when the same user does different bash processes, the updated information is based on the last user logged out.
~/.bash_logout the interface that leaves the user to do something when logging off. We can write some of the things that the machine will do automatically for you when you log out of the log-in, such as recording the log-out time and so on.
Several differences between running executable programs (including running SH scripts)
For executables, Linux provides a variety of execution methods, according to which the main categories are:
1. Perform the path by path such as relative or absolute:./test.sh,/tmp/test.sh
This method of execution requires that the current user has permission to execute the executed file, and if it is a shell script, the corresponding interpreter such as #!/bin/bash should be declared in the script header. There is no difference between executing a program from the perspective of a relative or absolute path.
Because the execution nature of the General command is also to find the relevant path in the environment variable path to execute, it can be considered as this kind of execution mode. As a result, the execution of a generic command is performed by a shell that opens a child process.
2. Shell command execution
Command execution via shell program such as: Bash test.sh, SH test.sh
The program executed by this method does not have to have execute permission, as long as there is read permission. The executed program runs as a parameter to a shell program.
3. Execute AS Read environment variable
such as: Source test.sh,. test.sh
The biggest difference between this and the first two is that the first two are shells that open a child process in an existing shell, and then place the program in the shell environment of the child process, and then close the child process shell when execution is complete. This approach is performed directly in the current shell environment. So if the executed program contains an incremental change to the environment variable, and you want to keep the result in the current shell environment, you need to use this method to execute.
"Linux" Environment variables and shell configuration & execution