📝 Edit on GitHub
Piping and redirection
Resources
- Redirection guide.
- Pipes and Redirection guide.
Send stderr to stdout
Send any error output to the same place as stdout.
COMMAND 2> &1
This is not so useful in itself when just running in the console alone. But more useful when using crontab, tee
or writing to a file.
Send stderr and stdout to the same file
COMMAND > stdout_and_sterr.txt 2> &1
The &1
is a pointer to where stdout
is currently going.
Shorthand for the above.
COMMAND &> stdout_and_sterr.txt
Apparently supported in all shells.
From askubuntu.com question.
Send stderr and stdout to different files
COMMAND > stdout.txt 2> stderr.txt
Append output
Append stdout
to a file.
COMMAND >> stdout_and_sterr.txt
Also append stderr
to that file.
COMMAND >> stdout_and_sterr.txt 2> &1
From SO question
Hide all output from a command
COMMAND > /dev/null 2> &1
e.g.
if command -v node /dev/null > 2> &1; then
echo 'Node is installed!'
else
echo 'Node is not installed :('
fi