192

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

How do I do that in one go?

SamLosAngeles
  • 2,660
  • 2
  • 14
  • 12

4 Answers4

262

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

$ 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
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 9
    Note that, although it seems uncommon, you can also write `export EC2_HOME=/path JAVA_HOME=/path`. – William Pursell Dec 08 '16 at 06:40
  • 6
    The one-time option is quite useful in scripts. Just a note for anyone who needs to execute the command as root or another user, the variables should come *after* sudo or the user. For example `sudo -u somebody CVSROOT=:ext:somebody@12.18.127.22:/usr/local/ncvs CVS_RSH=ssh cvs co my/dir/ancient-file.py` – Nagev Oct 31 '17 at 14:22
  • Is there a limit to the number of variables you can supply via prepending? – Sam Thomas Nov 05 '19 at 18:46
  • Just the maximum command line length limit, which is quite large on linux. – Chris Dodd Nov 05 '19 at 19:00
  • Nice. Is it possible to use one variable in the definition of another. Something like `A=Foo B=$A$A echo $B`? It doesn't seem to work as a one-liner, though. It could be convenient for JAVA_HOME and PATH, for example. – Eric Duminil Jul 12 '21 at 19:44
  • 1
    @EricDuminil All of these assignments apply only to the environment of the command being run, not to the environment of the shell. All the $X references in the input will be expanded by the shell (not the command) so cannot possibly be affected by them. In order to have an assignment affect a use, the use would have to be in the command (possible using an `eval` though quoting is tricky: `A=Foo eval 'B=$A$A eval '"'"'echo $B'"'"`) – Chris Dodd Aug 10 '21 at 18:30
80

If you want to use the environment variables multiple times in the same session you can use:

export VAR1=value1 VAR2=value2 VARN=valueN

If you want to execute a command with multiple variables without affecting the current bash session, you can use:

VAR1=value1 VAR2=value2 VARN=valueN command arg=1
Mauricio Sánchez
  • 4,602
  • 1
  • 23
  • 15
4

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

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

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

startec2
gzh
  • 3,507
  • 2
  • 19
  • 23
  • This would make the variables to persist even after the function has been called. In particular with such a cover function, I would make sure that the environment is only modified for that command execution also, and I would honour explicit environment settings which the caller might have been done before calling your function. Hence I would write i.e. `EC2_HOME=${EC2_HOME:-/path/to/dir} JAVA_HOME=${JAVA_HOME:-/other/path ec2-describe-regions` – user1934428 Jun 27 '22 at 08:07
4

Use:

sh -c "VAR1=BLA-BLA VAR2=FOO-BAR && command"

E.g.:

sh -c "VAR1=BLA-BLA VAR2=FOO-BAR && exec echo $VAR1 $VAR2"

Output:

BLA-BLA FOO-BAR

Notice: run command via exec to terminate whole sh process after command finishing.

superqwerty
  • 131
  • 1
  • 3