Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Last active March 14, 2017 19:42
Show Gist options
  • Save jhyland87/c451c78c145d2fc4d362aae508610e72 to your computer and use it in GitHub Desktop.
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
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