How do I set an environment variable in cygwin? - cygwin

In linux I would go:
setenv -p MYVAR "somevalue"
But this doesn't seem to work in cygwin.

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"

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.

Related

My alias name is referring to old alias not a new one [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the difference between .bashrc, .bash_profile, and .environment?
It seems that if I use
alias ls='ls -F'
inside of .bashrc on Mac OS X, then the newly created shell will not have that alias. I need to type bash again and that alias will be in effect.
And if I log into Linux on the hosting company, the .bashrc file has a comment line that says:
For non-login shell
and the .bash_profile file has a comment that says
for login shell
So where should aliases be written in? How come we separate the login shell and non-login shell?
Some webpage say use .bash_aliases, but it doesn't work on Mac OS X, it seems.
The reason you separate the login and non-login shell is because the .bashrc file is reloaded every time you start a new copy of Bash. The .profile file is loaded only when you either log in or use the appropriate flag to tell Bash to act as a login shell.
Personally,
I put my PATH setup into a .profile file (because I sometimes use other shells);
I put my Bash aliases and functions into my .bashrc file;
I put this
#!/bin/bash
#
# CRM .bash_profile Time-stamp: "2008-12-07 19:42"
#
# echo "Loading ${HOME}/.bash_profile"
source ~/.profile # get my PATH setup
source ~/.bashrc # get my Bash aliases
in my .bash_profile file.
Oh, and the reason you need to type bash again to get the new alias is that Bash loads your .bashrc file when it starts but it doesn't reload it unless you tell it to. You can reload the .bashrc file (and not need a second shell) by typing
source ~/.bashrc
which loads the .bashrc file as if you had typed the commands directly to Bash.
Check out http://mywiki.wooledge.org/DotFiles for an excellent resource on the topic aside from man bash.
Summary:
You only log in once, and that's when ~/.bash_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
Once you log in, you can run several more shells. Imagine logging in, running X, and in X starting a few terminals with bash shells. That means your login shell started X, which inherited your login shell's environment variables, which started your terminals, which started your non-login bash shells. Your environment variables were passed along in the whole chain, so your non-login shells don't need to load them anymore. Non-login shells only execute ~/.bashrc, not /.profile or ~/.bash_profile, for this exact reason, so in there define everything that only applies to bash. That's functions, aliases, bash-only variables like HISTSIZE (this is not an environment variable, don't export it!), shell options with set and shopt, etc. For an example, see: My .bashrc
Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.
From the bash manpage:
When bash is invoked as an
interactive login shell, or as a
non-interactive shell with the
--login option, it first reads and executes commands from the file
/etc/profile, if that file exists.
After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and
~/.profile, in that order, and reads
and executes commands from the first
one that exists and is readable. The
--noprofile option may be used when the shell is started to inhibit this
behavior.
When a login shell exits, bash
reads and executes commands from the
file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either .bashrc or .bash_profile, and then have the other file source the first one.
.bash_profile is loaded for a "login shell". I am not sure what that would be on OS X, but on Linux that is either X11 or a virtual terminal.
.bashrc is loaded every time you run Bash. That is where you should put stuff you want loaded whenever you open a new Terminal.app window.
I personally put everything in .bashrc so that I don't have to restart the application for changes to take effect.

export: Command not found raised by linux source command

after I installed Anaconda package on the server, I'm then trying source ~/.bashrc to set env variable, but it raise an error of export: Command not found, my .bashrc file is like this:
# added by Anaconda3 4.2.0 installer
export PATH="/projdata3/info_fil/wangtao/conda/bin:$PATH"
can anyone help? thanks very much!
Since you're using tcsh, not bash, you should edit your .cshrc and add the line:
set path = ( /projdata3/info_fil/wangtao/conda/bin $path )
Then use source .cshrc.
If your shell is .csh please use setenv to export a variable which in bash you would normally do with export
In bash, export My_VARIABLE=/some/location/or/.something/
In csh shell, it would be setenv My_VARIABLE /some/location/or/.something/
If echo $SHELL command is giving /usr/local/bin/tcsh then it means you are working in csh shell.
In csh shell, if you want to set environment variables, the syntax goes like this
setenv <variable_name> <variable_value>
Here is the useful csh commands link for reference
If you want to enter bash shell from csh shell, enter command bash
Then this command will work: export <variable_name>=<variable_value>

Can you run a script every time /bin/sh is invoked?

With bash, you can set your ~/.bashrc file to run something every time a new bash shell is created. Is it possible to do the same thing with /bin/sh? (This is on Debian, by the way).
For now, I just want to echo 'I am sh' when /bin/sh is invoked. It's easy to do in bash ("echo 'I am bash'" placed at the top of the file).
Thanks!
When starting a login shell of dash, which is /bin/sh on debian-like systems, it will read ~/.profile. If you also want a configuration file read for interactive non-login shells, add the following line to your ~/.profile file:
ENV=$HOME/.shinit; export ENV
Then, with the variable ENV appearing in the environment, the file $HOME/.shinit will be sourced with every new interactive (dash) shell.
You may change the file name specified by ENV to any file name you prefer.
To assure a dash login shell has added ENV to the environment, you may need to logout and log back in, or possibly reboot, depending on your system setup.
Documentation
This is documented in man dash:
A login shell first reads
commands from the files /etc/profile and .profile if they exist. If the environment variable ENV
is set on entry to an interactive shell, or is set in the .profile of a login shell, the shell
next reads commands from the file named in ENV. Therefore, a user should place commands that are
to be executed only at login time in the .profile file, and commands that are executed for every
interactive shell inside the ENV file.
Example
Suppose that we have files set up like:
$ echo "ENV=$HOME/.shinit; export ENV" >>~/.profile
$ cat .shinit
echo FOUND ME
Since I just added the ENV line to the ~/.profile file, ENV is not yet in the environment. If we run dash:
$ dash
$
Nothing happened because this is a non-login shell and ENV is not yet in the environment.
If we start a login shell, ENV is placed in the environment and ~/.shinit is run:
$ dash -l
FOUND ME
If, as a child of that shell, we run an interactive non-login shell, then ~/.shinit will be run because the parent shell created the ENV variable:
$ dash
FOUND ME
The environment created by the login shell above only affects its children. To assure that all interactive dash shells have ENV in their environment may, as mentioned above, require logging out and back in, or a reboot.

