如何为Linux,Windows,BSD和MacOSX设置环境变量

Environment variables are very useful for setting some data in the operationg system environment. They are like key-value pairs where we can set a key a specific value which can be used by all applications in the current operating system. Environment variables are used to set library path, Java path, Java version, current username, current connection type etc.

环境变量对于在操作系统环境中设置某些数据非常有用。 它们就像键值对,在这里我们可以为键设置一个特定的值,该值可以被当前操作系统中的所有应用程序使用。 环境变量用于设置库路径,Java路径,Java版本,当前用户名,当前连接类型等。

列出Linux和MacOSX的所有环境变量 (List All Environment Variables For Linux and MacOSX)

We can list Linux operating system environment variables in different ways. The most popular way to list all environment variables is using env command.

我们可以用不同的方式列出Linux操作系统环境变量。 列出所有环境变量的最流行方法是使用env命令。

$ env
List All Environment Variables For Linux
List All Environment Variables For Linux
列出Linux的所有环境变量

We can see that environment variables like USERNAME, USER, SHELL, LANG etc. are provided

我们可以看到提供了USERNAME,USER,SHELL,LANG等环境变量

列出Linux和MacOSX的环境变量(List Environment Variable For Linux and MacOSX)

If we can to just print and list specific environment variable we need to use grep command like below with the env command. In this example, we will list environment variables where their key or value contains the USER term.

如果我们只能打印并列出特定的环境变量,则需要使用下面的grep命令和env命令。 在此示例中,我们将列出环境变量,其中其键或值包含USER术语。

$ env | grep USER
List Environment Variable For Linux
List Environment Variable For Linux
列出Linux的环境变量

We can see that USERNAME and USER variable keys are filtered and printed.

我们可以看到USERNAME和USER变量键已经过过滤和打印。

We can also use the echo command by providing the variable name which is USERNAME in this example.

我们还可以通过提供变量名称(在此示例中为USERNAME)来使用echo命令。

$ echo $USERNAME
List Environment Variable For Linux
List Environment Variable For Linux
列出Linux的环境变量

为Linux和MacOSX设置环境变量(Set Environment Variable For Linux and MacOSX)

Now, this is the most interesting part in setting an environment variable key and value. We can set environment variables permanently or temporarily.

现在,这是设置环境变量键和值中最有趣的部分。 我们可以永久或临时设置环境变量。

永久设置 (Set Permanently)

Setting permanently will store the variable during reboots and restarts. It will not be removed after a system restart. We will use bash shell .profile file to set environment variable permanently. Add the following lines to the user’s profile file like below.

永久设置将在重新启动和重新启动期间存储变量。 系统重启后将不会删除它。 我们将使用bash shell .profile文件永久设置环境变量。 将以下行添加到用户的配置文件中,如下所示。

WEBSITE="POFTUT.COM"

Run following command which will add key and value to the users .profile file.

运行以下命令,该命令会将键和值添加到用户.profile文件。

$ echo "export WEBSITE=POFTUT.COM" >> ~/.profile
Set Permanently
Set Permanently
永久设置

For zsh shell we will add to .zprofile file like below.

对于zsh shell,我们将添加到.zprofile文件,如下所示。

$ echo "export WEBSITE=POFTUT.COM" >> ~/.zprofile

For ksh or KornShell we will add to .profile file

对于ksh或KornShell,我们将添加到.profile文件

$ echo "export WEBSITE=POFTUT.COM" >> ~/.profile

For bourne shell we will add to .profile file

对于bourne shell,我们将添加到.profile文件

$ echo "export WEBSITE=POFTUT.COM" >> ~/.profile

For csh shell we will add to .login file

对于csh shell,我们将添加到.login文件

$ echo "export WEBSITE=POFTUT.COM" >> ~/.login

临时设置 (Set Temporarily)

If we want to set an environment variable temporarily for just current session for all users we can use export and set commands. set will add the environment variable and export will add variable to all other users environment.

如果要为所有用户仅在当前会话中临时设置环境变量,则可以使用exportset命令。 set将添加环境变量,而export将向所有其他用户环境添加变量。

$ export set WEBSITE="POFTUT.COM"
Set Temporarily
Set Temporarily
临时设置

列出Windows的所有环境变量(List All Environment Variables For Windows)

We can use the set command in order to print currently used environment variables from the MS-DOS command line interface.

我们可以使用set命令来从MS-DOS命令行界面打印当前使用的环境变量。

> set
List All Environment Variables For Windows
List All Environment Variables For Windows
列出Windows的所有环境变量

We can see that popular environment variables like PATH, PROMPT,OS and their values are printed in this example.

我们可以看到在此示例中打印了流行的环境变量,例如PATH,PROMPT,OS及其值。

列出Windows的环境变量 (List Environment Variable For Windows)

If we want to just print single environment variable value we can use echo command by providing the environment variable key or name. In this example, we will print the value of USERNAME.

如果我们只想打印单个环境变量值,则可以通过提供环境变量键或名称来使用echo命令。 在此示例中,我们将输出USERNAME的值。

>echo %USERNAME%
List Environment Variable For Windows
List Environment Variable For Windows
列出Windows的环境变量

为Windows设置环境变量(Set Environment Variable For Windows)

We can set an environment variable with the set command from both MS-DOS and Powershell. We will also provide the key or variable name and the value. In this example, we will set the variable name WEBSITE to the value POFTUT.COM

我们可以使用来自MS-DOS和Powershell的set命令设置环境变量。 我们还将提供键或变量的名称和值。 在此示例中,我们将变量名称WEBSITE设置为值POFTUT.COM

> set WEBSITE="POFTUT.COM"
Set Environment Variable For Windows
Set Environment Variable For Windows
为Windows设置环境变量

We can also use GUI to list and set the environment variable. We will open Environment Variables pane Computer->System Properties->Performance Settings->Environment Variables

我们还可以使用GUI列出并设置环境变量。 我们将打开环境变量窗格Computer -> System Properties -> Performance Settings -> Environment Variables

As we can see that we can add User specific or System variables from this pane. Also, the user and system variables are printed in this pane too.

如我们所见,我们可以从此窗格中添加特定于用户或系统的变量。 同样,用户和系统变量也打印在此窗格中。

Set Environment Variable For Windows
Set Environment Variable For Windows
为Windows设置环境变量

When we click to the Newin the system variables part we will see the following screen where we can set Variable name or Key and Variable value.

当我们单击“系统变量”部分中的“ New ,将看到以下屏幕,我们可以在其中设置“ Variable name或“键和Variable value

Set Environment Variable For Windows
Set Environment Variable For Windows
为Windows设置环境变量

We can see that the system variable WEBSITE is added to the system variables list.

我们可以看到系统变量WEBSITE已添加到系统变量列表中。

LEARN MORE  Linux Bash How to Define, Use Shell Variables
了解更多Linux Bash如何定义,使用Shell变量
List Environment Variable For Windows
List Environment Variable For Windows
列出Windows的环境变量

翻译自: https://www.poftut.com/how-to-set-environment-variables-for-linux-windows-bsd-and-macosx/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值