why, when i restart the terminal the environment variables are restarted? - linux

i do the next:
export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
then to verify that all is correct :
mvn -version
and it works but when i close the terminal and open other terminal the environment variables are erased

Because that's how variables work. You're setting them for that session. If you want them to persist, put them in your .profile so they're set each time a new instance of your shell starts.

A process inherits its environment variables from its parent, at the time it was started. If you want to set some environment variable for all new processes, edit ~/.profile or /etc/profile, which are read by the login shell.

Related

Environment variable always reverts to old value

I'm using Linux and want to update an environment variable. I set a new entry with
export theKey=theOldValue
and checked with
printenv
Now I have a new value for this key and want to update it. I wasn't able to find any "update" commands so I run the export command again. This seems to update the entry when running printenv but whenever I try to read the value from my code I get the old entry value.
Whenever I change the value I reboot the machine.
Since this isn't working properly I tried the following
unset theKey
export theKey=theNewValue
reboot
When checking the value this variable reverted back to the old value. Does someone know what I'm missing?
The export command has the effect in the one terminal session you run it only. So you need to touch ~/.bashrc if you are using bash, and put your export entry in that file. Then to apply it in that session, do source ~/.bashrc. For future terminal sessions, this file will be loaded automatically.
See more info here.

Environment for rc.local

I need to create an environment variable that is set within a bash script. I want this environment variable to be a system wide environment variable and I also want to be able to use it in an if condition in rc.local. So far I have tried /etc/profile but rc.local does not see it when I set it there.
Which script should I set this environment variable in so that rc.local can see it when I boot?
Other question while I’m at it. Who starts rc.local?
Cheers,
Dave
Rc.local runs before the operating system is fully booted but has not yet started the login shell, so the environment variables we configured in /etc/profiles or bashrc are not executed, so no environment variables are visible during the rc.local implementation phase.
The solution is:
Add export ENVNAME=*********** before the command in rc.local

my fedora PATH variables went out, how to restore them

I wanted to add php to envirmonment variables
So I wrote:
export PATH=/opt/lampp/bin/
Then I discovered that this was wrong because it relaced the envirmonment variable PATH with only ( /opt/lampp/bin/ ).
Is there a way to restore the path to an earlier version?
restart your shell / exit your terminal. unless you edit your .bashrc file the changes to environment variable via the export command are not permanent

How to set up Environment java variable in Ubuntu bash?

I am trying to set up Java in my new laptop. I have installed java jdk and I am trying to set up the environment variable now, But I am really confused between the file .bash_profile, .bashrc, .profile. I understand the concept behind login and non login shells, but when I wrote the JAVA_HOME, PATH variable in .bash_profile I needed to manually run the command
. .bash_profile
It did not get executed at the start when I restarted the computer to make it run as .bash_profile is supposed to be called by login shells. Can anyone explain to me what is the reason behind .bash_profile not being called up?
Since .bash_profile was not being called, I decide to type the PATH, JAVA_HOME variable to the file .bashrc, While this is working is this the right way to set up environment variables?

global variable but still can't access in linux

i have trying to use some global variable in my ant file.
when i do login through terminal. i can access those variable like JAVA_HOME
but when i am trying to access variable through the ant command i am not able to find them.
global variable declared in .cshrc
setenv JAVA_HOME jdk_full_path
ant code using variable.
<property environment="env"/>
<property name="ear" value= "true"/>
<property name="home" value="${env.HOME}"/>
<property name="java_home" value="${env.JAVA_HOME}"/>
i can access home variable but i am not able to find JAVA_HOME variable ,i am executing this ant through eclipse
Please suggest me where should i declare the variable so i can access them
The problem is probably that Eclipse doesn't have JAVA_HOME in its environment.
Try logging out and back in again, then run Eclipse.
Alternatively, open a terminal, and run Eclipse from there.
The .cshrc file is only run when you start a new C shell, it is not for global variables. Since you are not starting Eclipse from the C shell it will not see any variables you set there.
Try the ~/.login file or the /etc/profile file in case eclipse starts up using bash. Both of these files are only loaded once when the user logs on so they should effect everything. You may require a restart or even a reboot for changes to these files to take effect.
Environment variables are inherited through process execution, they are not global. So, when you edit your shell's RC file to add a variable, it only takes effect for shells executed AFTER that modification, and for programs executed from those shells. If you started Eclipse from KDE/Gnome, and KDE/Gnome was started before you made that change, then KDE/Gnome never had the definition and therefore Eclipse did not either.
I recommend logging out and back in. Also, depending on your default shell setting, you may find that KDE/Gnome is being started through bash or something else, meaning that you'll need to modify .bashrc instead of .cshrc.
You should define this variables at one of the following files:
~/.profile or ~/.bash_profile runs only with login shells i.e when you first log in into system.
~/.bashrc file runs every time you open a new non-login bash shell such as xterm
So, you should add to this files the line:
export JAVA_HOME=jdk_full_path
You must re-login for the changes take efect

Resources