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.

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?

share|improve this question
add comment

4 Answers 4

up vote 9 down vote accepted

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.

share|improve this answer
    
That did it, thanks! –  John Berryman Feb 22 '11 at 14:44
add comment

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.)

share|improve this answer
add comment

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

or

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 ]]
then
    for conf_file in ~/.bash.d/*
    do
        if [[ -f $conf_file ]]
        then
            source "$conf_file"
        fi
    done
fi

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.

share|improve this answer
    
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 –  Jacobo de Vera Feb 22 '11 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 '11 at 10:21
    
I always quote outside [[ but never inside, since there is not splitting or glob expansion inside [[. mywiki.wooledge.org/BashFAQ/031 –  Jacobo de Vera Feb 23 '11 at 15:30
add comment

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 

or

shopt -u sourcepath
source .topic1rc
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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