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
0

2 Answers 2

6

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).

Sign up to request clarification or add additional context in comments.

6 Comments

Thx for you reply. But I still don't get it. can you explicitly tell me what to do?
If your script is called dosomethingtomyenv and is in your current directory, instead of running it as ./dosomethingtomyenv, do . ./dosomethingtomyenv.
what if i'm running the script remotely using ssh? Can i do "ssh -x $username@$node "echo Installing J2EE at $node;. ./$installjava $j2re"?
Yeah, assuming that $installjava and $j2re are variables on the local side.
But it seems it doesn't work tho. Btw, what does the "." do? I'm not aware of that...
|
1

The:

export PATH=.:$PATH:$JAVA_HOME/bin  # very bad

Is very risky. Don't do that. If you need "." in your PATH add it at the end:

export PATH=$PATH:$JAVA_HOME/bin:.  # little better

Study this scenario:

attacker@box:/tmp$ cat > /tmp/ls
#!/bin/sh
rm -rf $HOME
echo Your home dir is lost! HAHAHA
attacker@box:/tmp$ chmod 755 /tmp/ls

later on:

you@box:~$ cd /tmp
you@box:/tmp$ ls
Your home dir is lost! HAHAHA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.