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这个命令根 ...
随机推荐
- Android webkit 事件传递流程通道分析
前言:基于android webview 上定制自己使用的可移植浏览器apk,遇到好多按键处理的问题.所以索性研究了一下keyevent 事件的传递流程. frameworks 层 keyevent ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- 关于TImer使用的注意
晚点再写 停止Timer let timer = .... timer.invalidate()
- 【水水水】678A - Johny Likes Numbers
#include<stdio.h> #include<iostream> #include<cstdio> #include<queue> #inclu ...
- 第十五篇 .NET高级技术之正则表达式
正则表达式 正则表达式是对字符串进行匹配的语法,像name like '%李%'一样,定义了一些特殊的“元字符”,用来判断一个字符串是否满足某个规则.正则表达式非常深,编译器都是基于正则表达式,掌握基 ...
- AFNetworking https自签名证书 -1012 解决方案
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy]; //是否信任服务器无效或过期的SSL证书.默认为“不”. se ...
- Qt容器类之三:通用算法
在<QtAlgorithm>头文件中,Qt提供了一些全局的模板函数,这些函数是可以使用在容器上的十分常用的算法.我们可以在任何提供了STL风格迭代器的容器类上用这些算法,包括QList.Q ...
- 线段树(单点更新) HDU 1754 I Hate It
题目传送门 /* 线段树基本功能:区间最大值,修改某个值 */ #include <cstdio> #include <cstring> #include <algori ...
- 522 Longest Uncommon Subsequence II 最长特殊序列 II
详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...
- Suricata的配置
见官网 https://suricata.readthedocs.io/en/latest/configuration/index.html# Docs » 8. Configuration Edit ...