There are two scenarios:
- You are using Linux
- You are using Mac Os X
For both you want to source ~/.bashrc
in your profile that gets loaded, or sourced, when your shell for your terminal starts.
LINUX
In Linux, ~/.profile
is automatically source in your shell when it starts. So, if you go to your terminal and type cd ~; ls -A
, you will see all of your files and directories in your home directory (/home/usrname/
). You should see a file called .profile
. This is the file that is automatically "sourced" when you start the terminal.
If you want to add aliases and functions to ~/.bashrc
(which is what I do), then you should (inside of ~/.profile
add an if statement that checks if ~/.bashrc
is a non-empty file and then source it.
To check if your ~/.profile
already does this enter nano ~/.profile
. This will open it up in a text editor (you can use gedit if you know you have it or even vim if you know how to use it). You may get an empty text file (you shouldn't), but if you do simply proceed. If you do not see a line anywhere that says source ~/.bashrc
, then enter the following lines somewhere (put it in a reasonable place like at the end or beginning and not in the middle of an if statement):
if [ -s ~/.bashrc ]; then
source ~/.bashrc;
fi
This checks if ~/.bashrc
is a non-empty file (with if [ -s ~/.bashrc ]
), and if it is such, it sources it. Pretty simple. Now, you can add any valid alias, function, variable, etc to ~/.bashrc
.
MAC OS X
By default (prior to 2019 Catalina), Mac OS X sources ~/.bash_profile
. To be able to add things to ~/.bashrc
(which is, again, what I do in OS X, as well), then you follow the same procedure as linux. In the terminal enter cd ~; nano .bash_profile
. Check for a line that says source ~/.bashrc
and if it's not there add the if statement above (if [ -s ~/.bashrc ]; then source ~/.bashrc; fi
).
NOTES
When writing an if statement, be sure to leave spaces between basically everything (for example if [ -s ~/.bashrc]; then echo "found"; fi
does not work because there is no space beween ~/.bashrc
and ]
- the interpreter will think this is one word).
If you want to figure out how to check for stuff in an if statement better, go to terminal and enter man \[
; this essentially gives you the run-down of an if statement. For example, if [ -f /path/to/file ]; then echo "it's a file"; else echo "not a file"; fi
checks if /path/to/file
is a file and if it is prints to stdout accordingly. I always reference this as well for easier-on-the-eyes and quick reference: 7.2. File test operators.
I hope this helps. I remember how confused I was when I started all of this stuff (which wasn't too long ago); so, good luck with your future UNIX endeavours!
zsh
shell (not i.e:bash
) configured in 2019+ OS X (currently Catalina), see apple.stackexchange.com/a/338622/192005