Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I want to cleanup my home folder and have managed to move out most of the configuration files to the .config folder. But, one file I'm not able to figure out is the .bashrc file. How can I specify bash to load .bashrc from a custom location?

share|improve this question

marked as duplicate by jasonwryan, cuonglm, Anthon, l0b0, slm Aug 29 at 12:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

The bash command accepts a --rcfile filename option that will:

Execute commands from filename (instead of ~/.bashrc) in an interactive shell.

So if you run bash --rcfile ~/.config/bashrc then you will load that file instead.

There isn't a way to configure it to load from there by default (where would you configure it?), other than recompiling it with modifications (change the definition of bashrc_file in shell.c - it's not a configuration option). You can create a wrapper that runs bash with the --rcfile option and passes all other arguments along unchanged - you'd have to add it to /etc/shells to be able to set that wrapper as your shell, and you'd have to be careful doing it. Either of those options is viable, but nontrivial to get right.

If you're on a single-user system, another option is to place all your configuration in the system-wide files in /etc, but that isn't really solving the problem. You could also have the system-wide file detect how it was run and re-execute bash with the --rcfile option automatically, but that seems even more fraught than the other approaches.


My recommendation would be to acclimatise yourself to having an extra dotfile in your home directory, but the above approaches are doable if you really can't.

share|improve this answer

With a login shell, typically, your ~/.bash_profile contains the line:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

So you can set your own config folder here:

BASHRC_CONFIG_DIR=~/config/bash

if [ -f "$BASHRC_CONFIG_DIR/.bashrc" ]; then
    . "$BASHRC_CONFIG_DIR/.bashrc"
fi

For non-login shell, you can set it in /etc/bash.bashrc.

share|improve this answer
    
This will only work for a login shell (which doesn't otherwise load .bashrc). If you open an interactive shell inside a session (xterm, gnome-terminal… or you simply call bash again) it will only read ~/.bashrc and in your case would fail to load ~/.config/bash –  Ángel Aug 29 at 8:22
    
@Ángel: Added it, forgot about that. –  cuonglm Aug 29 at 8:29

Inspired by Gnouc answer, I realized that bash reads /etc/bash.bashrc and ~/.bashrc in the same cases. Thus, you can put at the bottom of /etc/bash.bashrc:

 if [ -s "${XDG_CONFIG_HOME:-$HOME/.config}/bash/bashrc" ]; then
    . "${XDG_CONFIG_HOME:-$HOME/.config}/bash/bashrc"
 fi

and then move ~/.bashrc to $XDG_CONFIG_HOME/bash/bashrc

Bash will still attempt to load the ~/.bashrc which is not a problem since it no longer exists. And /etc/bash.bashrc will ensure that the file is loaded from your new location.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.