DEV Community

Cover image for Dotfiles - Shared Config for ZSH and Bash
Michael Currin
Michael Currin

Posted on

Dotfiles - Shared Config for ZSH and Bash

How to setup a common shell config, to avoid duplicating content in .bashrc and .zshrc

I use ZSH as the default shell on macOS and Bash on Linux, I have to maintain a both .zshrc and .bashrc files. Some code in those is specific to the OS, but most of my logic can go in a shared file.

File structure

So I created a .commonrc. That gets loaded by ZSH and Bash config files. And if I make a change to the common config, it gets applied in both shells.

Here is my setup:

  • .commonrc - common content.
  • ~.bashrc - Bash-only content.
  • ~.zshrc - ZSH-only content.

Common config

Here is my .commonrc file.

### Common RC ###

# Common content here...
Enter fullscreen mode Exit fullscreen mode

The content in there is covered in Part 2 of this post series.

Bash config

At the top of .bashrc:

### BASH RC ###

source ~/.commonrc

# Bash-specific content here.
# ...

# Shell prompt.
# username@hostname:path$
PS1="\[\e]0;\u@\h: \w\a\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "

# Remove ZSH default warning when using Bash on macOS.
export BASH_SILENCE_DEPRECATION_WARNING=1
Enter fullscreen mode Exit fullscreen mode

I don't have much original content to share - most of my .bashrc is what came with standard Linux setup.

ZSH config

Here is part of my .zshrc file:

### ZSH RC ###

IS_ZSH='true'
IS_BASH='false'

source ~/.commonrc

# ZSH-specific content
# ...
Enter fullscreen mode Exit fullscreen mode

ZSH prompt

Again, I've left most things there as the default, or you can comment values from the standard setup.

Here some ZSH-specific values around the prompt. I set this up so that prompt will be green if there are uncommitted changes. An exclamation mark ! is added is there are changed files as a question mark ? for new files which are untracked by git.

This prompt can save having to run git status all the time. Though you will have to press Enter with no content to get to a new shell line and so update the prompt.

ZSH_THEME_GIT_PROMPT_SUFFIX="%{$fg[blue]%})%{$reset_color%} "

ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[green]%}!"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[green]%}?"
ZSH_THEME_GIT_PROMPT_CLEAN=""
Enter fullscreen mode Exit fullscreen mode

Conclusion

I hope you found this post series useful and you can add some content to your your dotfiles to make your development smoother.

If you are interested, check out these links:

Image credit: @bill_oxfor on Unsplash.

Top comments (0)