【Operating System】环境变量(Environmental Variables)

Posted by 西维蜀黍 on 2017-04-09, Last Modified on 2021-09-21

我们在安装一些编程环境时,经常需要配置环境变量。但是你是否完全了解其意义呢?

1 粗谈环境变量

环境变量相当于给系统或用户应用程序设置的一些参数,具体起什么作用这当然和具体的环境变量类型有关。比如path,是告诉系统,当要求系统运行一个程序,而没有告诉它程序所在的完整路径时,系统除了在当前目录下面寻找此程序外,还应到哪些目录下去寻找。

环境变量有三种类型:

  • 局部环境变量(Local Environment Variable):仅作用于当前会话(session),比如ssh登录到的remote session、或者本地terminal session
  • 用户环境变量(User Environment Variable):当一个特定用户登录后(本地登录或远程登录),这些环境变量就会被加载,比在如.bashrc.bash_profile.bash_login.profile中设置的环境变量都是用户环境变量
  • 系统环境变量(System wide Environment Variables):对所有用户均有效,比如/etc/environment/etc/profile/etc/profile.d//etc/bash.bashrc 中设置的环境变量都是系统环境变量

对于Windows平台,你可以通过代码查看或修改环境变量的设置(Here),

2 设置和查看环境变量

局部环境变量(Local Environment Variable)

设置局部环境变量

Local Environment Variables can be created using following commands:

$ <var>=<value> 
OR
$ export <var>=<value>

# e.g.,
$ aaa=bbb
# Seems in some Linux, the following doesn't work (we can print it to double check)
$ aaa='ccc'
$ aaa="ddd"

# 转义双引号
$ bbb="I am \"Wei\""
$ echo $bbb
I am "Wei"

# 拼接
$ your_name="Wei"
$ str="Hello, I know you are "$your_name" !"
$ echo $str
Hello, I know you are "Wei"!
$ str1="hello, ${your_name} !"
$ echo $str1

# Print a specific Environment Variable
$ echo $str

单引号字符串的限制:

  • 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
  • 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。

Another way to clear local environment variable is by using unset command. To unset any local environment variable temporarily,

$ unset <var-name>

查看局部环境变量

By default, "env" command lists all the current environment variables. But, if used with '-i' switch, it temporarily clears out all the environment variables and lets user execute a command in current session in absence of all the environment variables.

$ env –i [Var=Value]… command args…

Here, var=value corresponds to any local environment variable that you want to use with this command only.

$ env –i bash

Will give bash shell which temporarily would not have any of the environment variable. But, as you exit from the shell, all the variables would be restored.

当然,你也可以使用

$ export

来查看当前所有的局部环境变量。

用户环境变量(User Environment Variable)和系统环境变量(System wide Environment Variables)

用户环境变量(User Environment Variable)

.bashrc

This file is user specific file that gets loaded each time user creates a new local session i.e. in simple words, opens a new terminal. All environment variables created in this file would take effect every time a new local session is started.

.bash_profile

This file is user specific remote login file. Environment variables listed in this file are invoked every time the user is logged in remotely i.e. using ssh session. If this file is not present, system looks for either .bash_login or .profile files.

系统环境变量(System wide Environment Variables)

/etc/environment

This file is system wide file for creating, editing or removing any environment variables. Environment variables created in this file are accessible all throughout the system, by each and every user, both locally and remotely.

/etc/bash.bashrc

System wide bashrc file. This file is loaded once for every user, each time that user opens a local terminal session. Environment variables created in this file are accessible for all users but only through local terminal session. When any user on that machine is accessed remotely via a remote login session, these variables would not be visible.

/etc/profile

System wide profile file. All the variables created in this file are accessible by every user on the system, but only if that user’s session is invoked remotely, i.e. via remote login. Any variable in this file will not be accessible for local login session i.e. when user opens a new terminal on his local system.

Note: Environment variables created using system-wide or user-wide configuration files can be removed by removing them from these files only. Just that after each change in these files, either log out and log in again or just type following command on the terminal for changes to take effect:

$ source <file-name>

3 常用的环境变量

Path

比如说你要执行java命令,操作系统会在当前目录和所有在Path中设置的路径位置寻找这个命令对应的java.exe文件。

如果在任意位置找到了java.exe,就运行命令,且不再在其他路径下寻找了。但是如果在当前目录和所有在Path中设置的路径位置下均没有找到,则会提示'java' is not recognized as an internal or external command, operable program or batch file.,如下图:

因此,我们需要将jdk目录下的bin目录添加到环境变量的Path中,bin目录包含了经常用到的可执行文件,比如javajavacjavadoc

设置好后,我们就可以在任何目录下使用java或者javac了。

ClassPath

ClassPath 是一个针对Java的,用来告诉Java编译器在哪里存在编译过程中所需的文件(.class)或者包(.jar)路径的变量。

比如在引入一个类import javax.swing.JTable时, 是告诉编译器要引入javax.swing这个包下的JTable类,而 CLASSPATH 就是告诉编译器该到哪里去找到JTable这个类。

如果你想要编译器在当前目录下找,就在CLASSPATH中加上“.”。

如果你想要编译器去C:\Program Files\Java\jdk\下找javax.swing.JTable这个类,就在CLASSPATH中加入C:\Program Files\Java\jdk\

注意:

如果出现【java.lang.NoClassDefFoundError】错误,就有可能是ClassPath未设置或设置错误的问题。

因此,需要将 ClassPath 设置为.;%JAVA_HOME%/lib/;%JAVA_HOME%/jre/lib/,其中JAVA_HOME设置为对应 JDK的路径(我的为:C:\Program Files\Java\jdk1.8.0_60)。

3.参考