shell variables and environment variables

Shell script global variables, local variables
in different scopes, parameter names that do not interfere with each other, as if a man named Xiao Ming class A students, B classes are also a man named Xiao Ming students,
although they call Xiao Ming (corresponding to variable names), but because of where the class (corresponding to the scope) different, so it will not cause confusion.
But if there is a class with two students called the Little Ming, it must be similar to the "size of that", "Little that" so named to distinguish them.

Shell scoped variables can be divided into three types:

有的变量只能在函数内部使用,这叫做局部变量(local variable);
有的变量可以在当前 Shell 进程中使用,这叫做全局变量(global variable);
而有的变量还可以在子进程中使用,这叫做环境变量(environment variable)。

Shell local variables
Shell also supports custom function, but a different point Shell functions and C ++, Java, C # and other programming languages function is:
variables defined in the Shell function in default is also a global variable, it has a function outside the definition of variables the same effect. Consider the following code:

#!/bin/bash
#定义函数
function func(){
a=99
}
#调用函数
func
#输出函数内部的变量
echo $a

Output:
99

a function is defined in the interior, but the external function can be obtained in its value, prove that it is the global scope, not limited to internal functions.

To variable scope was limited to the function, you can add local command when you define, at which point it becomes a local variable variable. Consider the following code:

#!/bin/bash
#定义函数
function func(){
local a=99
}
#调用函数
func
#输出函数内部的变量
echo $a

The output is empty, indicating that a valid variable in the external function, is a local variable.

This feature and JavaScript Variables in Shell variables are similar. JavaScript variables within the function definition, the default is a global variable, only together with the var keyword, it will become a local variable.

This section simply demonstrates the definition and call functions, and did not make too many grammatical description of the details, we will conduct in-depth follow-up to explain the "Shell function" section.

Shell global variable
called global variables, meaning variables are valid in the current Shell entire process. Shell each process has its own scope, independently of each other with each other. Variables defined in the Shell, the default is the global variable.

Want unrelated demonstration of global variables in different processes of Shell, Shell can open both in a graphical interface, or use two remote terminal connected to the server (SSH).

Shell first open a window, a definition of a variable assignment and 99, and then printed, in the same time window Shell is correctly printed value of a variable.
Then open a new window Shell also print the value of a variable, but the result is null

This shows that a global variable is defined only in its first Shell process effective, no effect on the new Shell process. This is well understood, as xiaowangjia and Alex have a small television set (the same variable name),
but the same time small and small Xu Wang of television programs broadcast can be (different variable values) different.

It should be emphasized that the scope of global variables is the current Shell process, instead of the current Shell script file, they are different concepts. Open a window on Shell Shell created a process
to open multiple windows Shell Shell created a number of processes, each process is independent of Shell, has a different process ID. Shell in a process that can be used to perform multiple commands Shell script source file,
at this time of global variables are valid in the script file.

For example, there are two Shell scripts, which are a.sh and b.sh. a.sh code is as follows:

#!/bin/bash
echo $a
b=200

b.sh code is as follows:

#!/bin/bash
echo $b

Open a Shell window, enter the following command:

[py]$ a=99
[py]$ . ./a.sh
99
[py]$ . ./b.sh
200

These three commands are executed in one process, can be found from the output result, Shell window defined in the form of a command line variables a, the effective a.sh; a.sh the
variables defined in B, in the b.sh also effective, variable b has gone beyond the scope a.sh.

Note that you must run the script in the current process Shell, Shell scripts can not run in a new process, do not understand the readers go to the "Shell script execution."

Shell Environment Variables
Global variables are only valid in the current Shell process, other Shell and child processes are ineffective. If you use the export command to export a global variable,
then it is also valid in all the sub-process, which is called "environment variables."

When the environment variable is created in which the Shell process is called the parent process, if we create a new process in the parent process to execute the Shell command, then this new process is called Shell child process.
When Shell child process is generated, it will inherit the environment variables of the parent process for their own use, so that the environment variables can be passed to the child process from the parent process. Understandably, environment variables can also be passed to grandchild.

Note that there is no parent-child relationship of two Shell process is not passed environment variables, and environment variables can be passed down and can not be passed up, the "father to son does not pass."

Shell to create a child process is the easiest way to run a bash command

Shell layer by layer can exit through the exit command.

The following presentations about the use of environment variables:

[py]$ a=22       #定义一个全局变量
[py]$ echo $a    #在当前Shell中输出a,成功
22
[py]$ bash       #进入Shell子进程
[py]$ echo $a    #在子进程中输出a,失败

[py]$ exit       #退出Shell子进程,返回上一级Shell
exit
[py]$ export a   #将a导出为环境变量
[py]$ bash       #重新进入Shell子进程
[py]$ echo $a    #在子进程中再次输出a,成功
22
[py]$ exit       #退出Shell子进程
exit
[py]$ exit       #退出父进程,结束整个Shell会话

Can be found, by default, a child process in the Shell is invalid; the use of export will be exported to the environment after a variable in the child can use it.

This form is to export a variable defined in a later and then export it as an environment variable, if you want to export, while defined as an environment variable, can be written export a = 22.

We have always emphasized that the environment variables are valid in the Shell sub-process, and it is not effective in all of Shell process; if you create a new Shell window through the terminal,
and that it is not the child of the current Shell, Environment Shell variables of this new process is still ineffective

Environment variables are temporary
by export export environment variables Shell only on the current process and all child processes effective, if the top-most parent process was closed, then the environment variable was also gone,
other processes will not be able to use , so that the environmental variables are temporary.

All problems are solved last minute, if not resolved, you have not to last

In addition, several scripts difference:

(1) / etc / profile: This file is set up for each user environment information system, when a user first logs in, the file is executed and collect shell settings from the configuration file /etc/profile.d directory. .
(2) / etc / bashrc: each user to perform a file operation when the bash shell bash shell is opened, the file is read (i.e., each time a terminal to open, will be executed bashrc)..
(3) ~ / .bash_profile: Each user can use the file input information specific to their use of the shell, when the user logs in, the file is performed only once. By default, set some environment variables, execute .bashrc file users.
(4) ~ / .bashrc: This file contains dedicated to the bash shell bash your information, log on, and when every time you open a new shell when the the file is read.
(5) ~ / .bash_logout: each time when the system exits (exit bash shell), the file is executed Moreover, / etc / profile variables set (global) may be applied to any user, and ~ / .bashrc. variables (local), etc. can only be set in succession / etc / profile of the variables, they are "parent-child" relationship.
(6) ~ / .bash_profile: an interactive, login operation mode into the bash ~ / .bashrc interactive mode into the non-login bash often run both disposed substantially identical, the latter typically will call the former

Published 42 original articles · won praise 3 · Views 1208

Guess you like

Origin blog.csdn.net/qq_40910138/article/details/105195173