I don't know how to title this question but anyway
a="$(file . * | grep -i pgp)" || echo "no such files found! " && exit \
&& b=$(echo "$a" | fzf --cycle --height=15) && [ -n "$b" ] \
&& gpg --import "$b"
I'm confused about this. When variable a does not exist prints echo "no such files found! " and exits but when does exist doesn't print the message and exits as well. How can I do to exit only when no value is passed to variable a, in other words, when variable a does not exist.
||and&&look at the exit status of the latest executed command. If thegrepin the assignment toareturns a falsy status, the||is fulfilled and theechoruns. It in turn almost certainly returns a truthy status, which&&looks for, so theexitruns. On the other hand, if thegrepreturns a truthy status, theechois skipped, but theexitruns. The combination of&&and||isn't the same asif-else, which I think you probably should use here. – ilkkachu May 20 '21 at 04:42ifstatement, I was just curious about this behaviour @ilkkachu – testoflow May 20 '21 at 04:55a=$(whatever) || { echo "error"; exit; } && echo success && some commands. The braces would group theechoandexit, and in the failing case, the shell would exit before the&&is checked. But justif a=$(whatever); then echo success; ...; else echo error; exit; fiwould still be clearer – ilkkachu May 20 '21 at 09:17