3

If I do:

$(blah) bleh

I get

zsh: command not found: blah
zsh: command not found: bleh

This has me a little worried, because it means that zsh is executing bleh which was never intended to be a command; what if it happens to be executable and does something I didn't want?

Is there a way to make the whole line fail the moment blah fails?

Owen
  • 38,836
  • 14
  • 95
  • 125

2 Answers2

2

The reason bleh is tried is, that $(blah) gets replaced by an empty string if blah does not exists. In which case bleh will be the first word on the completely parsed command line and thus used interpreted as command.

So you have to catch the empty string and prevent further execution. Thankfully ZSH has a Parameter Expansion, namely ${name:?word} that can be used just for that case:

${$(blah):?No such thing} bleh

If blah does not exist, ZSH will print No such thing and return to the prompt (or exit the shell, if it is non-interactive):

zsh: : No such thing

else it will just the substituted command

% alias blah="echo echo '>>>'"
% ${$(blah):?No such thing} bleh
>>> bleh
Adaephon
  • 16,929
  • 1
  • 54
  • 71
0

Using && to separate commands will terminate on failure:

$(blah) && bleh

anthony
  • 40,424
  • 5
  • 55
  • 128
  • I'm actually not looking to separate the commands, but to execute it as a single command. Like for example `$(echo cat) /dev/null`. – Owen Feb 17 '14 at 19:19