linux中设置环境变量_如何在Linux中设置环境变量

本文详细介绍了如何在Linux中设置和删除环境变量,包括创建外壳变量、使用export命令使其成为环境变量,以及使用unset命令删除环境变量。环境变量在控制Shell会话功能方面起着关键作用,理解其设置方法对任何Linux用户都很重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux中设置环境变量

Wondering how to set environment variables in Linux? This is exactly what we’ll be doing in this tutorial. Operating systems make use of environment variables to define system properties for the runtime environment.

想知道如何在Linux中设置环境变量? 这正是我们在本教程中将要做的。 操作系统利用环境变量来定义运行时环境的系统属性。

Through this tutorial, we will help you understand how environment variables work. Further, we will see how to set environment variables in Linux or reset existing environment variables as per our needs.

通过本教程,我们将帮助您了解环境变量的工作方式。 此外,我们将了解如何根据需要在Linux中设置环境变量或重置现有环境变量。

了解环境变量 (Understanding Environment Variables)

When we start a session on our server through the shell, it has to create an environment for us. This is a mandatory step for us to interact with the server through the shell.

当我们通过外壳在服务器上启动会话时,它必须为我们创建一个环境。 这是我们通过外壳与服务器交互所必须执行的步骤。

Environment variables are special variables that are available to all commands and processes on a system. These variables have the following formats.

环境变量是特殊变量,可用于系统上的所有命令和进程。 这些变量具有以下格式。


ENVAR=value
ENVAR="Another value"
ENVAR=value_1:value_2
  • All variable names are case-sensitive. We provide all environment variables with an uppercase name by convention.

    所有变量名称均区分大小写。 根据约定,我们为所有环境变量提供一个大写名称。
  • When assigning a value to our variable, we make sure there is no space around the assignment operator (=).

    在为变量赋值时,请确保赋值运算符(=)周围没有空格。
  • If we need to assign multiple values to a single environment variable, we make use of a colon (:) to separate the different values.

    如果需要为单个环境变量分配多个值,则可以使用冒号(:)分隔不同的值。

列出Linux中的现有环境变量 (List existing Environment Variables in Linux)

If we want to see the environment variables for our current session, we use the following command.

如果要查看当前会话的环境变量,请使用以下命令。


printenv
Print Environment Variables In Linux Systems
Print Environment Variables In Linux Systems
Linux系统中的打印环境变量

As seen in the screenshot above, this command will print all the variables for the current shell session. If we want a list of shell variables with environment variables, we use this command.

如上面的屏幕截图所示,此命令将打印当前shell会话的所有变量。 如果我们想要带有环境变量的shell变量列表,请使用此命令。


set

This will return a similar output displaying the shell variables for the current shell session.

这将返回一个类似的输出,显示当前shell会话的shell变量。

Below are some examples of environment variables with their importance.

以下是一些环境变量及其重要性的示例。

Type of VariableSignificance
SHELLThe shell which interprets all the commands entered by the user. Usually, this will be set to bash by default. But users can change it as per their to suit their needs.
USERThe username of the user currently logged on to the system.
LANGThe language and localization settings for the system currently in place, which includes character encoding.
TERMThe type of terminal which needs to be emulated when the shell is running. While this isn’t something you would need to usually worry about, there can be instances when your operating needs might require emulating a specific hardware terminal.
HOMEThe home directory for the user currently logged on to the system.
PATHThe list of directories in which our system will check when a command is entered by the user. This search of directories is done to find the executable files associated with the command.
PWDThe current directory which the user is working with.
变量类型 意义
贝壳 解释用户输入的所有命令的外壳。 通常,默认情况下将其设置为bash。 但是用户可以根据自己的需要进行更改。
用户 当前登录到系统的用户的用户名。
当前就位系统的语言和本地化设置,其中包括字符编码。
术语 Shell运行时需要模拟的终端类型。 尽管您通常不必担心这一点,但是在某些情况下,您的操作需求可能需要模拟特定的硬件终端。
当前登录到系统的用户的主目录。
路径 当用户输入命令时,系统将在其中检查目录的列表。 完成目录搜索以查找与命令关联的可执行文件。
残障人士 用户正在使用的当前目录。

如何在Linux中设置环境变量? (How to set environment variables in Linux?)

Now that we know what environment variables are, it is time to learn how to set environment variables in Linux. 

既然我们知道什么是环境变量,现在该学习如何在Linux中设置环境变量了。

1.创建一个外壳变量并分配一个值 (1. Create a shell variable and assign a value)

When we need to create a new environment variable, we first need to create a variable and initialize it. For this tutorial, we will use the following.

当我们需要创建一个新的环境变量时,我们首先需要创建一个变量并将其初始化。 对于本教程,我们将使用以下内容。


TESTVAR="This is a test variable"
echo $TESTVAR

We’ve used the echo command to display the variable and confirm its existence.

我们已经使用echo命令显示变量并确认其存在。

Set A Variable In Shell
Set A Variable In Shell
在Shell中设置变量

Now, if you run the printenv command with the variable name as shown below, you will not get any output since the variable is currently a shell variable

现在,如果使用如下所示的变量名称运行printenv命令,则不会获得任何输出,因为该变量当前是shell变量


printenv TESTVAR

The above command will give us no output because TESTVAR isn’t an environment variable yet.

上面的命令不会给我们任何输出,因为TESTVAR还不是环境变量。

2.导出以将其转换为环境变量 (2. Export to Convert it to an Environment Variable)

Let’s make it into an environment variable using the export command and then we will use the printenv command once again.

让我们使用export命令将其放入环境变量中,然后再次使用printenv命令。


export TESTVAR
printenv TESTVAR
Set An Environment Variable In Linux
Set An Environment Variable In Linux
在Linux中设置环境变量

As you can see above, we get the value stored by TESTVAR. This is because by using the export command, we made TESTVAR an environment variable. 

如您在上面看到的,我们得到了TESTVAR存储的值。 这是因为通过使用export命令,我们将TESTVAR设置为环境变量。

3.替代方案–结合两个步骤 (3. Alternative – Combining the two steps)

There is a more commonly used method when you want to set environment variables in Linux. We can use the below line to declare and export the variable at the same time.

要在Linux中设置环境变量时,有一种更常用的方法。 我们可以使用下面的行来同时声明和导出变量。


export NEWTESTVAR="This is a new test variable"
printenv NEWTESTVAR
One Line Set Environment Variables In Linux 1
One Line Set Environment Variables In Linux 1
Linux 1中的一个行集环境变量

Now when we use the printenv command for our new variable, we will see that it displays the environment variable right away.

现在,当我们对新变量使用printenv命令时,我们将看到它立即显示环境变量。

如何在Linux中删除环境变量 (How to delete environment variables in Linux)

Now that we know how to set environment variables in Linux, we need to learn how to remove them. Once we have no use for our created environment variables, it is advised to delete them.

现在我们知道了如何在Linux中设置环境变量,我们需要学习如何删除它们。 一旦我们不用创建的环境变量,建议删除它们。

This ensures that the environment variable isn’t called unintentionally.

这样可以确保不会意外调用环境变量。

1.使用导出命令 (1. Using the export Command)


export -n TESTVAR
printenv TESTVAR

Now, if we try using the printenv command to get the value of TESTVAR, we will get no output.

现在,如果尝试使用printenv命令获取TESTVAR的值,则不会获得任何输出。

This confirms that TESTVAR is no longer an environment variable.

这确认TESTVAR不再是环境变量。

2.使用unset命令 (2. Using the unset Command)

Alternatively, we can use the unset command. Using it will remove the variable completely so that it is neither an environment variable nor a shell variable.

或者,我们可以使用unset命令 。 使用它会完全删除该变量,因此它既不是环境变量也不是shell变量。

This can be done as given below.

可以按照以下步骤完成。


unset TESTVAR
Unset Environment Variables
Unset Environment Variables
未设置环境变量

Notice that nothing is returned even when we use the echo command. Thus, we know that the variable is unset.

请注意,即使使用echo命令,也不会返回任何内容。 因此,我们知道该变量未设置。

结论 (Conclusion)

With environment variables having so much control over how our shell session functions, it is important for any shell user to understand how to set environment variables in Linux.

由于环境变量对我们的Shell会话的功能具有很大的控制权,因此对于任何Shell用户而言,了解如何在Linux中设置环境变量都非常重要。

Manipulating or creating new variables can greatly assist when you wish to customize your shell session and make your tasks easier.

当您希望自定义Shell会话并使任务更轻松时,操作或创建新变量将极大地帮助您。

翻译自: https://www.journaldev.com/38244/set-environment-variables-in-linux

linux中设置环境变量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值