Reputation: 1420
I'm trying to capture the error from pg_dump
and cannot figure out how to get it into a bash variable. This does not work because STDOUT is going to gzip.
OUTPUT=$(/bin/pg_dump -c --if-exists --dbname=cfMaster -U cfMaster | /bin/gzip > ~cftvdun/dbbackups/cfMaster.tmp.sql.gz)
How can I get STDERR into a bash variable in this situation?
Upvotes: 1
Views: 200
Reputation: 125788
The $( )
construct always captures stdout, but you can juggle between file descriptors. Just group the pipeline in { }
, and then redirect the stderr of the group to standard output with 2>&1
:
output=$( { /bin/pg_dump -c --if-exists --dbname=cfMaster -U cfMaster | /bin/gzip > ~cftvdun/dbbackups/cfMaster.tmp.sql.gz; } 2>&1 )
If you also wanted the standard output (rather than just sending it to a file), it'd get more complicated. I think in that case you'd have to juggle through FD #3.
BTW, I'd also recommend using lowercase (or mixed-case) variable names to avoid accidental conflicts with the variables with special meaning to the shell or other utilities.
Upvotes: 2