Last active
February 12, 2018 15:42
-
-
Save YakDriver/fc94b6766d73824e87a4288876848b21 to your computer and use it in GitHub Desktop.
Bash Basics: Redirecting STDOUT and STDERR
This file contains 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
#!/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