Skip to content

Instantly share code, notes, and snippets.

@YakDriver
Last active Feb 12, 2018
Embed
What would you like to do?
Bash Basics: Redirecting STDOUT and STDERR
#!/bin/bash
# Shows how STDOUT and STDERR can be redirected to /dev/null, which makes it go away, but could be redirected anywhere
#
# OUTPUT
#
# 1 - STDOUT redir, but not STDERR
# ./return_var.sh: line 13: thisisbad: command not found
# 2 - STDOUT redir, but not STDERR
# 3 - STDERR redir, but not STDOUT
# 4 - STDERR redir, but not STDOUT
# fred is silly
# 5 - redir both
# 6 - redir both
# 7 - redir STDERR to STDOUT
# 8 - redir STDERR to STDOUT
#
echo 1 - STDOUT redir, but not STDERR
thisisbad > /dev/null #produces STDERR
echo 2 - STDOUT redir, but not STDERR
echo "fred is silly" > /dev/null #produces STDOUT
echo 3 - STDERR redir, but not STDOUT
thisisbad 2> /dev/null #produces STDERR
echo 4 - STDERR redir, but not STDOUT
echo "fred is silly" 2> /dev/null #produces STDOUT
echo 5 - redir both
thisisbad &> /dev/null #produces STDERR
echo 6 - redir both
echo "fred is silly" &> /dev/null #produces STDOUT
echo 7 - redir both (alternate way)
thisisbad > /dev/null 2>&1 #STDERR
echo 8 - redir both (alternate way)
echo "fred is silly" > /dev/null 2>&1 #produces STDOUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment