Sharing PATH, Aliases, and Functions Between Bash and Zsh

When working with both Bash and Zsh, it can be incredibly helpful to maintain a consistent shell environment across both. Whether you’re scripting, switching between shells for different projects, or simply exploring alternatives, having the same aliases, PATH settings, and functions available in each shell saves time and reduces confusion. This guide explains a practical and portable approach for achieving this.

Before diving into the steps, you might also be interested in improving your command-line skills with articles like this trick to filter running processes with grep, or learning how to gather system hardware information via the CLI. These tools are made even more powerful when your shell environment is unified across platforms.

The goal is to create a shared configuration file that holds all portable shell customizations—such as aliases, functions, PATH, CDPATH, and even default editor variables—and source it in both your Bash and Zsh startup files. This method ensures DRY (Don’t Repeat Yourself) principles and makes managing your configuration much simpler.

Step 1: Create a Common Configuration File

Start by creating a common shell configuration file, typically placed in /etc for system-wide use. We’ll call this file /etc/commonrc.

sudo touch /etc/commonrc
sudo chmod 755 /etc/commonrc

The 755 permission ensures that the file is readable and executable by all users while being writable only by root. This step ensures that the settings stored are accessible to any user who sources the file.

Step 2: Export PATH and CDPATH

Adding directories to your PATH ensures that executables in those directories can be run without typing the full path. CDPATH works similarly for the cd command, allowing you to quickly change into frequently used directories.

echo 'export PATH="$PATH:/path/to/dir1:/path/to/dir2"' | sudo tee -a /etc/commonrc
echo 'export CDPATH="$CDPATH:/path/to/work_dir1:/path/to/work_dir2"' | sudo tee -a /etc/commonrc

Be sure to replace /path/to/... with actual directory paths relevant to your environment.

Step 3: Set the Default Editor

It’s good practice to set the EDITOR and VISUAL environment variables so that all command-line programs use your preferred text editor. For a more detailed approach, refer to this guide on changing the default text editor in Bash, Zsh, and Ksh.

echo 'export EDITOR=/usr/bin/nano' | sudo tee -a /etc/commonrc
echo 'export VISUAL=/usr/bin/nano' | sudo tee -a /etc/commonrc

Step 4: Add Useful Aliases and Functions

Aliases allow for shorthand commands, and shell functions can simplify complex workflows.

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

This set of examples includes:

  • clr: clears the terminal and lists the contents.
  • test: simple echo alias for testing the setup.
  • firefox: runs Firefox in the background, silencing output.
  • mkcd: creates a directory and immediately navigates into it.

Step 5: Source the Configuration in .bashrc and .zshrc

To make these settings active in both shells, source /etc/commonrc from your .bashrc and .zshrc files:

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

You can test if the configuration is being applied correctly by launching each shell and running one of your test aliases or functions:

bash
test  # Should output: it works!!!

zsh
test  # Should also output: it works!!!

Conclusion

By centralizing shell configuration in a single file and sourcing it across different shell environments, you gain a consistent experience while minimizing redundancy. This setup is especially helpful if you’re juggling multiple shells or working on diverse systems.

Keep in mind that not every shell feature is perfectly portable, but for most day-to-day needs—like aliases, prompt customization, and path management—this approach works very well. For more advanced shell tips, explore these bash one-liners or learn how to change the default internet browser in Ubuntu and Debian.

With these strategies, you’ll keep your shell setup tidy and efficient, no matter which shell you’re using.

1 Comment

Leave a Reply

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