Share PATH, aliases and functions between Bash and Zsh

Sharing the aliases, customized PS1 (prompt), functions and PATH/CDPATH between the zsh and bash shells is very usefull. To my mind, the cleanest way to do this is to create a new file, having the portable shell settings stored there and sourced in the .*rc shell configuration files:

I will create the /etc/commonrc file and put all the portable shell settings there, and source it in ~/.bashrc and ~/.zshrc.

Create the /etc/commonrc file and set the permissions:

# touch /etc/commonrc
# chmod 755 /etc/commonrc

Set the PATH and CDPATH:

# echo 'export PATH=$PATH:/path/to/dir1/:/path/to/dir2/' > /etc/commonrc
# echo 'export CDPATH=$CDPATH:/path/to/work_dir1:/path/to/work_dir2/' >> /etc/commonrc

Set the default editor:

# echo 'export EDITOR=/path/to/new/editor' >> /etc/commonrc
# echo 'export VISUAL=/path/to/new/editor' >> /etc/commonrc

Add the aliases and functions:

# echo 'alias clr='clear ; ls'' >> /etc/commonrc
# echo "alias test='echo \"it works!!!\"'" >> /etc/commonrc
# echo "alias firefox='nohup firefox &>/dev/null'" >> /etc/commonrc
# echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> /etc/commonrc

Source the /etc/commonrc file in the ~/.*rc files:

$ echo '/etc/commonrc' >> ~/.bashrc
$ echo 'source /etc/commonrc' >> ~/.zshrc

Testing if the portability works:

$ bash
$ test
it works!!!
$ zsh
naboo% test
it works!!!

Sharing the prompt, aliases, functions, PATH and CDPATH between zsh and bash is possible because these shells are sh compatible.

Tagged with: , , , , , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *




Subscribe to get the latest Linux news and how to guides directly on your e-mail!