Why do exported variables but not aliases from .bash_profile work in non-login shells?

I'm using the Fedora 20 graphical desktop. I found the alias put in the .bash_profile didn't have effect. Then I find the graphical terminal is not a login shell, so the bash_profile is not read at all.
Now it's weird to me that the export command does have effect in .bash_profile.
My .bash_profile is as below:
#bash_profile
export mytest=bash_profileIsRead
alias kk=ls
Test result:
$ shopt login_shell
login_shell off
$ echo $mytest
bash_profileIsRead
$ kk
bash: kk: command not found...
There's nothing unusual or surprising about this.
Your .bash_profile is run once per session, by your login shell. It is not run by other shells run later in your session.
.bashrc, by contrast, is run by every interactive shell instance, so things like aliases and shell functions placed there will be honored throughout the session.
Environment variables only need to be set once, because they're inherited by subprocesses (every subprocess, not just shells!). Aliases are not inherited, so they need to be set in every shell.
See the DotFiles page on the wooledge.org wiki (maintained by irc.freenode.org's #bash channel) for more.
Aliases are not inherited like environment variables. They should not be placed in profile, but instead in the .bashrc file.
Basically, .profile (or .bash_profile) is for things that are inherited (e.g. env variables) and the rc file is for things that must be re-initialized in non-login shells, such as aliases.

How can I permanently export a variable in Linux?

I am running RHEL 6, and I have exported an environment variable like this:
export DISPLAY=:0
That variable is lost when the terminal is closed. How do I permanently add this so that this variable value always exists with a particular user?
You can add it to your shell configuration file, e.g., $HOME/.bashrc or more globally in /etc/environment.
After adding these lines, the changes won't reflect instantly in GUI-based systems. You have to exit the terminal or create a new one and on the server, log out the session and log in to reflect these changes.
You have to edit three files to set a permanent environment variable as follow:
~/.bashrc
When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file:
export DISPLAY=0
~/.profile
Same as bashrc you have to put the mentioned command line at the end of this file to have your environment variable in every login of your OS.
/etc/environment
If you want your environment variable in every window or application (not just terminal window) you have to edit this file. Add the following command at the end of this file:
DISPLAY=0
Note that in this file you do not have to write export command
Normally you have to restart your computer to apply these changes. But you can apply changes in bashrc and profile by these commands:
$ source ~/.bashrc
$ source ~/.profile
But for /etc/environment you have no choice but restarting (as far as I know)
A Simple Solution
I've written a simple script for these procedures to do all those work. You just have to set the name and value of your environment variable.
#!/bin/bash
echo "Enter variable name: "
read variable_name
echo "Enter variable value: "
read variable_value
echo "adding " $variable_name " to environment variables: " $variable_value
echo "export "$variable_name"="$variable_value>>~/.bashrc
echo $variable_name"="$variable_value>>~/.profile
echo $variable_name"="$variable_value>>/etc/environment
source ~/.bashrc
source ~/.profile
echo "do you want to restart your computer to apply changes in /etc/environment file? yes(y)no(n)"
read restart
case $restart in
y) sudo shutdown -r 0;;
n) echo "don't forget to restart your computer manually";;
esac
exit
Save these lines in a shfile then make it executable and just run it!
Add the line to your .bashrc file or .profile.
The variables set in file $HOME/.profile are active for the current user, and the ones in /etc/profile are global. The .bashrc file is pulled on each Bash session start.
On Ubuntu systems, use the following locations:
System-wide persistent variables in the format of JAVA_PATH=/usr/local/java store in
/etc/environment
System-wide persistent variables that reference variables such as
export PATH="$JAVA_PATH:$PATH" store in
/etc/.bashrc
User-specific persistent variables in the format of PATH DEFAULT=/usr/bin:usr/local/bin store in
~/.pam_environment
For more details on #2, check this
Ask Ubuntu answer.
NOTE: #3 is the Ubuntu recommendation, but it may have security concerns in the real world.
If it suits anyone, here are some brief guidelines for adding environment variables permanently.
vi ~/.bash_profile
Add the variables to the file:
export DISPLAY=:0
export JAVA_HOME=~/opt/openjdk11
Immediately apply all changes:
source ~/.bash_profile
Source: How to Set Environment Variables in Linux
A particular example:
I have Java 7 and Java 6 installed, I need to run some builds with 6, others with 7. Therefore I need to dynamically alter JAVA_HOME so that Maven picks up what I want for each build. I did the following:
created j6.sh script which simply does export JAVA_HOME=... path to j6 install...
then, as suggested by one of the comments above, whenever I need J6 for a build, I run source j6.sh in that respective command terminal. By default, my JAVA_HOME is set to J7.

Resources