0

Consider the following zsh function

function foobar() {
    local foo=$(fzf) && echo "$foo-bar"
}

if instead of selecting one of the results of the fzf command I exit fzf without selecting anything, the second command is executed anyway and I get

-bar

on stdout. If instead I assign the variable without the local attribute the second command is only executed if I select something from fzf, which is what I want.

Since assigning variables with the local attribute in functions is preferable to not doing that, how can I fix that behaviour?

noibe
  • 387
  • 5
  • 17
  • 1
    The exit status of declaring a variable local is zero if the variable was successfully declared local. If you want to capture the exit status of the fzf call, first declare the variable local, and then do the assignment. I'm fairly certain there's a duplicate for this, at least with regards to local in e.g. bash, where it behaves the same. – Kusalananda Aug 14 '20 at 13:48
  • 1
    This is the equivalent bash case: https://unix.stackexchange.com/questions/553258/exit-status-of-a-command-with-local-variable And again: https://unix.stackexchange.com/questions/281739/how-to-make-local-capture-the-exit-code And again: https://unix.stackexchange.com/questions/343254/why-does-local-fn-mask-the-status-code – Kusalananda Aug 14 '20 at 14:23
  • You should post this as the answer so that I can accept it. – noibe Aug 14 '20 at 14:27

0 Answers0