linux环境变量_Linux环境变量

linux环境变量

介绍 (Introduction)

An Environment is an area that contains information about how various programs and applications behave. Linux Environment Variables are used by the applications to get information about the environment.

环境是一个区域,其中包含有关各种程序和应用程序行为的信息。 应用程序使用Linux环境变量来获取有关环境的信息。

The Environment is configured every time a new shell session is created. Changing the Linux Environment Variables helps the user to control how they interact with programs via the shell, what default values are used for the appearance of the shell window, the type of shell used, default home directory, etc.

每次创建新的Shell会话时都会配置环境。 更改Linux环境变量可帮助用户控制它们如何通过Shell与程序交互,用于Shell窗口外观的默认值,使用的Shell类型,默认主目录等。

Linux环境变量文件 (Linux Environment Variables File)

Information required for setting up the Environment is obtained via various files through a process known as Initialization. The operating system performs this process by reading through /etc/profile and .profile files.

设置环境所需的信息通过称为初始化的过程通过各种文件获得。 操作系统通过读取/etc/profile.profile文件来执行此过程。

The /etc/profile file is under Linux administrative control and contains essential information which cannot be edited by the user. The .profile file is fully under user control and can be used to modify the environment.

/ etc / profile文件受Linux管理控制,并且包含用户无法编辑的基本信息。 .profile文件完全在用户控制之下,可用于修改环境。

If you are using the bash shell, the user environment variables can be created in .bash_profile and .bashrc file.

如果使用bash shell,则可以在.bash_profile和.bashrc文件中创建用户环境变量。

If these files don’t exist, the operating system proceeds with creating the Environment and does not display any error.

如果这些文件不存在,则操作系统将继续创建环境,并且不会显示任何错误。

The terminal prompts the user for input (indicated by the $ sign) after the Environment has been set up.

设置环境后,终端会提示用户输入(用$符号表示)。

Linux环境变量 (Linux Environment Variables)

These are name/value pairs.

这些是名称/值对。

Environment variables can be handled using the following 5 main commands.

可以使用以下5个主要命令来处理环境变量。

  1. printenv

    打印环境
  2. env

    环保
  3. set

  4. export

    出口
  5. unset

    未设定

Let’s look into each of these commands one by one.

让我们逐一研究这些命令。

  1. printenv can be used to print the value of an environment variable. If no key is specified, then all name-value pairs are printed as output. These only include variables with a global scope.
    printenv command example

    printenv command example

    Linux Environment variables are defined in capital letters by convention.

    printenv可用于打印环境变量的值。 如果未指定键,则所有名称/值对都将打印为输出。 这些仅包括具有全局范围的变量。

    printenv命令示例

    Linux环境变量按照约定用大写字母定义。

  2. env also prints the name-value pairs like printenv. This command also only prints variables with global scope. However, it cannot be used to print specific values of environment variables.

    env还可以像printenv一样打印名称/值对。 此命令还仅打印具有全局作用域的变量。 但是,它不能用于打印环境变量的特定值。
  3. set is used to print shell variables, environment variables, local variables and shell functions. These include variables with both local and global scope.
    set command example

    set command example

    set用于打印外壳变量,环境变量,局部变量和外壳函数。 这些变量包括局部和全局范围的变量。

    设置命令示例

  4. export is used to create new environment variables that persist even after a shell session has been closed.

    export用于创建新的环境变量,即使在关闭Shell会话之后,这些变量仍将保留。
  5. unset is used to unbind or remove an environment variable.

    unset用于取消绑定或删除环境变量。

一些重要的预定义环境变量 ( Some Important Pre-Defined Environment Variables)

  • LANG: Contains current system language.

    语言:包含当前的系统语言。
  • USERNAME: Contains username of the current user.

    USERNAME:包含当前用户的用户名。
  • PWD: Contains path of the current directory.

    PWD:包含当前目录的路径。
  • PATH: Contains colon separated list of directories in which system looks for executables.

    PATH:包含用冒号分隔的目录列表,系统在其中查找可执行文件。
  • HOME: Contains path of the home directory.

    HOME:包含主目录的路径。
  • SHELL: Contains path to the user’s preferred shell.

    外壳:包含用户首选外壳的路径。
  • EDITOR: Contains command to run lightweight editor. Eg: /usr/bin/nano

    编辑器:包含运行轻量级编辑器的命令。 例如: /usr/bin/nano
  • VISUAL: Contains command to run editor used for heavy editing tasks. Eg: Vi (Vim)

    VISUAL:包含用于运行用于繁重编辑任务的编辑器的命令。 例如: Vi (Vim)
  • BROWSER: Contains command to run default web-browser.

    浏览器:包含用于运行默认Web浏览器的命令。

环境变量运算 (Environment Variable Operations)

Let’s look at some common environment variable operations in Linux.

让我们看一下Linux中一些常见的环境变量操作。

1.创建新的环境变量 (1. Creating New Environment Variables)

New variables can simply be created by typing a new variable name and assigning it a value in "", separated by = sign.

