Linux Environment Variables - linux

I'm trying to change my environment variables using a shell script, but the shell script isn't changing it.
Sorry for asking that type of question, but I'm learning about Linux, I just can't figure out what I'm doing wrong here..
Here's the script
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_60
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/jre/bin
After I execute this .sh file, when I type
echo $PATH
It doesn't return the new PATH I setted

Instead of executing it like
$ ./script.sh
source it with
$ source script.sh
The first variant creates a new process which does not pass back its modified environment variables to the calling process, while sourcing just executes the script within the already running shell.
See also What is the difference between executing a bash script and sourcing a bash script?

As Andreas explained, you need to source the definition (and you need the export to happen in your current shell process, not in a subshell or child process)
You could instead define in your ~/.bashrc some shell functions to do the job:
## remember the original path at start of the interactive bash
export ORIGINAL_PATH=$PATH
function prepare_for_java() {
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_60
export PATH=$ORIGINAL_PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
}
function forget_java() {
unset JAVA_HOME
export PATH=$ORIGINALPATH
}
then in your terminal you would type
prepare_for_java
before starting doing Java things, and
forget_java
after you've done with them.
I would suggest to read the Advanced Bash Scripting Guide and Advanced Linux Programming (to get a wider picture).
See also bash(1), credentials(7), fork(2), execve(2), environ(7)

Related

How to set java environment variables using shell script

I know I can just type
$ vi .bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
However how to use shell script to do it? I prefer to write shell script because I need to configure multiple servers, if type one by one I would take me a long time to go.
Can someone guide me how to do this? Thanks a lot!
EDIT: I just realized that you wanted a script to automate the process of adding environment variables. These commands may work for you:
echo "export JAVA_HOME=/usr/lib/jvm/java-7-oracle" >>~/.bashrc
echo "export PATH=$JAVA_HOME/bin:$PATH" >>~/.bashrc
What this does is append the given text to .bashrc. Instead of copying .bashrc files from server to server, run these commands (you could probably write a script for these) on each server. This preserves the contents of the original rc files on each server, which I find is a better idea than completely overwriting the file.
Original answer
Your .bashrc file is actually written as a shell script. You would place the exact same lines in the shell script, possibly with a hashbang at the beginning of the file. For example:
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
# do java stuff here...
If you were to "do java stuff" in this script, this would work fine. However, if these variables are going to be used outside of the script, you would have to "source" this file. That is what happens with .bashrc. Before the first prompt is given, Bash runs source ~/.bashrc to publish the variables defined in .bashrc.

Linux: export environment variable in a shell script to make it flexible on any server

In order to run a Tcl script on Linux, I need to set the environment variable "$LD_LIBRARY_PATH" each time.
For convenience, I develop a shell script to do this.Currently, on my own server, if I type
echo $LD_LIBRARY_PATH
the result is:
/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib
so in my shell script I write the following code:
export LD_LIBRARY_PATH="/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
where the "$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64" part is what I want to add. It works well. Now the issue is:
If I want to run the script on any server, so the original "$LD_LIBRARY_PATH" will be different, based on my understanding. So how to make it flexible on any server?
I try this in my shell script:
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
But not so sure,
I am new to system stuffs, need some help. Hope explain the issue clearly.
If your default shell is bash, i would define the variables in ~/.bashrc like:
export INSTALL_ROOT=...##assuming a lready defined
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64
So in this way, you dont have to worry anything about setting variables in multiple shell scripts as this .bashrc is going to setup variable for you beforehand.

How to export a library path from a child shell script and use the path in the parent shell?

I have the following files
build.sh
source exportpath.sh
exportpath.sh
export LD_LIBRARY_PATH=/usr/local/lib/
But,when I am executing this build.sh and then running the echo $LD_LIBRARY_PATH command I am not able to get the LD_LIBRARY_PATH value. How to set this value to the current shell.
I found that we have to source it in the current shell. But I want it done by a shell script.
When I am using source exportpath.sh in current shell, then my LD_LIBRARY_PATH is working but I want this should be done by a shell script.
How can I do this?
There is no magical way to have an environment variable set in the child shell be propagated to the parent shell. See this reply
You could implement some convention, for instance by having exportpath.sh taking a filename, that build.sh would later source (or use eval).
You may want to have your own shell function to wrap both.
But you should usually not do such weird tricks. For example, you could wrap some of your programs into a shell script setting the LD_LIBRARY_PATH (mozilla or firefox is often doing such things).
echo $LD_LIBRARY_PATH
Note the Use of "$" to reference the variable value.

shell export variable not come into effect

I (on mac osx) often use
export http_proxy=http://192.168.0.205:1099
to proxy http connection to get a highed download speed. To make things easy, I wrote a shell file named proxy.sh to do this:
#!/bin/sh
export http_proxy=http://192.168.0.205:1099
Before I downlaod, I execute proxy.sh shell command, but I found it did't not come into effect.It lost http_proxy variable in current commnad window(terminal). I must type export command in current terminal,it will come into effect.
So I want to know what's reason for this and a solution? thanks.
Running a shell script "normally" (with proxy.sh for example) results in that running in a sub-process so that it cannot affect the environment of the parent process.
Using . or source will run the shell script in the context of the current shell, so it will be able to affect the environment, using one of the following:
. proxy.sh
source proxy.sh
Another possibility (if you're using bash at least) is to create an alias to do the work for you. You can use something like:
alias faster='export http_proxy=http://192.168.0.205:1099'
so that you can then simply type faster on the command line and it will export that variable (in the context of the current shell).
You could also allow for one-shot settings such as:
alias faster='http_proxy=http://192.168.0.205:1099'
and then use:
faster your_program
which would translate into:
http_proxy=http://192.168.0.205:1099 your_program
That's a bash way to set a variable for just the one invocation of a command.
The export variable will only apply to the script -- if you want it to apply to the shell, you need to use source, and execute the script like so:
. ./proxy.sh
or:
source ./proxy.sh
Note the "." in the first example -- the dot follow by space means the script will apply to the shell.
The reason why your script does not work has been explained by Drakosha & how to make your script work has been explained by Anothony. But with the export in the script you need to source your script each time you open a new terminal. A better solution will be to add the export in .bash_profile or .bashrc
Hope this helps!
When executing a shell script a new shell is launched, the script is executed, and the shell dies. That's why you don't see the variable defined in your shell.
I suggest using an alias for the same purpose.

export env at archlinux shell

when I at company , I have to export 3 enviroment variables, http_proxy,https_proxy,all_proxy,
I wrote a file ~/bin/setproxy like this
#! /bin/sh
export http_proxy=http://......:8888
export https_proxy=http://......:8888
export all_proxy=http://......:8888
but when I execute this file at bash, then use env | grep http_proxy , I got nothing.
but "source ~/bin/setproxy" works, but is there any way short this to 1 word command.
I wrote another file only 1 line,
source ~/bin/setproxy
but it does not work.
When you execute that script a sub-shell is spawned and the three export are perfomed in that shell, when the script finishes, the sub-shell exits, that's why you don't see the environment variables as set.
You could put that code in a function, say in your .bashrc, and call that, this way it will work, something like the following:
function setproxy {
export http_proxy=http://......:8888
export https_proxy=http://......:8888
export all_proxy=http://......:8888
}
I think your problem is because you are executing either:
~/bin/setproxy
or:
your_other_file_which_sources_setproxy
In both those cases, they run in a subshell which means the export is in that subshell, not the shell you're calling them from.
You can either use the short form of source:
. ~/bin/setproxy
or create an alias:
alias sp='source ~/bin/setproxy'
in your .bashrc or other startup scripts.
That latter solution will allow you to just execute:
sp
to do the work.

Resources