I’ve written a simple bash script that adds an alias to my .bashrc automatically, and when it finishes, I would like it to source the .bashrc
It works fine as of now, for example
./addalias.sh ls 'ls -l'
properly appends ‘alias ls=’ls -l’ to the .bashrc, but doesn’t source it.
The code is as follows:
#!/bin/bash
FIRST=$1
SECOND=${2:-cd `pwd`}
echo alias $FIRST="'$SECOND'" >> /home/oscar/.bashrc
echo alias $FIRST="'$SECOND'"
source /home/oscar/.bashrc
That doesn’t work, nor does running an alias (“sourcebash”) to source the bash instead of the last line.
Any thoughts on how this could be fixed?
The shell that runs ‘addalias.sh’ does source the
.bashrcfile; it then exits. It does not and cannot affect the parent shell’s environment.You’d have to invoke the command as:
Or:
(Now fixed: And I’m not convinced that, even in a question, playing with
sudo rm -fr /*is remotely sensible. There’s too much risk of an idiot copying and not realizing.)