Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have copied this code from somewhere and am trying to modify it for my needs. It was designed for bash but I am running it under zsh.

I always have trouble running bash scripts under zsh, as there seems to be quite a few differences between the two languages(?). Is anyone able to tell me what I need to modify here so that it works with zsh?

declare -a files=(
  "$HOME/.dotfiles/bash/exports" # Exports
  "$HOME/.dotfiles/bash/aliases" # Aliases
  "$HOME/.dotfiles/bash/functions" # Functions
  "$HOME/.z.sh" # z binary from https://github.com/rupa/z
  "$NVM_HOME/nvm.sh" # NVM

# If these files are readable, source them
for index in ${!files[*]}
  if [ -r ${files[$index]} ]; then
    source ${files[$index]}

unset files
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Here's a ~/.zshrc that sources a bunch of other files:

# Load the zsh files. 
# This is taken from Frank Terbeck's ZSH setup.
for rc in ~/.zshrc.d/???-*.z; do
    if [[ -r ${rc} ]] ; then
        zprintf 1 "zshrc: loading %s\n" "$rc"
        source "$rc"
        zprintf 0 "zshrc: could not load %s\n" "$rc"
unset rc

This'll source files in ~/.zshrc.d/ which have a filename beginning with a 3-digit number (for example, 421-Name.zsh).

The advantage of this method is that it allows you to have a single directory (~/.zshrc.d/) containing all your startup-scripts. You can then add to / remove from that directory without having to touch multiple files (you don't have to edit ~/.zshrc every time you add a script).

To change the filenames, edit this line:

for rc in ~/.zshrc.d/???-*.z; do

The directory it's searching is ~/.zshrd.d/. If you don't want a 3-digit prefix, remove the ???-. The extension (.z) can also be changed here.

Note: the ? doesn't actually match a digit, my mistake. From man zshexpn:

?      Matches any character.
share|improve this answer
This looks good! What if I didn't want to have the requirement of a 3-digit prefix? –  Oliver Joseph Ash Jan 6 '13 at 11:23
@OliverJosephAsh Added a comment to the answer. –  simont Jan 6 '13 at 23:08
add comment

I'm more familiar with bash than zsh, but this should work in either:

# zsh's declare (/typeset) command doesn't allow arrays to be assigned as
# they're declared. Actually, arrays don't really have to be declared, you can
# just create them by assigning an array value; but for completeness...
declare -a files
# Comments aren't recognized inside an array declaration, so leave them out

# If these files are readable, source them
# Note: rather than iterating over a list of array indexes and using them to
# pull out the array elements, I've switched this to iterating over the array
# elements directly. It's simpler, and works the same in both bash and zsh.
for file in "${files[@]}"
    if [ -r "$file" ]; then
        # Note that if any of the files have bashisms, they may break in zsh
        source "$file"

unset files
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.