← Blog · · 3 min read
zsh: ${AUTH:+-H '$AUTH'} is one word, so the token lands in Accept
On this page (5 sections)
In zsh 5.9, curl ${AUTH:+-H "$AUTH"} URL passed curl 1 argument, not 2, and every gated request from our download script failed. curl sent the token on a header line that started with a space; over HTTP/2 it landed inside accept.
TL;DR In zsh, don't build optional curl flags with
${VAR:+-H "$VAR"}. Put them in an array and pass"${hdr[@]}". We tested this with zsh 5.9 and curl 8.7.1 on macOS 27.0.1.
What happened
Our zsh download script adds a Hugging Face token header only when a token is set, using the bash idiom ${AUTH:+-H "$AUTH"}. In zsh that expansion stayed one word, and every gated request failed.
Reproduce it
The repro uses a fake token. wire sends the request with --http1.1 and keeps only our request lines:
AUTH="Authorization: Bearer hf_FAKE_PLACEHOLDER"
URL="https://huggingface.co/api/whoami-v2"
show_argv() { local i=0; for a in "$@"; do print -r -- " argv[$((++i))] = <$a>"; done }
wire() {
curl -s -o /dev/null --http1.1 --trace-ascii - "$@" "$URL" \
| grep -E '^[0-9a-f]{4}: ' | grep -vi 'proxy' \
| grep -E 'GET |Host:|User-Agent:|Accept:|Authorization' | sed -n '1,8p'
}
show_argv ${AUTH:+-H "$AUTH"}
wire ${AUTH:+-H "$AUTH"}
Output (we removed the two proxy CONNECT lines from the trace):
argv[1] = <-H Authorization: Bearer hf_FAKE_PLACEHOLDER>
0000: GET /api/whoami-v2 HTTP/1.1
001d: Host: huggingface.co
0033: User-Agent: curl/8.7.1
004b: Accept: */*
0058: Authorization: Bearer hf_FAKE_PLACEHOLDER
Over HTTP/2, curl's default here, the token ends up inside the accept header:
$ zsh -c 'AUTH="Authorization: Bearer hf_FAKE_PLACEHOLDER"; curl -s -o /dev/null --trace-ascii - ${AUTH:+-H "$AUTH"} https://huggingface.co/api/whoami-v2' | grep "HTTP/2\] \[1\] \[accept"
== Info: [HTTP/2] [1] [accept: */* Authorization: Bearer hf_FAKE_PLACEHOLDER]
Why
Bash splits the result of an unquoted expansion into words; zsh doesn't, so curl gets the single argument -H Authorization: …. curl treats the text after -H as the value, including the leading space visible at 0058: Authorization.
In HTTP/1.1, a header line that starts with whitespace is an obsolete continuation of the previous header. Hypothesis: the server reads the token as part of Accept; we did not capture its response or status code. Over HTTP/2, curl does the folding itself, as the info line shows.
The fix
hdr=()
[[ -n $AUTH ]] && hdr=(-H "$AUTH")
show_argv "${hdr[@]}"
wire "${hdr[@]}"
AUTH=""
hdr=()
[[ -n $AUTH ]] && hdr=(-H "$AUTH")
print " ${#hdr[@]} extra args"
Output (trace trimmed to the request line, Accept and Authorization):
argv[1] = <-H>
argv[2] = <Authorization: Bearer hf_FAKE_PLACEHOLDER>
0000: GET /api/whoami-v2 HTTP/1.1
004b: Accept: */*
0058: Authorization: Bearer hf_FAKE_PLACEHOLDER
0 extra args
show_argv ${=${AUTH:+-H $AUTH}} looks like a fix, but it also splits the spaces inside the header and prints 4 arguments.
Takeaways
- In zsh scripts, build optional flags as an array and pass
"${arr[@]}". When the variable is empty, the array adds no argument. - Don't use
${=...}to port the bash idiom, because it also breaks up the header value. - If an auth header seems ignored, check
--trace-ascii -. A header line that starts with a space is the sign of this bug.
Applies to: zsh 5.9, GNU bash 3.2.57 and curl 8.7.1 on macOS 27.0.1. We did not test other zsh versions, zsh with word-splitting options changed, or other curl builds.
Which other bash idioms have broken for you when a script moved to zsh?
How this was made: a short note drafted by an AI agent (Claude) from our own engineering logs; numbers are checked against those logs before publishing.