Linux中环境变量配置的步骤解读

更新时间:2017年07月19日11:16:00作者:Myths

Linux中环境变量包括系统级和用户级,系统级的环境变量是每位登陆到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的环境变量。所以下边这篇文章主要给你们介绍了关于Linux中环境变量配置的相关资料,须要的同学可以参考下。

简介

我们你们在平常使用Linux的时侯,常常须要配置一些环境变量,这时侯通常都是网上随意搜搜就有人介绍经验的。不过问题在于她们的方式各不相同,有人说配置在/etc/profile里,有人说配置在/etc/environment,有人说配置在~/.bash_profile里,有人说配置在~/.bashrc里,有人说配置在~/.bash_login里,还有人说配置在~/.profile里。。。这真是公说公有理。。。这么问题来了,Linux究竟是如何读取配置文件的呢,根据又是哪些呢?下边这篇文章就来给你们详尽的介绍下,一上去瞧瞧吧。

文档

我一向厌恶那个说推论不说出处的行为,这会给人一种“我凭哪些相信你”的觉得。并且事实上没有出处就随意议论得出的推论也基本上是人云亦云的。事实上,与其去问他人linux软件工程师,不如去问文档。找了一会,发觉关于环境变量配置的相关文档虽然是在bash命令的man文档里,虽然我们常用的就是这个shell

在$manbash里,我发觉了下边的一段文字:

INVOCATION
  A login shell is one whose first character of argument zero is a -, or
  one started with the --login option.
  An interactive shell is one started without non-option arguments and
  without the -c option whose standard input and error are both connected
  to terminals (as determined by isatty(3)), or one started with the -i
  option. PS1 is set and $- includes i if bash is interactive, allowing
  a shell script or a startup file to test this state.
  The following paragraphs describe how bash executes its startup files.
  If any of the files exist but cannot be read, bash reports an error.
  Tildes are expanded in filenames as described below under Tilde Expan‐
  sion in the EXPANSION section.
  When bash is invoked as an interactive login shell, or as a non-inter‐
  active shell with the --login option, it first reads and executes com‐
  mands from the file /etc/profile, if that file exists. After reading
  that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
  in that order, and reads and executes commands from the first one that
  exists and is readable. The --noprofile option may be used when the
  shell is started to inhibit this behavior.
  When a login shell exits, bash reads and executes commands from the
  file ~/.bash_logout, if it exists.
  When an interactive shell that is not a login shell is started, bash
  reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
  these files exist. This may be inhibited by using the --norc option.
  The --rcfile file option will force bash to read and execute commands
  from file instead of /etc/bash.bashrc and ~/.bashrc.
  When bash is started non-interactively, to run a shell script, for
  example, it looks for the variable BASH_ENV in the environment, expands
  its value if it appears there, and uses the expanded value as the name
  of a file to read and execute. Bash behaves as if the following com‐
  mand were executed:
    if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
  but the value of the PATH variable is not used to search for the file‐
  name.

通过这段文字,我们发觉虽然所谓的环境变量配置文件,就是在shell登录的时侯手动加载的这些文件。不过他所定义的登录却分为两种:

loginshell登录

所谓的loginshell登录,实际上就是指须要输入密码的登入。具体的说,包括开机登录、ssh登录,或则是输入bash--login这些“假装自己输入密码登入”的形式。在这些登录形式下,系统会先读取/etc/profile文件,之后,系统会依次搜索~/.bash_profile、~/.bash_login、~/.profile这三个文件,并运行只其中第一个存在的文件。尤其要注意到后三个文件的“逻辑或”的关系。好多情况下我们会发觉,明明早已更改了~/.profile文件为何重新登入后配置不生效呢?这是由于我们的系统可能存在了上面两个文件中的一个,致使不会继续读取剩下的文件。

下边的三张图挺好的说明了这个问题:

interactiveshell登录

linux下安装lamp环境_linux安装桌面环境变量_linux一键安装php环境

所谓的interactiveshell登录,虽然就是相对于loginshell登录而言的。我们平常在登入后右键打开终端、或者CTRL+ALT+T打开终端都是interactiveshell登录。在这些登录形式下,系统会依次读取/etc/bash.bashrc和~/.bashrc,并加以执行。一般情况下,~/.bashrc文件里会默认记录一些常量和一些别称linux下载,尤其是$PS1变量,这个变量决定着bash提示符的格式、样式以及颜色等。

注意:

须要注意的是,这两种登录方法读取的是不同的配置文件,并且相互之间没有交集,因而当我们须要配置环境变量时,我们要按照自己的登录方法将须要的变量配置到不同的文件里。诸如下边这个精典的问题。

典型问题

环境配置文件配置异常的事例是,当我用ssh登陆服务器的时侯,发觉提示符是这样的:

bash-4.3$

没错,如同前面第三张图片里的那种bash一样,提示符十分奇怪,但是当输入ls时文件和文件夹的颜色也没有分辨。这个问题其实是因为$PS1这个环境变量没有配置,致使他用了默认值,即使查看.bashrc文件时发觉有$PS1这个变量的定义。,然而因为ssh属于loginshell,因而他在登录时读入的配置文件是/etc/profile一类的文件,并没有读入.bashrc。造成这个问题的缘由一般是我们删掉不仅/etc/profile里默认的配置文件linux安装桌面环境变量,因而解决的办法也很简单。。。把.bashrc里的部份文件复制到/etc/profile里就行了。

这个问题给我们的启示是,当我们为服务器配置变量时,尽量配置到/etc/profile里或则~/.bash_profile里,由于用ssh登陆服务器是基本上用不到.bashrc文件的;当我们给自己的笔记本配置环境变量时,尽量配置到.bashrc里,由于这样我们只要打开终端都会读入这个文件,这样就可以不用注销能够应用配置了(只有注销重新登入才能应用/etc/profile一类的配置文件)。

总结

以上就是这篇文章的全部内容,希望本文的内容对你们的学习或则工作能带来一定的帮助linux安装桌面环境变量,假如有疑惑你们可以留言交流,感谢你们对脚本之家的支持。

本文原创地址:https://www.linuxprobe.com/gylzhjblpzdy.html编辑:刘遄,审核员:暂无