Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Suppress standard output, capture error output

Tags:

bash

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.

like image 421
craigmiller160 Avatar asked Oct 30 '25 09:10

craigmiller160


1 Answers

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.

like image 133
anubhava Avatar answered Nov 01 '25 13:11

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!