I have run into something interesting. As the title says, I am trying to redirect stderr to stdout and suppress/throw stdout. Before you flag this post as a duplicate (Shell: redirect stdout to /dev/null and stderr to stdout, or How to pipe stderr, and not stdout?, or IO Redirection - Swapping stdout and stderr) please give me a chance.
So, I want something like this:
#!/bin/bash
temp_func () {
GLOBALVAR="$($1 "$2" 2>&1 >/dev/null)"
}
temp_func "echo" "hello world"
echo "should be empty: $GLOBALVAR"
temp_func "ecsdfho" "hello world"
echo "should show err: $GLOBALVAR"
The above works, as expected. Very cool.
If I now instead do:
PUSH_RESULT="$(git push "$REMOTE" "$NEW_TAG" 2>&1 >/dev/null)"
$PUSH_RESULT
ends up being populated with an error when an error occurs (which is great) but also ends up containing git output upon a successful push. Why!?
NOTE: I was able to bypass this problem by doing something like:
PUSH_RESULT="$(git push -q "$REMOTE" "$NEW_TAG" 2>&1)"
but am curious why the initial method doesn't work.
Thank you.