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?
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 tolocal
in e.g.bash
, where it behaves the same. – Kusalananda Aug 14 '20 at 13:48bash
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