New ZSH user here writing his .zshrc file, which uses GNU find and a for loop to source shell scripts in ~/.aliases that set up my shell aliases. I just spent over an hour on the internet and trying various things wondering why this code worked in BASH, but not in ZSH:

$ for aliases in $(find ~/.aliases -maxdepth 1 -name "*_aliases"); source $aliases

aliases: attempt to set slice of associative array

Over an hour later, I happened to re-write that code with a different variable name:

$ for files in $(find ~/.aliases -maxdepth 1 -name "*_aliases"); source $files

and it worked.

How is ZSH treating this code differently than BASH and why? Are there any advantages?


This gives us the clue what happens:

aliases: attempt to set slice of associative array

The parameter aliases is used by zsh to store the alias which are defined. It's defined as an associative array and you cannot store a scalar into it.

Use e.g. this code to display the content of associative arrays nicely formatted:

for k in "${(@k)aliases}"; do
  echo "$k -> $aliases[$k]"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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