Linux环境变量(一)

1:全局环境变量

  1.1:查看全局变量 printenv/env

yusuwudeMacBook-Pro:bin yusuwu$ printenv
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
...
SHLVL=1
LOGNAME=yusuwu
_=/usr/bin/printenv
yusuwudeMacBook-Pro:bin yusuwu$ 

  1.2:显示个别环境变量的值 printenv EnvironmentVariableName /echo $EnvironmentVariableName

yusuwudeMacBook-Pro:bin yusuwu$ printenv HOME
/Users/yusuwu
yusuwudeMacBook-Pro:bin yusuwu$ env HOME
env: HOME: No such file or directory
yusuwudeMacBook-Pro:bin yusuwu$ echo $HOME
/Users/yusuwu
yusuwudeMacBook-Pro:bin yusuwu$ 

  1.3:全局变量在子shell中通用

yusuwudeMacBook-Pro:bin yusuwu$ bash

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ echo $HOME
/Users/yusuwu
bash-3.2$ exit
exit
yusuwudeMacBook-Pro:bin yusuwu$ 

2:局部环境变量

  2.1查看局部变量

遗憾的是在linux系统中并没有一个只显示局部变量的命令,set命令会显示某个特定进程设置的所有环境变量,包括局部变量,全局变量以及用户自定义变量.

  2.2:设置用户自定义变量

yusuwudeMacBook-Pro:bin yusuwu$ echo $lala

yusuwudeMacBook-Pro:bin yusuwu$ lala=hello
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello
yusuwudeMacBook-Pro:bin yusuwu$ lala=hello world
-bash: world: command not found
yusuwudeMacBook-Pro:bin yusuwu$ lala="hello world"
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello world
yusuwudeMacBook-Pro:bin yusuwu$ lala= "hello ysw"
-bash: hello ysw: command not found
yusuwudeMacBook-Pro:bin yusuwu$ lala ="hello ysw"
-bash: lala: command not found
yusuwudeMacBook-Pro:bin yusuwu$ lala="hello ysw"
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello ysw
yusuwudeMacBook-Pro:bin yusuwu$ /bin/bash 

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ echo $lala

bash-3.2$ exit
exit
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello ysw

通过上面代码可知:

[1]:定义局部变量:变量名=变量值

[2]:变量名,等号和值之间不能有空格,如果在赋值表达式中加上了空格,bash shell 就会把值当成一个单独的命令.

[3]:如果要给变量赋给一个有空格的值,必须用引号来界定字符串的首尾.

[4]:局部变量无法在子shell中使用.

3:设置去全局变量

  3.1:在设定全局环境变量的进程所创建的子进程中,该变量都是可见的.创建全局环境变量的方法是先创建一个局部环境变量,然后将它导出到全局环境中.

这个过程通过export命令来完成,变量名前面不需要加$.

yusuwudeMacBook-Pro:bin yusuwu$ echo $lala

yusuwudeMacBook-Pro:bin yusuwu$ lala="hello ysw"
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello ysw
yusuwudeMacBook-Pro:bin yusuwu$ export lala
yusuwudeMacBook-Pro:bin yusuwu$ bash

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ echo $lala
hello ysw
bash-3.2$ exit
exit
yusuwudeMacBook-Pro:bin yusuwu$ unset lala
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala

yusuwudeMacBook-Pro:bin yusuwu$ 

  3.2:子shell无法使用exprot命令来改变父shell中全局环境变量的值

yusuwudeMacBook-Pro:bin yusuwu$ echo $lala

yusuwudeMacBook-Pro:bin yusuwu$ lala="hello ysw"
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello ysw
yusuwudeMacBook-Pro:bin yusuwu$ export lala
yusuwudeMacBook-Pro:bin yusuwu$ bash

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ echo $lala
hello ysw
bash-3.2$ lala="hello yusuwu"
bash-3.2$ echo $lala
hello yusuwu
bash-3.2$ export lala
bash-3.2$ echo $lala
hello yusuwu
bash-3.2$ exit
exit
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello ysw
yusuwudeMacBook-Pro:bin yusuwu$ bash

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ echo $lala
hello ysw
bash-3.2$ exit
exit
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello ysw
yusuwudeMacBook-Pro:bin yusuwu$ unset lala
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala

yusuwudeMacBook-Pro:bin yusuwu$ 

  3.3:删除环境变量 unset environmentVariable

yusuwudeMacBook-Pro:bin yusuwu$ echo $lala
hello ysw
yusuwudeMacBook-Pro:bin yusuwu$ unset lala
yusuwudeMacBook-Pro:bin yusuwu$ echo $lala

yusuwudeMacBook-Pro:bin yusuwu$ 

 

Linux环境变量(一)

上一篇:linux文件的特殊权限——SUID、SGID、SBIT


下一篇:ubuntu 安装 Protobuf3