Skip to content

Instantly share code, notes, and snippets.

@YakDriver
Last active February 12, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YakDriver/fc94b6766d73824e87a4288876848b21 to your computer and use it in GitHub Desktop.
Save YakDriver/fc94b6766d73824e87a4288876848b21 to your computer and use it in GitHub Desktop.
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