Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an environment variable in cygwin?

Tags:

In linux I would go:

setenv -p MYVAR "somevalue" 

But this doesn't seem to work in cygwin.

like image 479
BeeBand Avatar asked Jun 22 '11 18:06

BeeBand


People also ask

How do I change the working directory in Cygwin?

To change directory, you can use cd command with the target directory parameter. The target directory can be expressed in a DOS-like manner (d:/ or d:/Ogo1. 2). Note that drives in Cygwin are treated as directories, and the usage of forward slash in place of the DOS backslash (d:/Ogo1.

Where is my Cygwin home directory?

In a new installation of Cygwin, your home directory will be in C:/cygwin/home/<user>/ , and can be accessed by the usual ~ shortcut.

What is Cygwin PATH?

Description. The cygpath program is a utility that converts Windows native filenames to Cygwin POSIX-style pathnames and vice versa. It can be used when a Cygwin program needs to pass a file name to a native Windows program, or expects to get a file name from a native Windows program.


2 Answers

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.

like image 41
vsingh Avatar answered Sep 30 '22 15:09

vsingh


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:

  • difference between .bash_profile and .bashrc
  • difference between .profile and .bash_profile

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" 
like image 137
dbmikus Avatar answered Sep 30 '22 14:09

dbmikus