Bash – How to make functions created in a bash script persist like those in .bashrc

bashfunctionscripting

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?

Best Answer

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.