0

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 . with source, even though they're aliases.

What am I doing wrong?

  • What you're doing wrong is quoting the `~` – Charles Duffy Jan 14 '22 at 13:17
  • 2
    Make it `bash_file="$HOME/.bash_$module"` or `bash_file=~/.bash_"$module"` and you'll be fine. – Charles Duffy Jan 14 '22 at 13:17
  • 1
    Also, you should always quote `"${array[@]}"` -- when it's expanded unquoted you have all the same bugs/limitations as `${array[*]}` – Charles Duffy Jan 14 '22 at 13:18
  • 1
    When I write shell scripts I always check them with [shellcheck](https://www.shellcheck.net/), it has considerable amount of rules that is checks and thorough description which allows to learn something. – Monsieur Merso Jan 14 '22 at 13:23
  • 1
    As an aside, quote the `${modules[@]}`: `"${modules[@]}"`. It doesn't cause an issue for this case since your array's elements don't contain whitespace or glob characters but that is a good habit to have. – M. Nejat Aydin Jan 14 '22 at 13:23
  • Thanks all! @CharlesDuffy that did the trick! Feel free to post as an answer and I'll accept. (Edit: nevermind, looks like it was closed as a duplicate. Apologies, I was having trouble finding similar answers here.) – Aleksandr Hovhannisyan Jan 14 '22 at 13:25
  • 1
    The question is asked-and-answered, so I closed it as duplicate rather than adding an answer, as called for by the _Answer Well-Asked Questions_ section of [How to Answer](https://stackoverflow.com/help/how-to-answer). (Those rules specifically refer to questions "asked and answered many times before" -- I've edited the duplicate list to make it clear there's more than one preexisting instance). – Charles Duffy Jan 14 '22 at 13:26
  • @MonsieurMerso Thanks! I'll use that in the future. – Aleksandr Hovhannisyan Jan 14 '22 at 13:26

0 Answers0