Progress Programmer
Progress Programmer

Reputation: 7394

shell script not executed

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

Upvotes: 1

Views: 426

Answers (2)

Michał Šrajer
Michał Šrajer

Reputation: 31182

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

Upvotes: 1

chaos
chaos

Reputation: 124277

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

Upvotes: 6

Related Questions