Linux environment variables (环境变量)
Environment variables are often used to store a list of paths of where to search for executables, libraries, and so on.
环境变量通常存放一堆路径,这些路径用来搜索可执行文件、动态链接库,等等。
Examples are $PATH, $LD_LIBRARY_PATH, 可以通过 echo 命令来查看:
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/ruby/bin:/root/bin:/opt/python36/bin
[root@localhost ~]#
在终端命令行中输入命令或可执行文件的文件名,系统会去$PATH定义的环境变量里的逐个去查找,注意有先后顺序的,如果找不到,会提示 command not found。
如果是自己编译安装的软件,刚开始要执行的话,前面需要指定路径(绝对路径和相当路径都可以),如果想像系统命令一样,不管在哪里只需要输入命令都可以执行的话,需要把 软件的路径 添加到环境变量中,如何添加呢?
1、使用export命令: 只在当前的session中有效
export PATH=$PATH:/opt/nginx/sbin
2、在系统已有的路径中比如:/usr/local/bin 中添加 你的可执行文件的硬链接(或软链接)
ln /opt/redis-4.0.10/src/redis-server /usr/local/bin/redis-server
ln /opt/redis-4.0.10/src/redis-cli /usr/local/bin/redis-cli
3、修改 ~/.bashrc 文件 :只对当前用户有效
vim ~/.bashrc
在里面加一行: export PATH=$PATH:/opt/ruby/bin
让环境变量立即生效需要执行如下命令:source ~/.bashrc
4、修改/etc/profile文件 :对系统里所有用户都有效
vim /etc/profile
在里面加一行: export PATH=/opt/python36/bin:$PATH
最后可以直接输入命令执行或者echo $PATH 来检验是否修改配置成功。
Advanced Bash-Scripting Guide 高级脚本编程指导 (有兴趣可以好好读一下)
在Linux Shell Cookbook, Second Edition中还提到一种自定义命令在系统环境变量中添加变量:
However, we can make this easier by adding this function in .bashrc-:
prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }
This can be used in the following way:
prepend PATH /opt/myapp/bin
prepend LD_LIBRARY_PATH /opt/myapp/lib
How it works...
We define a function called prepend(), which first checks if the directory specified by the
second parameter to the function exists. If it does, the eval expression sets the variable with
the name in the first parameter equal to the second parameter string followed by : (the path
separator) and then the original value for the variable.
However, there is one caveat, if the variable is empty when we try to prepend, there will be a
trailing : at the end. To fix this, we can modify the function to look like this:
prepend() { [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1 ;}
In this form of the function, we introduce a shell parameter
expansion of the form:
${parameter:+expression}
This expands to expression if parameter is set and is not null.
With this change, we take care to try to append : and the old value if, and only if, the old
value existed when trying to prepend.
Linux environment variables (环境变量)的更多相关文章
- linux 、 CentOs ---> 环境变量设置
Linux下环境变量设置 1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错 ...
- linux配置java环境变量(详细)
linux配置java环境变量(详细) 本文完全引用自: http://www.cnblogs.com/samcn/archive/2011/03/16/1986248.html 一. 解压安装jdk ...
- linux配置java环境变量
linux配置java环境变量(详细) 一. 解压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586. ...
- Linux里设置环境变量的方法(export PATH)
1.动态库路径的设置 Linux下调用动态库和windows不一样.linux 可执行程序是靠配置文件去读取路径的,因此有些时候需要设置路径 具体操作如下 export LD_LIBRARY_PATH ...
- [转]在Linux里设置环境变量的方法
在Linux里设置环境变量的方法(export PATH) 一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/a ...
- hadoop搭建杂记:Linux下JDK环境变量的设置(三种配置环境变量的方法)
Linux下JDK环境变量的设置(三种配置环境变量的方法) Linux下JDK环境变量的设置(三种配置环境变量的方法) ①修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种 ...
- linux配置java环境变量(转)
linux配置java环境变量(详细) 一. 解压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586. ...
- linux配置java环境变量jdk jre(详细)【转】
linux配置java环境变量(详细) 本文完全引用自: http://www.cnblogs.com/samcn/archive/2011/03/16/1986248.html 一. 解压安装jdk ...
- linux下配置环境变量方式
linux下配置环境变量有多种方式,下面简述之 方式1.编辑 /etc/profile 文件,增加如下内容 JAVA_HOME=/usr/local/jdk1. export JAVA_HOME PA ...
- Linux中PATH环境变量的作用和使用方法
关于PATH的作用:PATH说简单点就是一个字符串变量,当输入命令的时候LINUX会去查找PATH里面记录的路径.比如在根目录/下可以输入命令ls,在/usr目录下也可以输入ls,但其实ls这个命令根 ...
随机推荐
- css里关于浏览器的前缀
今天遇到一个比较坑爹的 -moz-box-sizing: border-box; box-sizing' border-box; 一下子有点懵逼,第一个什么鬼??一查,原来是火狐浏览器的前缀.应该 ...
- js的NaN变量
js中,我们经常在parseInt函数的时候遇到NaN变量,这个变量到底是什么呢? w3c上这样解释: NaN 属性是代表非数字值的特殊值.该属性用于指示某个值不是数字.可以把 Number 对象设置 ...
- 如何使用webstorm去操作git
0. 前言 在上一篇文章中,讲述了使用webstorm去调试node程序,最近研究了一下如何使用webstorm去操作git. 对于git的使用,大家的使用方式均有不同,最王道的方式非命令行莫属,基于 ...
- IT兄弟连 JavaWeb教程 URI、URL
URI介绍 URI(Uniform Resource Identifier),是统一资源标识符的缩写,是一个用于标识某一个Web资源名称的字符串,该标识允许用户对任何资源通过特定的协议进行交互.Web ...
- 黑客攻防技术宝典web实战篇:定制攻击自动化习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 指出使用自动技巧在应用程序中枚举标识符时用到的 3 个标识符“触点”. (a) HTTP ...
- Selenium | 基础入门
在maven项目搭建环境: <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifact ...
- ie下,php HTTP_REFERER获取失败的整理
HTTP_REFERER有效的情况1.以iframe 形式调用地址2.以window.open调用,打开新页面window.open(url);3.使用window.location.replace在 ...
- Python入门小练习 002 批量下载网页链接中的图片
我们常常需要下载网页上很多喜欢的图片,但是面对几十甚至上百张的图片,一个一个去另存为肯定是个很差的体验. 我们可以用urllib包获取html的源码,再以正则表达式把匹配的图片链接放入一个list中, ...
- CentOS下查看网络状态
查看网络状态:lsof -Pnl +M -i4 显示ipv4服务及监听端情况netstat -anp 所有监听端口及对应的进程netstat -tlnp 功能同上 网络基本命令 (1)network ...
- 微信小程序生成分享图片,保存到本地
1.页面 <canvas canvas-id="shareCanvas" style="width:600px;height:900px">< ...