Created
May 9, 2024 17:11
-
-
Save excalq/0168c565e6d06ed771993a7f12348f3a to your computer and use it in GitHub Desktop.
Bash stdout, stderr Output Redirection
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 | |
set +e # Don't exit on error | |
succeed () { | |
echo " ✅ Doing stuff successfully" | |
} | |
fail () { | |
echo " ❌ This is an error" >&2 | |
return 1 | |
} | |
echo "Bash Version:" $BASH_VERSION | |
echo "This should display both stdout and stderr:" | |
{ | |
succeed | |
fail | |
} | |
echo "This should display stderr only:" | |
{ | |
succeed | |
fail | |
} >/dev/null | |
echo "This should display stdout only:" | |
{ | |
succeed | |
fail | |
} 2>/dev/null | |
echo "This should be silent:" | |
{ | |
succeed | |
fail | |
} &>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment