How to Reload bash_profile, zsh profiles from Command Line
For anyone making customizations to their command line appearance, prompt, adding aliases, a new PATH, setting environment variables, or otherwise changing their bash_profile, bashrc, zshrc, and related shell profiles, it’s helpful to know how to quickly reload the profile.
Many users simply launch a new shell or open a new terminal window, but there is no need to do that, there’s a way to directly refresh the profile in use thanks to the source command.
Reload .bash_profile Bash Profile
Refresh bash_profile from the users home directory:
source .bash_profile
Reload .zshrc ZSH Profile
Refresh zsh profile from the users home directory:
source ~/.zshrc
Reloading and refreshing other command line shell profiles, tcsh, fish, etc is just a matter of pointing source at the initialization script like so:
source ~/.profile
source ~/.tcshrc
This is much easier and faster than launching a new shell, give it a try the next time you’re making adjustments to your command line initialization files.
And yes this works in Mac OS X Terminal as well as linux.
You can set up your .bashrc file to be portable between operating systems (that run BASH). You can also customize your .bashrc file to be dependent on who you are and on what host.
Here’s how I set it up for OS-specific settings:
# OS-specific variables – no more commenting out or editing .bash* files when setting up a new system
case $(uname) in
Darwin)
# There are a lot of directories on Macs with hidden directories and files:
shopt -u dotglob
# uncomment the following to activate bash-completion:
# This isn’t available in Mac & brew isn’t able to download the package (20131111-0928)
[ -f /etc/profile.d/bash-completion ] && source /etc/profile.d/bash-completion
alias ejectall=”osascript -e ‘tell application \”Finder\” to eject (every disk whose ejectable is true)'”
export EDITOR=/usr/bin/vim
;;
FreeBSD)
# Some FreeBSD specific variables:
export EDITOR=/usr/local/bin/vim
# FreeBSD specific command aliases:
alias vmstat=’vmstat -n 5′
# Activate bash-completion:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
;;
Linux)
# Some Linux specific variables:
export LS_OPTIONS=’ –color=auto’
# Command aliases:
alias fr=’free -m’
# Some more environmental things:
# This Debian/Ubuntu specific:
[ -f /etc/environment ] && . /etc/environment
# Activate bash-completion:
[ -f /etc/bash_completion ] && . /etc/bash_completion
;;
esac
Or just invoke a new login shell and reload them all at once.
`exec $SHELL -l`
Just get this as an alias in your .aliases:
`alias rel=’exec $SHELL -l’`
And if typing the word “source” troubles your tired fingers, simply use a period, which does the same thing:
. ~/.bash_profile
There are actually two such files, with slightly different purposes (at least, for bash):
* .bash_profile is run when bash starts as an interactive login shell
* .bashrc is run when bash starts an an interactive non-login shell.
I put variables in .bash_profile and aliases in .bashrc.
Thanks for this post. I wondered how to do this. Posts concerning any and all things regarding the Terminal and related functions, (commands, shells, scripting, etc..), are most welcome and highly anticipated!
Thanks OSXDaily.