如何为bash命令设置多个env变量

时间:2022-10-18 17:42:54

I am supposed to set the EC2_HOME and JAVA_HOME variables before running a command (ec2-describe-regions)

在运行一个命令之前,我应该设置EC2_HOME和JAVA_HOME变量(ec2- description -regions)

How do I do that in one go?

我该怎么做呢?

2 个解决方案

#1


89  

You can one-time set vars for a single command by putting them on the command line before the command:

您可以通过在命令前将它们放在命令行上,一次性设置一个命令的vars:

$ EC2_HOME=/path/to/dir JAVA_HOME=/other/path ec2-describe-regions

Alternately, you can export them in the environment, in which case they'll be set for all future commands:

或者,您可以在环境中导出它们,在这种情况下,它们将被设置为所有未来的命令:

$ export EC2_HOME=/path/to/dir
$ export JAVA_HOME=/other/path
$ ec2-describe-regions

#2


2  

As other *nix system, you can add function as following in your .bashrc file under your HOME directory.

作为其他*nix系统,您可以在您的主目录下的.bashrc文件中添加以下功能。

function startec2(){
    EC2_HOME=/path/to/dir
    JAVA_HOME=/other/path 
    ec2-describe-regions
}

Now, you can start your program by the following command:

现在,您可以通过以下命令启动程序:

startec2

#1


89  

You can one-time set vars for a single command by putting them on the command line before the command:

您可以通过在命令前将它们放在命令行上,一次性设置一个命令的vars:

$ EC2_HOME=/path/to/dir JAVA_HOME=/other/path ec2-describe-regions

Alternately, you can export them in the environment, in which case they'll be set for all future commands:

或者,您可以在环境中导出它们,在这种情况下,它们将被设置为所有未来的命令:

$ export EC2_HOME=/path/to/dir
$ export JAVA_HOME=/other/path
$ ec2-describe-regions

#2


2  

As other *nix system, you can add function as following in your .bashrc file under your HOME directory.

作为其他*nix系统,您可以在您的主目录下的.bashrc文件中添加以下功能。

function startec2(){
    EC2_HOME=/path/to/dir
    JAVA_HOME=/other/path 
    ec2-describe-regions
}

Now, you can start your program by the following command:

现在,您可以通过以下命令启动程序:

startec2