array assignment syntax in zsh
(like in bash
which borrowed that syntax from zsh
) is:
array=( value1 value2
value3...)
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)}")
Or:
all_branches=( "${(@f)$(git branch -r)}" )
Or:
all_branches=(
"${(@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
test
$ =
test
While:
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 =
.
=
? – steeldriver Aug 12 '21 at 00:02