I'm running the following command in bash:

yarn config get workspaces-experimental

Which returns the following:


That is, "true" or "false" followed by two end lines. I want to take this process, verify its standard output and pipe it into another process that returns an exit code when the result is not "true".

How can I do this? Use the vertical bar (|)?

  • true as in literal string or you are taking about exit codes/ – Inian Feb 12 '20 at 13:13
  • I think a literal string. In the terminal, what gets printed is the word "true" followed by two end lines. – Paul Razvan Berg Feb 12 '20 at 13:15
  • 1
    Did you check the actual return code? You might be making things more complicated than needed. – Panki Feb 12 '20 at 13:17
  • 2
    @PaulRazvanBerg "returns" can be ambiguous - are you trying to test the standard output of the command (i.e. what gets printed in a terminal) or its exit status (what would be printed by echo $? after the command has run)? Usually it's simpler and more robust to test the latter – steeldriver Feb 12 '20 at 13:23
  • 1
    @roaima echo $? returns "0" in both cases. Here's a screen capture for you. – Paul Razvan Berg Feb 12 '20 at 13:41

With bash, this will generate an exit status code of 0 (success) only when the text literal true is matched. In other cases it will return non-zero (failure)

[[ $(yarn config get workspaces-experimental) == true ]]

You can use this directly in a conditional statement, for example

if [[ $(yarn config get workspaces-experimental) == true ]]
    echo "Relax, everything is fine"

It's possible to pipe to xargs and use test:

yarn config get workspaces-experimental | xargs test true =

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.