sudo cat ~/.bashrc $addpath > ~/.bashrc
… is actually extremely problematic.
You will almost certainly wipe out the existing contents of
~/.bashrc: the shell will likely truncate
~/.bashrc before it runs
cat reads the file. See Unix Big Redirection Mistake #1. The way to append to a file is using
echo "$blah" >> ~/.bashrc.
$addpath variable is incorrectly defined as
addpath="export $PATH". You would be trying to export a variable named
~/bin:/bin:/usr/bin because there is no
PATH=… assignment. You meant
cat $addpath has two errors: it's the wrong command (you want
echo), and you failed to double-quote its argument. Therefore, it will just try to read files named
PATH=~/bin:… — if you're lucky. If you're unlucky,
$PATH could expand to a string that contains a nasty shell command. That scenario is admittedly farfetched, but that kind of carelessness in quoting is how exploits get introduced. When writing shell scripts, double-quote every variable you use unless you have a good reason not to.
sudo is unnecessary for that operation; it could even backfire if the
sudoers doesn't allow
cat to be executed. By the way,
sudo would only elevate the
cat ~/.bashrc to root privileges; the writing would still be done using output redirection in the non-elevated context.
~/.bashrc is not a good place to put a statement to edit
$PATH, since it gets executed with every interactive shell. If you run
bash from within Bash, the subshell would unexpectedly have its
$PATH redefined. A more appropriate place might be
After correcting for the mistakes above, with the code
echo "$PATH" >> ~/.bash_profile
… you would be "freezing" the current value of
~/.bash_profile. Instead, you probably want to prepend
~/bin to whatever
$PATH exists when
~/.bash_profile is executed in the future (notice the single quotes instead of double quotes):
echo 'export PATH=~/bin:$PATH' >> ~/.bash_profile