In my current project, I am trying to take the output from git branch -r to an array in zsh, as follows:

% all_branches = ("${(@f)$(git branch -r)}")

However, when I run this command, I get the following error message on the terminal:

zsh: bad pattern: (  origin/Backplane-Broken-Links

I'm inferring that zsh is taking issue with the forward slash in the branch name. Is there a way I can process the forward slashes (as well as other special characters, if I run into any others) when sending the output from git into my array?

  • 2
    Are you sure the issue isn't the whitespace around the assignment operator ` = `? Aug 12 at 0:02

array assignment syntax in zsh (like in bash which borrowed that syntax from zsh) is:

array=( value1 value2

There can't be whitespace on either side of = (though there can be (space, tab or newline) inside the (...) which are used to delimit the words as usual, and those words making up the array members).

So you want:

all_branches=("${(@f)$(git branch -r)}")


all_branches=( "${(@f)$(git branch -r)}" )


  "${(@f)$(git branch -r)}"

Whichever style you prefer.

all_branches=(${(f)"$(git branch -r)"})

Would also work. Placing the whole expansion in "..." combined with the @ flag is when you want to preserve empty elements. I don't think git branch names can be empty, and note that anyway $(...) strips all trailing newline characters, so empty branches would be removed anyway if they were at the end of the output. We still need $(...) to be quoted though to prevent $IFS-splitting.

Incidentally, array = () would be the start of the definition of both the array and = function:

$ array = ()
function> echo test
$ array
$ =


array = (foo|bar)

Would be running the array command (if found) with = and the files that match the (foo|bar) glob pattern (if any) as argument.

But here, your ("${(@f)$(git branch -r)}") doesn't form a valid glob pattern, nothing to do with slashes.

For completeness,

array = ( value1 value2 )

is valid syntax in the rc shell (the shell of Research Unix V10 and plan9). In that shell, you can't do echo = which gives you a syntax error. You need to quote the =.

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