Last active
March 14, 2017 19:42
-
-
Save jhyland87/c451c78c145d2fc4d362aae508610e72 to your computer and use it in GitHub Desktop.
Bash function echo2 - used to redirect STDOUT to STDERR, or to send text to STDERR directly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function echo2 { | |
if test -p /dev/stdin | |
then | |
echo "$(</dev/stdin)" 1>&2 | |
elif test -n $1 | |
then | |
echo "$@" 1>&2 | |
else | |
return 1 | |
fi | |
} | |
# EXAMPLE USAGES | |
# 1) Pipe STDOUT to the echo2 function, which redirects to STDERR | |
# $ echo "Cant see me in STDOUT" | echo2 | grep should-grep-nothing-but-show-stderr-data | |
# Cant see me in STDOUT | |
# 2) Use echo2 just as you would echo | |
# $ echo2 "Cant see me in STDOUT" | grep should-grep-nothing-but-show-stderr-data | |
# Cant see me in STDOUT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment