Include additional files in .bashrc

I have stuff that I want to execute in .bashrc that I would prefer to exist in another file on the system. How to include this file in .bashrc?

+45
command-line linux bash shell
Feb 10 2018-11-11T00:
source share
4 answers

Add source /whatever/file (or . /whatever/file ) to .bashrc where you want to include another file.

+78
Feb 10 2018-11-11T00:
source share

To prevent errors, you need to check first to make sure the file exists. Then specify the file. Do something like that.

 # include .bashrc if it exists if [ -f $HOME/.bashrc_aliases ]; then . $HOME/.bashrc_aliases fi 
+26
Aug 20 '15 at 0:19
source share

If you have several files that you want to download that may or may not exist, you can keep it somewhat elegant using the for loop.

 files=(somefile1 somefile2) path="$HOME/path/to/dir/containing/files/" for file in ${files[@]} do file_to_load=$path$file if [ -f "$file_to_load" ]; then . $file_to_load echo "loaded $file_to_load" fi done 

The result will look like this:

 $ . ~/.bashrc loaded $HOME/path/to/dir/containing/files/somefile1 loaded $HOME/path/to/dir/containing/files/somefile2 
+2
Jan 11 '17 at 18:26
source share

I prefer to check the version first and assign a variable to the config path:

 if [ -n "${BASH_VERSION}" ]; then filepath="${HOME}/ls_colors/monokai.sh" if [ -f "$filepath" ]; then source "$filepath" fi fi 
0
Jul 04 '17 at 14:20
source share



All Articles