I use z shell via "oh my zsh". I'd like to source multiple alias files from within my .zshrc file so I can keep things well organized. I've prefixed the alias files with .alias_
so I can wildcard load them. But, a call to source ~/.alias_*
only loads the first file. How can I script it to source multiple files?
Filename examples: .alias_git
, .alias_local
, .alias_server
...
You could use a for loop:
for file in ~/.alias_*; do
source "$file"
done
The other option is to build an array of all the files you want to source and then iterate over the array using a for loop.
typeset -a aliases
aliases+="~/.alias_foo"
aliases+="~/.aliases_bar"
# etc...
for file in $aliases[@]; do
if [[ -a "$file" ]]; then
source "$file"
fi
done
This can actually be quite effective in making a well organized zshrc setup.
An even more ZSH-specific way (requires setopt EXTENDED_GLOB
):
alias_src=(
.alias_git
.alias_local
.alias_server
)
for f ($^alias_src(.N)) source $f
unset alias_src
The prefix ^
has the effect of applying the subsequent glob modifiers to every element of the array separately.
The trailing (.N)
has the following effect:
.
: only match filesN
: temporarily setopt NULL_GLOB
, which means that only those elements of the array that actually exist remain in the arrayIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With