Linux环境变量详解与应用
在bash shell中,环境变量分为:
>全局变量
>局部变量
全局变量,不仅对shell可见,对其子进程也可见
查看预设的全局环境变量:
ghostwu@dev:~$ printenv
ghostwu@dev:~$ env
这两个命令都可以打印全局环境变量
ghostwu@dev:~$ printenv | grep HOME
HOME=/home/ghostwu
ghostwu@dev:~$
HOME是一个全局环境变量,保存用户的家目录
ghostwu@dev:~$ echo $HOME
/home/ghostwu
上面说了,全局环境变量对子shell也有用,我们就开启一个子进程,来验证一下:
ghostwu@dev:~$ bash
ghostwu@dev:~$ ps -f
UID PID PPID C STIME TTY TIME CMD
ghostwu : pts/ :: bash
ghostwu : pts/ :: bash
ghostwu : pts/ :: ps -f
ghostwu@dev:~$ echo $HOME
/home/ghostwu
ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $HOME
/home/ghostwu
由此可见,在当前的shell 以及子shell中 都能访问到全局环境变量
局部环境变量:只能在定义他们他们的进程中可见
设置局部变量,跟php的语法差不多,只不过不需要美元符号,读取局部变量需要用$符号
ghostwu@dev:~$ echo $my_var
读取一个没有定义的局部环境变量,值为空
ghostwu@dev:~$ echo $my_var ghostwu@dev:~$ my_var=ghostwu
ghostwu@dev:~$ echo $my_var
ghostwu
定义局部变量的=号左右不要用空格,否则会被当做命令执行
ghostwu@dev:~$ myvar = 'hello'
myvar: command not found
在子shell中,是不能访问到父shell(进程)定义的局部变量.
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ ps -f
UID PID PPID C STIME TTY TIME CMD
ghostwu : pts/ :: bash
ghostwu : pts/ :: ps -f
ghostwu@dev:~$ bash
ghostwu@dev:~$ echo $my_var ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $my_var
ghostwu
如果需要在子shell中访问到父shell中定义的局部变量,我们可以把局部变量导入到全局变量中,怎么导入?用export 变量名称 即可,导入的时候,变量名称前面
不要用$符号
ghostwu@dev:~$ printenv | grep my_var
ghostwu@dev:~$ export my_var
ghostwu@dev:~$ !p
printenv | grep my_var
my_var=ghostwu
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ bash
ghostwu@dev:~$ ps -f
UID PID PPID C STIME TTY TIME CMD
ghostwu : pts/ :: bash
ghostwu : pts/ :: bash
ghostwu : pts/ :: ps -f
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ printenv | grep my_var
my_var=ghostwu
删除环境变量,用unset。
>在父shell中,删除一个导入到全局变量的 环境变量,之后就访问不到这个变量了
ghostwu@dev:~$ printenv | grep my_var
my_var=ghostwu
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ unset my_var
ghostwu@dev:~$ echo $my_var ghostwu@dev:~$ bash
ghostwu@dev:~$ echo $my_var ghostwu@dev:~$ exit
exit
>如果在子shell中,导入一个全局变量,然后删除,那么子shell中访问不到,但是在父shell中,还可以访问到
ghostwu@dev:~$ echo $my_var ghostwu@dev:~$ my_var="ghostwu"
ghostwu@dev:~$ export my_var
ghostwu@dev:~$ printenv | grep my_var
my_var=ghostwu
ghostwu@dev:~$ bash
ghostwu@dev:~$ echo $my_var
ghostwu
ghostwu@dev:~$ unset my_var
ghostwu@dev:~$ echo $my_var ghostwu@dev:~$ exit
exit
ghostwu@dev:~$ echo $my_var
ghostwu
PATH环境变量,定义了一堆路径,这些路径的作用是,当命令行输入一个命令的时候,在去PATH变量中定义的路径中去寻找。所以,如果想让一个自定义的可执行
程序能够在任意目录下执行,就需要把这个可执行的程序所在的目录设置到PATH环境变量。怎么设置呢? 在PATH变量中,路径用:号分隔。
假如,我在root的家目录下面创建了一个bin目录,用来放一个shell脚本,名字为ghost
ghostwu@dev:~/bin$ pwd
/home/ghostwu/bin
ghostwu@dev:~/bin$ ls -l
total
-rwxrwxr-x ghostwu ghostwu 5月 : ghost
ghostwu@dev:~/bin$ cat ghost
#!/bin/bash
ls -l /
ghostwu@dev:~/bin$ ./ghost
total
drwxr-xr-x root root 5月 : bin
drwxr-xr-x root root 2月 : boot
drwxr-xr-x root root 2月 : cdrom
drwxr-xr-x root root 5月 : dev
drwxr-xr-x root root 5月 : etc
drwxr-xr-x root root 2月 : home
lrwxrwxrwx root root 2月 : initrd.img -> boot/initrd.img-4.10.--generic
drwxr-xr-x root root 5月 : lib
drwxr-xr-x root root 5月 : lib64
drwx------ root root 2月 : lost+found
drwxr-xr-x root root 2月 : media
drwxr-xr-x root root 4月 : mnt
drwxr-xr-x root root 5月 : opt
drwxr-xr-x root root 5月 : patch
dr-xr-xr-x root root 5月 : proc
drwx------ root root 5月 : root
drwxr-xr-x root root 5月 : run
drwxr-xr-x root root 2月 : sbin
drwxr-xr-x root root 4月 snap
drwxr-xr-x root root 8月 srv
dr-xr-xr-x root root 5月 : sys
drwxrwxrwt root root 5月 : tmp
drwxr-xr-x root root 8月 usr
drwxr-xr-x root root 5月 : var
lrwxrwxrwx root root 2月 : vmlinuz -> boot/vmlinuz-4.10.--generic
drwxr-xr-x root root 5月 : www
由于我的PATH变量,已经包含了/home/ghostwu/bin这个路径,所以在任意的目录下,都会搜素这个目录,就会执行ghost
ghostwu@dev:~$ cd /etc
ghostwu@dev:/etc$ ghost
total
drwxr-xr-x root root 5月 : bin
drwxr-xr-x root root 2月 : boot
drwxr-xr-x root root 2月 : cdrom
drwxr-xr-x root root 5月 : dev
drwxr-xr-x root root 5月 : etc
drwxr-xr-x root root 2月 : home
lrwxrwxrwx root root 2月 : initrd.img -> boot/initrd.img-4.10.--generic
drwxr-xr-x root root 5月 : lib
drwxr-xr-x root root 5月 : lib64
drwx------ root root 2月 : lost+found
drwxr-xr-x root root 2月 : media
drwxr-xr-x root root 4月 : mnt
drwxr-xr-x root root 5月 : opt
drwxr-xr-x root root 5月 : patch
dr-xr-xr-x root root 5月 : proc
drwx------ root root 5月 : root
drwxr-xr-x root root 5月 : run
drwxr-xr-x root root 2月 : sbin
drwxr-xr-x root root 4月 snap
drwxr-xr-x root root 8月 srv
dr-xr-xr-x root root 5月 : sys
drwxrwxrwt root root 5月 : tmp
drwxr-xr-x root root 8月 usr
drwxr-xr-x root root 5月 : var
lrwxrwxrwx root root 2月 : vmlinuz -> boot/vmlinuz-4.10.--generic
drwxr-xr-x root root 5月 : www
在ubuntu16.04下面,这个路径设置是在.profile这个文件中的,而用户登录系统时,会执行这个.profile,所以PATH中的设置就会生效,接下来把他删除.
ghostwu@dev:~$ tail - .profile
PATH="$HOME/bin:$HOME/.local/bin:$PATH"
删除之后,你会发现,他依然存在,但是文件中确实是删除了,这个时候,我们要重启系统.
ghostwu@dev:~$ vim .profile
ghostwu@dev:~$ tail - .profile
#PATH="$HOME/bin:$HOME/.local/bin:$PATH"
PATH="$HOME/.local/bin:$PATH"
ghostwu@dev:~$ echo $PATH
/home/ghostwu/bin:/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
注销系统也可以,我们删除的路径,已经生效了
ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ghostwu@dev:~$ cd /etc
ghostwu@dev:/etc$ ghost ghostwu@dev:/etc$
我们可以临时把路径设置回去,在bin目录下创建一个脚本ghost2.,没有设置路径之前,ghost2是找不到的
ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ghostwu@dev:~$ cat ~/bin/ghost2
#!/bin/bash
ls -l /etc
ghostwu@dev:~$ ghost2
ghost2: command not found
现在,就生效了,但是这个生效,在当前shell进程退出,或者另外的shell里面访问,是访问不到的,如果我们想永久让设置的环境保存下来,应该把他写在文件中,
一般写在下面4个文件中
/etc/profile: 这个是所有登录的用户都会加载的文件
$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile
后面3个文件是用户专用。我们之前的变量就是设置在.profile中。其实,还有一个文件,也可以设置:.bashrc。为甚呢?因为.profile会判断是否存在.bashrc。从而
加载.bashrc。所以在.bashrc中设置的环境变量,也会永久保存在当前用户环境下.
ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ghostwu@dev:~$ cat ~/bin/ghost2
#!/bin/bash
ls -l /etc
ghostwu@dev:~$ ghost2
ghost2: command not found
ghostwu@dev:~$
ghostwu@dev:~$ PATH=$PATH:$HOME/bin
ghostwu@dev:~$ echo $PATH
/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ghostwu/bin
ghostwu@dev:~$ ghost2
total
drwxr-xr-x root root 8月 acpi
....
Linux环境变量详解与应用的更多相关文章
- Linux 环境变量详解
1. $HOME变量从哪里获得的? HOME 本身是 shell 来设置的,你可以自己手工设置(例如使用 bash 的 export),也可以在 shell 的初始化文件中设置(例如 bash 的 . ...
- net core体系-web应用程序-4net core2.0大白话带你入门-5asp.net core环境变量详解
asp.net core环境变量详解 环境变量详解 Windows操作系统的环境变量在哪设置应该都知道了. Linux(centos版本)的环境变量在/etc/profile里面进行设置.用户级的 ...
- asp.net core环境变量详解
环境变量详解 Windows操作系统的环境变量在哪设置应该都知道了. Linux(centos版本)的环境变量在/etc/profile里面进行设置.用户级的环境变量在其它文件里面,不多说了,有兴趣的 ...
- 【转】asp.net core环境变量详解
asp.net core环境变量详解 环境变量详解 Windows操作系统的环境变量在哪设置应该都知道了. Linux(centos版本)的环境变量在/etc/profile里面进行设置.用户级的环境 ...
- Maya 常用环境变量详解
Maya 常用环境变量详解 前言: Maya 的环境变量让用户可以很方便的自定义 Maya 的功能. 在 Maya 的 Help 帮助文档中有专门的一个章节< Environment Varia ...
- 如何查找YUM安装的JAVA_HOME环境变量详解
如何查找YUM安装的JAVA_HOME环境变量详解 更新时间:2017年10月27日 09:44:56 作者:铁锚 我要评论 这篇文章主要给大家介绍了关于如何查找YUM安装的JAVA_HOM ...
- .NET Core 环境变量详解
一.概述 软件从开发到正式上线,在这个过程中我们会分为多个阶段,通常会有开发.测试.以及上线等.每个阶段对应的环境参数配置我们会使用不同的参数.比如数据库的连接字符串,开发环境一般我们都是连接的测试库 ...
- PATH环境变量和CLASSPATH环境变量详解
大凡装过JDK的人都知道要安装完成后要设置环境变量,可是为什么要设置环境变量呢?环境变量有什么作用? 1)PATH详解: 计算机安装JDK之后,输入“javac”“java”之类的命令是不能马上被计算 ...
- Hyperledger Fabric 通道配置文件和容器环境变量详解
摘要 Fabric 网络启动的过程中需要进行大量配置,新学时对各个配置的作用一无所知,这导致我曾在网络出问题时先对配置文件的内容进行排列组合后再祈祷它能在某个时刻顺利运行,因此掌握 fabric 各个 ...
随机推荐
- winform判断一个事件是否已经绑定了事件处理函数
public static class ComponentHelper<T> where T : Control { public static bool HaveEventHandler ...
- org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver '
问题摘要: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nest ...
- ORACLE 日期加减操作
无论是DATE还是timestamp都可以进行加减操作. 可以对当前日期加年.月.日.时.分.秒,操作不同的时间类型,有三种方法: 1 使用内置函数numtodsinterval增加小时,分钟和秒2 ...
- Iptables-redhat/centos
6用iptables 7默认用firewalld firewalld 与 iptables 过滤点,表 做nat是使用postrouting,prerouting表 Samba服务所使用的端口和协议: ...
- PHP字母数字验证码和中文验证码
1:字母数字组合的验证码 HTML代码: 验证码:<input type="text" name="code"> <img onclick=& ...
- Spring Boot 核心配置文件 bootstrap & application 详解。
用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件 bootstrap (.yml 或者 .properties) application (.yml 或者 .pr ...
- [原创]MOF提权下载者代码
0x001 网上的mof提权 调用的是js执行添加用户 而且有个缺陷 还不能一步到位...目标3389也连不上...也不知道上面安装了什么软件...毛然添加用户也不好比如有个类似狗之类的拦截添加用户 ...
- mysql 开发进阶篇系列 31 工具篇(mysql连接工具与MyISAM表压缩工具)
一.mysql 连接工具 在mysq提供的工具中,DBA使用最频繁的莫过于mysql.这里的mysql是指连接数据库的客户端工具. 1.1 连接选项 -u, -- user=name 指定用户名 -p ...
- mac下nginx搭建
首先使用brew安装nginx brew install nginx 安装完毕后,如果我们要使用nginx监听本地的80端口,需要改掉mac自带的apache占用的80端口 sudo vim /etc ...
- 「Java基本功」一文读懂Java内部类的用法和原理
内部类初探 一.什么是内部类? 内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的一个成员,并且依附于外部类而存在的.内部类可为静态,可用protected和private修饰(而外部类只能 ...