可以通过键入新的变量名称并在""为其分配一个值(以=号分隔)来简单地创建新变量。

No spaces must be there between = and variable name/value. Otherwise, the terminal will return an error.

=和变量名称/值之间不能有空格。 否则,终端将返回错误。

Once the variable is created, its values is displayed using the echo command.

创建变量后,将使用echo命令显示其值。

new env var example

new env variable example

新的环境变量示例

A $ sign must precede the variable name while displaying the value using echo, as shown in the example above. However, it is not required while creating a new variable.

如上例所示,在使用echo显示值时,必须在变量名称前加$号。 但是,创建新变量时不需要。

Now that we have created a new Environment Variable, let’s try finding it in the list of variables displayed by the printenv command, and printing out its value.

现在,我们已经创建了一个新的环境变量,让我们尝试在由printenv命令显示的变量列表中找到它,并打印出它的值。

This can be done using the grep command.

这可以使用grep命令来完成。

grep env var value

grep env variable example

grep env变量示例

No output is displayed. This is because even though we have defined a new Environment Variable, we are yet to “export” it into the file containing names of all variables.

没有输出显示。 这是因为即使我们定义了一个新的环境变量,我们仍未将其“导出”到包含所有变量名称的文件中。

Exporting allows the variable to be accessible by other programs and subshells.

导出允许变量被其他程序和子外壳访问。

This is achieved by using the export command we discussed in the last section.

这是通过使用上一节中讨论的export命令来实现的。

$ export [VARIABLE_NAME]
export command example

export command example

导出命令示例

Value of the variable is now displayed. It can even be accessed in a new subshell.

现在显示变量的值。 甚至可以在新的子Shell中对其进行访问。

env var subshell export example

subshell export example

subshel​​l导出示例

We can exit the subshell by typing the exit command. This brings us back to the original shell.

我们可以通过键入exit命令退出子外壳。 这使我们回到了原始的外壳。

2.更改现有变量的值 (2. Changing Values of Existing Variables)

Existing variables can be edited by simply assigning a new value to them. We can see the changes made by printing the variable value using echo.

可以通过简单地给它们分配一个新值来编辑它们。 我们可以看到通过使用echo打印变量值所做的更改。

changing env var value

changing env variable value

更改环境变量值

3.删除环境变量 (3. Removing Environment Variables)

The unset command is used to remove existing Environment variables.

unset命令用于删除现有的环境变量。

$ unset [VARIABLE_NAME]
unset command example

unset command example

未设置命令示例

持久性与非持久性环境变量 (Persistent vs Non-Persistent Environment Variables)

All Environment Variables that we have dealt with till now, have been non-persistent variables. The changes made to these only last until the current shell is active and do not affect new shell windows. Once the current session is closed, all changes made to variables – created or edited, are lost.

到目前为止,我们处理的所有环境变量都是非持久变量。 对这些所做的更改只会持续到当前外壳程序处于活动状态,并且不会影响新的外壳程序窗口。 当前会话关闭后,对变量进行的所有更改(创建或编辑)都将丢失。

In order to preserve changes through new sessions and windows, we can create Persistent Environment Variables. These are created by defining variables within the environment variable file in the home directory – .profile, .bash_profile, and .bashrc.

为了通过新的会话和窗口保留更改,我们可以创建持久性环境变量。 通过在主目录中的环境变量文件-.profile,.bash_profile和.bashrc中定义变量来创建这些变量。

We can open the .bashrc file using nano or vim.

我们可以使用nanovim打开.bashrc文件。

persistent env var example

accessing .bashrc file

访问.bashrc文件

Define a new variable anywhere inside the .bashrc file using the export command. Save changes and exit the file.

使用export命令在.bashrc文件内的任何位置定义一个新变量。 保存更改并退出文件。

new env var in bashrc

defining env variable in .bashrc

在.bashrc中定义env变量

Close the current terminal session and open a new one.

关闭当前的终端会话,然后打开一个新的会话。

Now, print the Environment Variable just created using the echo command.

现在,打印刚刚使用echo命令创建的环境变量。

Persistent Env Var Display

persistent env variable output example

持久环境变量输出示例

We can see that the variable persists even after creating a new terminal session and closing the previous one.

我们可以看到,即使在创建新的终端会话并关闭前一个会话之后,该变量仍然存在。

结论 (Conclusion)

Environment variables are very important and useful. They can modify the working of programs and make it easier for the user to interact with them. Existing variables can be easily modified according to the user’s need. Moreover, new variables can also be created easily. Environment Variables can change how the user interacts with the shell and even make it quicker to access certain programs.

环境变量非常重要且有用。 他们可以修改程序的工作并使用户更容易与它们进行交互。 现有变量可以根据用户需要轻松修改。 此外,还可以轻松创建新变量。 环境变量可以更改用户与外壳的交互方式,甚至可以更快地访问某些程序。

翻译自: https://www.journaldev.com/30868/linux-environment-variables

linux环境变量

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值