Environment variable to explain and set in the environment variables under Linux

First, the environment variable to explain

What environment variables?

References Baidu Baike inside explanation: the environment variable is the target operating system has a specific name that contains one or more applications will use the information. Such as Windows system environment variables in the path, when the system is required to run a program and it does not tell the full path where, in addition to the system to find this program in the current directory, you can also specify the path to the path to find. Users by setting environment variables to better run the process.

Why do we need an environment variable?

the windows system, if we installed a piece of software, after installation, the installation directory will generate a .exe file of the software, double-click the file, we will be able to start the software. But would not we want to run the software every time they have to find the path to the .exe file is located, and then double-click it, is clearly impossible, because we do not remember all the paths installed software. This time it needs environment variables. We look down through example.

For example QQ

After the first installed QQ, we do not have a desktop shortcut to open it, open Windows with the dos window, enter the dos interface, direct input Enter QQ found:

img

You want to let the system run a program, you first have to tell it where this program. So we want to tell the location of the executable file QQ system, that is the path to the file system.

img

After this path to the system:

img

Then you will open the QQ; then after you turn off the dos re-enter, enter the QQ find, but also the question above, which means you open the QQ from dos, it must file each time you enter the path QQ.exe, each had to file system QQ.exe absolute path;

Therefore, it is troublesome, how to solve it? A method once and for all is to QQ.exe file path Path environment variable on the inside, we wanted to open the QQ would not enter the path of each. As shown below:

img

Next, open the dos enter the QQ Enter:

img


Second, the environment variable settings under Linux and interpretation

同理,在Linux系统上的环境变量与Windows上的作用类似。在Linux系统,如果你下载并安装应用程序,很有可能在键入它的名称的时候出现 “command not found ” 的提示内容。 如果每次都到安装目录文件夹内,找到可执行文件来进行操作就太繁琐了, 这就需要设置path环境变量了。
Shell定义的环境变量
Shell在开始执行的时候就已经定义了一些与系统工作环境有关的变量,用户还可以重新定义这些变量。常用的shell环境变量有以下几种。

  • HOME:用于保存用户主目录的完全路径名。
  • PATH:用于保存用冒号分隔的目录路径名,shell将按PATH变量中给出的顺序搜索这些目录,找到的第一个与命令名称一致的可执行文件将被执行。
  • SHELL:当前用户使用的Shell
  • UID:当前用户的UID
  • LOGNAME:当前用户的登录名
  • HOSTNAME:主机名称

查看环境变量
Linux中set 、env 和 export 都可以查看环境变量。

  • set<命令显示当前shell的变量,包括当前用户的变量,set主要用来设置sh的参数与选项
  • env命令显示当前用户的变量,env用来在构建的环境中运行命令
  • export命令显示当前导出成用户变量的shell变量

这里我们只用 export 命令举例来查看PATH值:

xxx@xxx:~$ export
declare -x CLUTTER_IM_MODULE="xim"
declare -x COMPIZ_CONFIG_PROFILE="ubuntu"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-RvyK9kjBBX"
declare -x DEFAULTS_PATH="/usr/share/gconf/ubuntu.default.path"
declare -x DESKTOP_SESSION="ubuntu"
declare -x DISPLAY=":0"
declare -x GDMSESSION="ubuntu"
declare -x GDM_LANG="en_US"
declare -x GNOME_DESKTOP_SESSION_ID="this-is-deprecated"
declare -x GNOME_KEYRING_CONTROL=""
declare -x GNOME_KEYRING_PID=""

export no arguments when shows which variables are exported become a user variable, because a shell variable can be turned into its own through a user variable export "Export."

Linux environment variable settings

If you want to join a path to $ PATH, there may be several ways. For example, I would like to / tmp / test paths added to the $ PATH variable:

1, the console settings (only valid for the current shell)

Execute export PATH=$PATH:/tmp/testthe command:

2, modify the current .bashrc file in the home directory (only valid for the current user)

To execute vim ~/.bashrcthe command, add the following to the end of most, then source ~/.bashrc, the configuration file to take effect.

export PATH=$PATH:/tmp/test

3, modify / etc / bashrc file (valid for all users)

To execute sudo vim /etc/profilethe command, add the following to the end of most, then source /etc/profile, the configuration file to take effect.

export PATH=$PATH:/tmp/test

Note: source typically used to re-execute the command just modify the initialization file to make it take effect immediately, without having to log out and back.


reference:

Why go with an environment variable? path used to do?

In the Linux environment variable settings

Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11022532.html