23

In linux I would go:

setenv -p MYVAR "somevalue"

But this doesn't seem to work in cygwin.

BeeBand
  • 10,953
  • 19
  • 61
  • 83

2 Answers2

35

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"
dbmikus
  • 5,018
  • 6
  • 31
  • 37
  • will MYVAR still be there next time I boot up cygwin? – BeeBand Jun 22 '11 at 18:19
  • 1
    @BeeBand I just updated my answer. If you type in the command only in your shell, you will have it until you exit your shell. If you put it in a configuration file, you will have it every time you use the shell. If you modify your configuration file and don't restart your shell, you can type `~/.bashrc` to execute the configuration file without restarting. – dbmikus Jun 22 '11 at 18:40
  • 1
    @dbmikus - you can also add that, you can access the value of the enviroment variable like this - `$MYVAR`. To see the value of the variable, type `echo $MYVAR`. – Steam Jan 01 '15 at 00:18
  • 1
    To delete an env variable, use the unset command. `unset $MYVAR` – Steam Jan 01 '15 at 00:55
  • Thanks for the suggestions, @Steam. I added those in. I've tried to keep the information ordered from most relevant to least relevant to the question. – dbmikus Nov 21 '17 at 16:06
2

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.

vsingh
  • 6,365
  • 3
  • 53
  • 57