In my bash profile, I currently have separate if statements to check if a file exists, and if it does, to execute it and load the corresponding variables into the shell environment:
# Aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Functions
if [ -f ~/.bash_functions ]; then
. ~/.bash_functions
fi
# Environment variables
if [ -f ~/.bash_variables ]; then
. ~/.bash_variables
fi
This currently works just fine because the following files do indeed exist:
~/.bash_aliases~/.bash_functions~/.bash_variables
However, since all of these conditionals have shared logic, I'd like to do this instead:
modules=(aliases functions variables)
for module in ${modules[@]}; do
bash_file="~/.bash_$module"
echo "Loading $bash_file"
if [ -f "$bash_file" ]; then
. "$bash_file"
fi
done
Unfortunately, this doesn't work.
When launching a new shell, I can confirm the following logs:
Loading ~/.bash_aliases
Loading ~/.bash_functions
Loading ~/.bash_variables
But for whatever reason, it seems that the file-checking logic is evaluating to false because none of my aliases/functions/variables load.
I've also tried:
- Using
test -f "$bash_file". - Removing quotes from around
$bash_file. - Quoting the elements of the array, even though this isn't necessary as far as I know.
- Replacing
.withsource, even though they're aliases.
What am I doing wrong?