junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
61.81k stars 2.35k forks source link

chore(shell): Separate declaration and assignment for zsh legacy versions #3856

Closed LangLangBart closed 3 weeks ago

LangLangBart commented 3 weeks ago

description

Close #3841

zsh versions prior to 5.1 deal with assignments differently on builtins like declare, export, local, readonly and typeset

❯ ~/.local/zsh-test/bin/zsh-5.0.2 -fxvc 'local scalar=$(echo one word)'
+zsh:1> echo one word
+zsh:1> local 'scalar=one' word

❯ ~/.local/zsh-test/bin/zsh-5.1 -fxvc 'local scalar=$(echo one word)'
local scalar=$(echo one word)
+zsh:1> echo one word
+zsh:1> local scalar='one word'
❯ ~/.local/zsh-test/bin/zsh-5.0.2 -fxvc 'local -a array=(several words)'
zsh:1: missing end of string

❯ ~/.local/zsh-test/bin/zsh-5.1 -fxvc 'local -a array=(several words)'
local -a array=(several words)
+zsh:1> local -a array=( several words )

To ensure compatibility with older zsh versions, split assignment and declaration when using multiple values or arrays.

❯ ~/.local/zsh-test/bin/zsh-5.0.2 -fxvc 'local scalar;scalar=$(echo one word)'
+zsh:1> local scalar
+zsh:1> scalar=+zsh:1> echo one word
+zsh:1> scalar='one word'
❯ ~/.local/zsh-test/bin/zsh-5.0.2 -fxvc 'local -a array;array=(several words)'
+zsh:1> local -a array
+zsh:1> array=( several words )

Lines like local ret=$? should be fine.

❯ ~/.local/zsh-test/bin/zsh-5.0.2 -fxvc '$RANDOM; local ret=$?'
+zsh:1> 29160
zsh:1: command not found: 29160
+zsh:1> local 'ret=127'
junegunn commented 3 weeks ago

Thanks a lot! I really appreciate your help.

LangLangBart commented 3 weeks ago

Thanks a lot! I really appreciate your help.

Thank you, glad to hear. fzf is a useful tool, and helping out with some issues saves you time to focus on other things.