Skip to content

zsh: support omitted parameter name in ${:-word} expansions #1280

Closed

Description

@LangLangBart

Zsh allows the parameter name to be omitted in expansions:

This is commonly combined with the (%) prompt expansion flag, which enables
prompt escape expansion, see man zshmisc for details.

./shfmt -ln zsh <<<'echo ${:-word}'
# <standard input>:1:8: invalid parameter name

# Get name of script, sourced file, or function currently being executed (%N)
./shfmt -ln zsh <<<'echo ${(%):-%N}'
# <standard input>:1:11: invalid parameter name

# Get absolute path of the file containing the source code currently being executed (%x)
./shfmt -ln zsh <<<'echo ${${(%):-%x}:a}'
# <standard input>:1:13: invalid parameter name

# Powerlevel10k instant prompt (very common in the wild)
./shfmt -ln zsh <<<'source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"'
# <standard input>:1:66: invalid parameter name

zsh docs

${name-word}
${name:-word}

If name is set, or in the second form is non-null, then substitute its
value; otherwise substitute word. In the second form name may be
omitted, in which case word is always substituted.

Source: https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion

additional

zsh allows omitting the parameter name almost everywhere for its expansion
syntaxes, but the ${:-word} syntax seems the most useful to support

Form Result (✅/❌ exit staus)
${:-word} ✅ Substitutes word
${:+word} ✅ Substitutes nothing
${:?word} ❌ zsh: : word
${:=word} ❌ zsh: not an identifier:
${::=word} ❌ zsh: not an identifier:
${:#pat} ✅ Operates on empty string
${##pat} ✅ Operates on empty string
${%%pat} ✅ Operates on empty string
${/p/r} ✅ Operates on empty string

Related

a couple users mentioned the issue in #120

EDIT1: typo

Activity

  1. added a commit that references this issue on Mar 22, 2026
    87df0f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址