I need to run some commands and if any of those fails I would like to output JSON with it's stderr.

I have the following line:

OUT="$( (ssh internal "custom_script1" | custom_app1 2>&- | custom_script2) 2>&1)"

If the pipeline is broken, every command will write onto its stderr and OUT will have 2 lines. How can I know which command causes the failure and grab stderr of that command?

Let's say that I am unable to ssh to internal, so I want to print SSH stderr only:

  "state": "failed",
  "reason": "Permission denied (publickey,password,keyboard-interactive)."

1 Answer 1


if i understood this correctly, just avoid the global 2>1.

$ OUT="$( (ssh internal "custom_script1" | cut -f 1 2>&- | cut -f 2 2>&1) )"
ssh: Could not resolve hostname internal: nodename nor servname provided, or not known
$ echo $OUT

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.