linux and set environment variables inquiry

Linux environment variable settings and queries

In the bash shell, environment variables fall into two categories: global variables, local variables.

Global environment variables for all sub-shell and shell session generated are visible. Local variables are only visible to create their shell.

Global Environment Variables

View the global variables, use the env or printenv command. If you want to view an environment variable, you can use the printenv command, but can not use the env command.

[root@localhost ~]# printenv HOME
/root
[root@localhost ~]# 

The second approach is to view the echo command. But need to add $ dollar sign before the variable view.

[root@localhost ~]# echo $HOME
/root
[root@localhost ~]# 

Local environment variables

Local environment variables visible only in the definition of their processes. Linux system default defines a standard local environment variables. If the custom local variable, then the user-defined variables are called local variables.

Linux is not a specific command to view the local environment variables. Set command will display all environment variables set for a particular process, including local variables, global variables, user-defined variables.

[root@localhost ~]# set

Custom Variables

All environment variable names are in uppercase letters, it is standard practice in the bash shell. If you create a local variable or shell script, use lowercase letters. Variable names are case-sensitive. Stick with lowercase letters when a user-defined local variables involved, it is possible to avoid disaster redefine the system environment variables may bring.

Define local variables

Once you start the bash shell (or execute a shell script), you can create a visible within the shell process local variables. Can be assigned to an environment variable by an equal sign or the value may be a numeric string.

After setting up the local environment variables, you can use it anywhere in the shell process. However, if a generated additional shell, which is not available in the sub-shell.

[root@localhost tmp]# echo $my_var   由于没有my_var变量,打印空行

[root@localhost tmp]# my_var="Hello"   设置my_var变量的值为Hello,如果值没有空格,可以省略双引号
[root@localhost tmp]# echo $my_var   使用my_var变量,需要加上$作为引用,打印出来值
Hello
[root@localhost tmp]#

Define global variables

In the child process to set global environment variables in the process of being created, the variables are visible. Way to create a global environment variables is to create a local environment variable, and then export it to the global environment. This process is done by the export command, in front of variable names do not need to add $.

[root@localhost tmp]# echo $my_var   由于没有my_var变量,打印空行

[root@localhost tmp]# my_var="Hello"   设置my_var变量的值为Hello,如果值没有空格,可以省略双引号
[root@localhost tmp]# echo $my_var   使用my_var变量,需要加上$作为引用,打印出来值
Hello
[root@localhost tmp]# export my_var   将局部变量导入到全局变量中
[root@localhost tmp]# echo $my_var    打印该变量
Hello
[root@localhost tmp]# bash    重新打开一个bash shell进程,测试是否可以访问变量my_var
[root@localhost tmp]# echo $my_var   新的进程依旧可以访问变量
Hello
[root@localhost tmp]# exit   退出当前进程,返回上一个进程
exit
[root@localhost tmp]# echo $my_var   原本的进程有也可以访问。
Hello
[root@localhost tmp]# printenv   打印全局变量
# 其他全局变量省略不显示
my_var=Hello World   刚才导入的全局变量已经显示
_=/usr/bin/printenv
[root@localhost tmp]# 

Modify global sub-shell environment variables will not affect the value of the variable in the parent shell.

[root@localhost tmp]# echo $my_var   父shell的全局变量值
Hello World
[root@localhost tmp]# bash   开启一个子shell
[root@localhost tmp]# echo $my_var  打印子shell的变量值
Hello World
[root@localhost tmp]# my_var="Bash Hello World"   重新设置子shell的变量值
[root@localhost tmp]# echo $my_var   重新打印子shell变量
Bash Hello World
[root@localhost tmp]# exit   退出子shell进程
exit
[root@localhost tmp]# echo $my_var   重新打印父shell进程
Hello World
[root@localhost tmp]# 

Sub-shell can not even use the export command to change the value of the global environment variables of the parent shell. Although sub-shell redefined and derived variables my_variable, but the parent of the shell variable my_variable still retains the original value. So linux, the sub-shell is inoperable parent shell environment variables.

To delete an environment variable

Of course, since you can create a new environment variable, naturally, you can delete the environment variable that already exists. This operation can be done with the unset command. When referencing environment variables unset command, remember not to use $.

[root@localhost tmp]# echo $my_var    打印环境变量
Hello World
[root@localhost tmp]# unset my_var    删除my_var环境变量
[root@localhost tmp]# echo $my_var
[root@localhost tmp]# 

When it comes to the environment variable names, when to use $, when not to use the $, it is confusing.

Remember that on the line: If you want to variables, use $; if you want to operate variable, do not use $. This is a rule

The exception is the use printenv display the value of a variable.

In dealing with the global environment variables, things get a little tricky. If you delete a global environment variable in the child,

Only a child process effective. The global environment is still available in the parent process. Therefore, to remove an environment variable, the parent needs to deal with before they can.

Set Environment Variables

Problems encountered modify the system environment variables. If a variable has been a long, if re-set fallible, so linux provides a method to modify variables expanded.

[root@localhost tmp]# my_var="Hello"  设置环境变量
[root@localhost tmp]# echo $my_var 
Hello
[root@localhost tmp]# my_var=$my_var" World"   环境变量扩展,添加World
[root@localhost tmp]# echo $my_var  重新打印
Hello World
[root@localhost tmp]# 
Published 233 original articles · won praise 189 · views 390 000 +

Guess you like

Origin blog.csdn.net/sinat_32366329/article/details/104081766