JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Redirecting Stderr and Stdout to a file in Bash

Author:JIYIK Last Updated:2025/03/23 Views:

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

Redirect stderr to a file

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

Redirect stdout and stderr with a single command

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.

Article URL:

Related Articles

Creating a Progress Bar in Bash

Publish Date:2025/03/23 Views:94 Category:OPERATING SYSTEM

A progress bar is a visual indicator that shows the progress of a task, such as a long-running script or command. It can be used to provide feedback to the user about the status of a task and can also help estimate the time remaining before

Deleting Duplicate Lines in Bash

Publish Date:2025/03/23 Views:123 Category:OPERATING SYSTEM

Duplicate entries can cause a variety of problems in Bash scripts, such as incorrect or inconsistent results, and they can also make the script difficult to maintain. Removing duplicate entries from a script is often necessary to avoid thes

Count unique lines in a file in Linux

Publish Date:2025/03/23 Views:190 Category:OPERATING SYSTEM

Counting unique lines in a file is a common task in Linux, and there are a number of different tools and methods that can be used to perform this operation. In general, the appropriate method depends on the specific requirements and constra

Counting files in a directory in Bash

Publish Date:2025/03/23 Views:178 Category:OPERATING SYSTEM

Counting how many files are in a directory is a common task in Bash, and there are a number of different tools and methods that can be used to perform this operation. In general, the appropriate method depends on the specific requirements a

Execute commands in a variable in a Bash script

Publish Date:2025/03/23 Views:111 Category:OPERATING SYSTEM

This article is about storing Bash commands in a variable and then executing it directly from that variable. First, we will discuss the various ways to execute commands contained in a variable, followed by several script examples. Let’s g

Bash variable multiplication

Publish Date:2025/03/23 Views:150 Category:OPERATING SYSTEM

This article explains how to multiply two variables in Bash. Multiplying variables in Bash Multiplying two variables is a simple operation in Bash. We can use the arithmetic operator * to multiply two numbers in Bash. In Bash, multiplicatio

Bash md5sum command

Publish Date:2025/03/23 Views:116 Category:OPERATING SYSTEM

This article explains how to use the md5sum command in Bash. Bash md5sum command md5sum command prints the 32 character and 128 bit checksum of a given file. This command converts the file into a hash using the MD5 algorithm; the syntax of

Sorting Arrays in Bash

Publish Date:2025/03/23 Views:73 Category:OPERATING SYSTEM

Sorting an array is a very common task in any programming language. In Bash scripting, we can also accomplish this task in two different ways. The first one uses any sorting algorithm and the second one uses a built-in keyword in Bash scrip

Multidimensional arrays in Bash

Publish Date:2025/03/23 Views:68 Category:OPERATING SYSTEM

Multidimensional array is a very important element for any program. It is mainly used to create table view of data and many other purposes. This article demonstrates how to create a two-dimensional array. In addition, we will discuss the to

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial