温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux中环境变量配置的示例分析

发布时间:2021-07-16 11:55:04 来源:亿速云 阅读:115 作者:小新 栏目:服务器

这篇文章将为大家详细讲解有关Linux中环境变量配置的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

文档

我一向讨厌那种说结论不说出处的行为,这会给人一种“我凭什么相信你”的感觉。而且事实上没有出处就随便议论得出的结论也基本上是人云亦云的。事实上,与其去问别人,不如去问文档。 找了一会,发现关于环境变量配置的相关文档其实是在bash命令的man文档里,毕竟我们常用的就是这个shell。

在$man bash里,我发现了下面的一段文字:

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登陆的时候自动加载的那些文件。不过他所定义的登陆却分为两种:

  • login shell登陆。

  • interactive shell登陆。

login shell 登陆

所谓的login shell登陆,实际上就是指需要输入密码的登陆。具体的说,包括开机登陆、ssh登陆,或者是输入bash --login这种“假装自己输入密码登陆”的方式。 在这种登陆方式下,系统会先读取/etc/profile文件,然后,系统会依次搜索~/.bash_profile、~/.bash_login、~/.profile 这三个文件,并运行只其中第一个存在的文件。 尤其要注意到后三个文件的“逻辑或”的关系。很多情况下我们会发现,明明已经修改了~/.profile文件为什么重新登陆后配置不生效呢?这是因为我们的系统可能存在了前面两个文件中的一个,导致不会继续读取剩下的文件。

下面的三张图很好的说明了这个问题:

Linux中环境变量配置的示例分析 

Linux中环境变量配置的示例分析

Linux中环境变量配置的示例分析

interactive shell 登陆

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

注意:

需要注意的是,这两种登陆方式读取的是不同的配置文件,而且互相之间没有交集,因此当我们需要配置环境变量时,我们要根据自己的登陆方式将需要的变量配置到不同的文件里。 例如下面这个经典的问题。

典型问题

环境配置文件配置异常的例子是,当我用ssh登录服务器的时候,发现提示符是这样的:

bash-4.3$

没错,就像上面第三张图片里的那个bash一样,提示符非常奇怪,而且当输入ls时文件和文件夹的颜色也没有区分。 这个问题显然是由于$PS1这个环境变量没有配置,导致他用了默认值,虽然查看.bashrc文件时发现有$PS1这个变量的定义。,但是由于ssh属于login shell,因此他在登陆时读入的配置文件是/etc/profile一类的文件,并没有读入.bashrc。 导致这个问题的原因通常是我们误删除了/etc/profile里默认的配置文件,因此解决的办法也很简单。。。把.bashrc里的部分文件复制到/etc/profile里就行了。

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

关于“Linux中环境变量配置的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI