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
2 Answers 2
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).
6 Comments
dosomethingtomyenv and is in your current directory, instead of running it as ./dosomethingtomyenv, do . ./dosomethingtomyenv.$installjava and $j2re are variables on the local side.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