views:

60

answers:

1

I'm trying to set the environment variables in shell script. The command "source .bashrc" is not executed. As long as type the last line in the terminal, everything works fine. What's wrong with my script? thx.

echo "export CLASSPATH=.:$HOME/java/lib
export JAVA_HOME=$HOME/java
export PATH=.:$PATH:$JAVA_HOME/bin" >> .bashrc
source .bashrc
+3  A: 

source .bashrc is being executed, but it only affects the shell that's running your script, not its parent shell, which is your interactive shell. In order for what you're doing to work, you would have to source your script (or, y'know, use ., which is shorter).

chaos
Thx for you reply. But I still don't get it. can you explicitly tell me what to do?
CKeven
If your script is called `dosomethingtomyenv` and is in your current directory, instead of running it as `./dosomethingtomyenv`, do `. ./dosomethingtomyenv`.
chaos
wow, it works!!! thx man!
CKeven
what if i'm running the script remotely using ssh? Can i do "ssh -x $username@$node "echo Installing J2EE at $node;. ./$installjava $j2re"?
CKeven
Yeah, assuming that `$installjava` and `$j2re` are variables on the local side.
chaos
But it seems it doesn't work tho. Btw, what does the "." do? I'm not aware of that...
CKeven
`.` is the same as `source`. Don't know why it wouldn't work remotely. `$installjava` is the name of a script on the remote system that does something like you posted, plus a j2re install?
chaos