Search code examples
bashshort-circuiting

Prevent short-circuiting in Bash


I want to

  1. generate a list of matching files
  2. know if at least one of the patterns matched

The following will not work if there are any pdf files because then the first compgen will return true and the second compgen will not be executed.

{ compgen -G "*.pdf" || compgen -G "*.txt"; }

Is there a way to prevent short-circuiting?


Solution

  • Using ; would just return the result of the last compgen. I still need the logical or of all the compgens to know if there was any match.

    You can write logic to handle exit status yourself. A short version could look likethe following:

    { cmd1; ret=$?; cmd2; ! ((ret | $?)); }