Bash - redirect command errors to file

0 points
Created by:

This article will show you how to redirect command errors to file in Bash.

Quick solution:

command 2> errors.txt


If you want data to be added to an existing file use >> instead of >.


Practical examples

In the example below, we will try to execute a wrong command that bash will not recognize - normally the result would be the message bash: sample_invalid_command: command not found.


$ abcd 2> errors.txt

After executing the above line, no message will be displayed, but the contents of the errors.txt file will looks like this:

bash: abcd: command not found
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.


Bash - redirect command errors to file
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