3

So I'm running commands in a bash shell script, and I want to ONLY capture error output in a variable. Any standard output I want suppressed.

So far what I have definitely isn't working. So I'm trying to come up with a better idea, and struggling to find an answer.

Here's a code sample:

ERROR=$(svn switch "$NEW_URL" --accept postpone 1>/dev/null 2>&1) &

Looks like everything is getting suppressed. Any help would be appreciated. Thanks.

craigmiller160
  • 5,751
  • 9
  • 41
  • 75
  • 2
    This is a duplicate of [this question](https://stackoverflow.com/questions/962255/how-to-store-standard-error-in-a-variable-in-a-bash-script) – Matt C Apr 07 '16 at 22:38

1 Answers1

2

It should be like that:

error=$({ echo "stdout"; echo "stderr" >&2; } 2>&1 >/dev/null)
echo "$error"
stderr

i.e. redirect stderr->stdout first and then redirect stdout to /dev/null to suppress stdout.

For your command it should be:

error=$(svn switch "$NEW_URL" --accept postpone 2>&1 >/dev/null)

You also need to remove & to avoid pushing your command in background.

anubhava
  • 761,203
  • 64
  • 569
  • 643