My .bashrc was getting a little long, so I decided to break it up into smaller files according to topic and then call these files from within .bashrc as so

#my long .bashrc file
bash .topic1rc
bash .topic2rc

but, within one of these sub-scripts, I had created a bash function. Unfortunately it is no longer available to me as it was before I broke everything into chunks. How do a break my .bashrc file into chunks, but retain access to the variables and functions that I create?

4 Answers 4


If I understand your question correctly, you need to source or . your files. For example, within your .bashrc and taking care of order (only you know):

source .topic1rc
source .topic2rc

source can be shortened to . on the command line for ease of use - it is exactly the same command. The effect of source is to effectively inline the script that is called.


What you are doing doesn't work because each bash .topic1rc invokes a new bash shell as a child process, and there's no way to communicate the environment, shell variables, or functions back to the parent process, the bash shell you're interested in.

What you want to do is use the . command (also available as the more verbose source — either one does the same thing). This reads the given script in the current shell rather than into a new shell. So:

#my long .bashrc file
. ~/.topic1rc
. ~/.topic2rc

(It probably works fine for almost all use to omit the path, since the main ~/.bashrc is usually executed when you log in and your home directory is your initial working directory, but it's much better practice to spell it out.)


As others have explained, you are running those scripts in a new shell that dies after they've run. You need to source your files with either

. .topic1rc


source .topic1rc

You can also create a slightly more advanced solution that won't require you to edit your .bashrc if you want to add more chunks in the future.

You can put all your configuration files under a directory, say ~/.bash.d, and then have this as the only contents of your .bashrc file:

if [[ -d ~/.bash.d ]]
    for conf_file in ~/.bash.d/*
        if [[ -f $conf_file ]]
            source "$conf_file"

This way all your bash config files under .bash.d are sourced and you can control the order in which this happens since they will be sourced in alphabetical order of the file name.

  • You can also make that a function and have several configuration directories, that is what I am currently doing, have a look at my bashrc at github.com/jdevera/dotfiles/blob/master/bashrc Feb 22, 2011 at 9:05
  • If you're going to quote the conf_file variable in your source command, it's probably good practice to quote it in your [[ pseudo-command, too. This apparently isn't actually required, since bash seems to expand variables differently inside of it than out, but it would help prevent an accidental removal of them in other places... To wit: compare: foo='test this thing'; touch $foo; [[ -f $foo ]] && echo true || echo false versus foo='test this thing'; touch "$foo"; [[ -f $foo ]] && echo true || echo false (run these from an empty directory for improved sanity)
    – lindes
    Feb 22, 2011 at 10:21
  • I always quote outside [[ but never inside, since there is not splitting or glob expansion inside [[. mywiki.wooledge.org/BashFAQ/031 Feb 23, 2011 at 15:30

Note that when you use the source or . commands, by default in bash, the file argument will be searched for in the $PATH unless it contains a / character. You want to either

source ./.topic1rc 


shopt -u sourcepath
source .topic1rc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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