Skip to content

Instantly share code, notes, and snippets.

@excalq
Created May 9, 2024 17:11
Show Gist options
  • Save excalq/0168c565e6d06ed771993a7f12348f3a to your computer and use it in GitHub Desktop.
Save excalq/0168c565e6d06ed771993a7f12348f3a to your computer and use it in GitHub Desktop.
Bash stdout, stderr Output Redirection
#!/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