Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Its pretty simple, if you understand that each invocation of the shell gets its own copy of the environment that was exported by it's parent, and commands (other than builtin shell commands) run in their own shell.
Therefore, if you say "set joe=fred", you've just set the value of joe in your current shell environment. If you then start another process, that process will get it's own environment and will inherit (get a copy of) only the variables that were exported to you and the ones you exported in the current shell. Therefore, the child process will not get the value of "joe", because you didn't export it. In some shells, you had to both "set" and "export" a variable to pass it on to your offspring. You can use just "export joe=fred" in bash as a shortcut.
I believe "setenv()" is only available to "C" programs and it works similarly to "set", but in "C", if you spawn another process, you have to specify how the environment will be passed to the child process. That is a more complex subject.
I hope this helps. I've pretty much managed to confuse myself. Maybe someone else can try to explain it. To simplify the situation, I'd advise you to use "export" whenever you are calling another script or application from your script. Usually, you want to carry your variables forward. If you are just writing a simple script that won't call any other scripts, you can just use "set".
OK,. First of all let me say that I am a Linux newbie.
I tried the above mentioned export ENV_NAME=value, but it did not work as thought. I'm trying to add something to my PATH variable, but the result is that I have to set this variable up for each and every bash window I open. Can someone please explain to me what I am doing wrong? I do not seem to grasp what is going on.
A brief explanation of what I am doing:
# export|grep PATH
# export PATH=<what the above grep returned for PATH and add>:/usr/java/.....
Then javac works for one "session". In the next window I open I have to do this again. Very frustrating!
I gathered somewhere that I have to edit my .bashrc file, but do not know what to do with that file (~/.bashrc).
Ok, in order to understand what is happening, you need to realize that export only exports to the children of your shell ... not the parent. i.e. When you close your shell, you return to the parent environment and all that was set in your shell is lost with it.
To simplify what you are doing, you can reference your PATH variable as $PATH, so you can update your path with "export PATH=$PATH:/usr/java/...".
Finally, if you need to update your PATH permanently, you do need to edit your login profile. In Redhat, I think you'll find your PATH in ~/.bash_profile. It should look something like the following...
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME
......... all you need to do is add your Java path to the "PATH=$PATH:$HOME/bin" line.
First of all, thanks for the quick response. In spite of this my efforts where pointless. It still does not work. The line in my .bash_profile now looks like this:
PATH=$PATH:$HOME/bin:/usr/java/j2sdk1.4.1_05/bin
But after exiting the vi the env variable PATH was not altered at all. Even after I've opened another bash window. If I write this
export PATH=$PATH:/usr/java/j2sdk1.4.1_05/bin
it works like you suggested, only for the current window. What am I doing wrong?
I'm sorry I forgot to mention that you need to log completely out and log back in for your .bash_profile changes to take effect. That may mean exiting out of X-windows.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.