Use Linux environment variables

 

 

使用Linux环境变量
	环境变量
		特性
			内存有关shell会话和工作环境的信息
			允许在内存中存储数据
		分类
			全局变量
				对于shell会话和所有生成的子shell都是可见的
				# printenv
					查看全局变量
				#echo $HOME
					显示变量HOME的值
				#bash
#ps -f
#echo $HOME
#exit
					用bash命令生成子shell,显示HOME环境变量的当前值
			局部变量
				只能定义在它们的进程中可见。只对创建它的shell可见
				#set
					显示为某个特定进程设置的所有环境变量(包括局部变量、全局变量以及用户定义变量)
	设置用户定义变量
		设置局部用户定义变量
			[root@localhost test]# echo $my_var
[root@localhost test]# my_var=Hello
[root@localhost test]# echo $my_var
Hello
			设置局部变量后,可以在shell进程中使用,但是,如果生成另一个shell, 子shell不可用局部变量。
同理:如果在子shell设置局部变量,退出子进程,局部变量不可用。
		设置全局环境变量
			[songpy@localhost test]$ export my_global_var
[songpy@localhost test]$ echo $my_global_var

[songpy@localhost test]$ my_global_var="I am global var now"
[songpy@localhost test]$ echo $my_global_var
I am global var now
[songpy@localhost test]$ bash
[songpy@localhost test]$ echo $my_global_var
I am global var now
[songpy@localhost test]$ exit
exit
[songpy@localhost test]$ echo $my_global_var
I am global var now
[songpy@localhost test]$
				export命令声明全局变量
				启动子shell, 退出子shell可正确显示全局变量的值
	删除环境变量
		[songpy@localhost test]$ unset my_global_var
[songpy@localhost test]$ echo $my_global_var

[songpy@localhost test]$
			unset命令删除变量
			注意:如果在子进程中删除全局变量,只对子进程有效,该全局环境变量在父进程中依然可用
	默认shell环境变量
		#printenv
			例如:HOME
	设置PATH环境变量
		#echo $PATH
			输出可供shell用来查找命令和程序,PATH中的目录用冒号分隔。
		[root@localhost test]# PATH=$PATH:/songpeiying/test/Scripts
[root@localhost test]# echo $PATH
/home/songpy/.local/bin:/home/songpy/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/songpeiying/test/Scripts
			将目录加到PATH环境变量
	定位系统环境变量
		登录shell
			登录Linux系统,会从5个不同的启动文件读取命令
				/etc/profile
				$HOME/.bash_profile
				$HOME/.bashrc
				$HOME/.bash_login
				$HOME/.profile
			# cat /etc/profile
				系统默认bash shell的主启动文件,系统上的每个用户登录时都会咨询这个启动文件
		交互式shell进程
			定义
				启动shell是在命令行提示符下敲入bash时启动
				不会访问/etc/prifle文件,会检查用户HOME目录中的.bashhrc文件
		非交互式shell
			定义
				bash shell是登录系统时启动的
				没有命令行提示符
			[root@localhost ~]# printenv BASH_ENV
[root@localhost ~]# 
[root@localhost ~]# echo $BASH_ENV

[root@localhost ~]#
				BASH_ENV非交互式shell环境变量
				Ubuntu发行版此变量未设置,显示空行
		环境变量持久化
			$HOME/.bashrc文件
				存储个人永久性bash shell变量
			/etc/profiled.d目录
				创建一个以.sh结尾的文件,把所有新的或修改过的全局变量设置放在这个文件夹中。
	数组变量
		[root@localhost ~]# mytest=(one two three four five)
[root@localhost ~]# echo $mytest
one
[root@localhost ~]# echo ${mytest[2]}
three
[root@localhost ~]#
			变量设置多个值,放在括号中,空格分隔
		[root@localhost ~]# echo ${mytest[*]}
one two three four five
[root@localhost ~]# mytest[2]=seven
[root@localhost ~]# echo ${mytest[*]}
one two seven four five
[root@localhost ~]# unset mytest[2]
[root@localhost ~]# echo ${mytest[*]}
one two four five
[root@localhost ~]# echo ${mytest[2]}

[root@localhost ~]# echo ${mytest[3]}
four
[root@localhost ~]# unset mytest
[root@localhost ~]# echo ${mytest[*]}

[root@localhost ~]#
			通配符*, 查看数组值
			某个索引赋值
			unset删除数组某个值
			unset删除数组

Guess you like

Origin blog.csdn.net/songpeiying/article/details/130842609