BeeBand
BeeBand

Reputation: 11493

How do I set an environment variable in cygwin?

In linux I would go:

setenv -p MYVAR "somevalue"

But this doesn't seem to work in cygwin.

Upvotes: 24

Views: 77324

Answers (2)

dbmikus
dbmikus

Reputation: 5319

By default Cygwin is running the Bourne shell or Bash, so the command to set a variable is different. This is the code you need:

export MYVAR="somevalue"

The export part lets the shell know that it is an environment variable instead of a local variable.

If you type ls -a in your home directory, you should see some or all of the following files:

.bashrc
.bash_profile
.profile

.bash_profile is executed for login shells, and .bashrc is executed for interactive non-login shells. To most simply ensure that your environment variable is always set, open up .bash_profile and add the text:

export MYVAR="somevalue"

Your shell with then execute .bash_profile every time it starts up, and it will run this command. You will then have the MYVAR variable accessible all of the time. If you didn't export the variable, it would only be accessible within your .bash_profile file.

You can check that this variable is defined by printing its value to your shell:

echo $MYVAR

You can delete (unset) the variable with:

unset $MYVAR

Brief words on shell config files

As an aside, regarding .bashrc vs .bash_profile vs. .profile, see these answers:

For simplicity of configuration, I recommend sourcing your .bashrc file from .bash_profile. Add this to .bash_profile:

if [ -f ${HOME}/.bashrc ]; then
   source ${HOME}/.bashrc
fi

This will load .bashrc from .bash_profile.

If you do this, you can instead put the following line in .bashrc, if you wish:

export MYVAR="somevalue"

Upvotes: 35

vsingh
vsingh

Reputation: 6759

The best way to set up environment variables in cygwin is to create a bash profile and execute that profile everytime you login and run the shell.

In my .bash_profile file , this is the setting I have

JAVA_HOME = C:/Program Files/Java/jdk1.7.0_51
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin

Once you run bash, check out echo $JAVA_HOME and you should see the path as output.

Upvotes: 2

Related Questions