Redirecting Stderr and Stdout to a file in Bash
In this article, we will discuss standard output and standard error in Linux. We will see how to redirect standard output and standard error in Bash.
Let us start by understanding the terms standard output and standard error in Linux.
Standard output and standard error in Linux
In Linux, commands take some input from the user which can be a file or any attribute. When we execute these commands, they give us some output of our input which is called standard output.
This standard output can be either success or error. If the output we get is an error, we call it standard error.
Both will be displayed on our terminal screen. Sometimes, we want to store standard output and standard error in files for testing or debugging code.
In Linux, we can redirect both the output into a file, the process of storing it is called redirection.
In Linux, for every process we have three data streams, standard input (stdin), standard output (stdout) and standard error (stderr).
Standard input (stdin) takes input from the user through the keyboard. Standard output (stdout) displays output on the terminal screen.
Standard error (stderr) displays error messages on the screen. By default, both standard output and standard error are printed on the terminal screen.
In Linux, each data stream has a unique numeric ID:
- For standard input (stdin), the numeric ID is 0.
- For standard output (stdout), the numeric id is 1.
- For standard error (stderr), the numeric ID is 2.
Let's explain the redirection of standard output and standard error in more detail.
Redirecting standard output to a file in Bash
In Linux, we can redirect the standard output to a file using numeric ID. To redirect the standard output of any command, we use the redirection operation 1 and the > symbol.
In our example, we will use the ls command. We will redirect the output of the ls command to a file.
We will then look at the text file to see the output of the ls command stored in the file.
Sample code:
$ ls 1> stdout.txt
To view the file, we use the following command:
$ cat stdout.txt
As shown in the above terminal screen, we have used ls 1> stdout.txt command in the terminal. This command stores the output of the ls command into a file named stdout.txt.
We use ls command in Linux to list all the files and directories. So we redirect the output of ls command into a file.
When we view the file using cat command, we can see that the standard output of ls command is stored in the file. This is how we can redirect the standard output of any command to a file using its numeric id.
Redirecting standard error to a file in Bash
We can also redirect standard error to a file using its numeric id. To redirect standard error to a file, we use its numeric id 2 with the redirection operator >.
We can do this using the following command in the terminal.
Sample code:
$ cat file.txt 2> stderr.txt
To view a file called stderr.txt, we use the following command:
$ cat stderr.txt
As we can see, we have used cat file.txt 2> stderr.txt command in the terminal. Since there is no file named file.txt in the current directory, the cat command will give the error which we have appended in the stderr.txt file.
When we view the stderr.txt file using the cat command, we can see that it displays the error No such file or directory. This means that we have redirected the standard error of the cat command to a file named stderr.txt.
This is how we redirect the standard error of any command to a file.
Redirecting standard output and standard error to files in Bash
We can also redirect both the outputs into a file using a single command. For this, we use the following command.
Sample code:
$ ls 1> stdout.txt 2> stderr.txt
To view the stdout.txt file, we use the following command:
$ cat stdout.txt
To view the stderr.txt file, we use the following command:
$ cat stderr.txt
As we can see, we redirected both standard output and standard error to files in a single command. The output of ls command will be stored in the file stdout.txt while stderr.txt fill will remain empty as no error will occur.
This is how we redirect both standard output and standard error to a file with one command.
We hope this Linux article helped you understand how to redirect standard output and standard error of any command to a file in Bash.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u