How to Use the Stdin, Stderr, and Stdout Streams in Bash

Three streams are opened when the Linux operating system starts. These are stdin, stdout, and stderr. The full form of stdin is standard input which is used to take an input from the user. The full form of stdout is the standard output which is used to store the output of the command into the stdout stream. The full form of stderr is the standard error which is used to store the error message that is generated from any command into the data stream. The uses of these streams are shown in this tutorial. The corresponding numerical identifier values of stdin, stdout, and stderr are 0, 1, and 2.

Redirection Operators of Stdin, Stdout, and Stderr

    • The “<” or “0<” is used for the stdin stream.
    • The “>” or “1>” is used for the stdout stream.
    • The “2” is used for the stderr stream.

Uses of Stdin, Stdout, and Stderr

The uses of stdin, stdout, and stderr are shown in this part of the tutorial using multiple examples.

Example 1: Use of Stdin

The method of taking the content of a file and printing it in the terminal is shown in this example.

Run the following “cat” command to create a text file named testdata.txt with some content:

$ cat > testdata.txt

 
Run the following “cat” command to append some content into the testdata.txt file:

$ cat >> testdata.txt

 
Run the following “cat” command to take an input from the testdata.txt file and print it into the terminal:

$ cat < testdata.txt

 
Output:

The following output appears after executing the previous commands after adding the string, “linuxhint.com”, and “Scripting Language” into the testdata.txt file:


Example 2: Use of Stdout

The method of creating a file using pipe (|) and redirection operator is shown in this example.

Run the following command to write a string data into the text file named testdata2.txt by piping. The output of the “echo” command is sent to the input of the “cat” command using the pipe (|) operator:

$ echo "Learn Bash Programming" | cat > testdata2.txt

 
Run the following “cat” command to check the content of the testdata2.txt file:

$ cat testdata2.txt

 
Output:

The following output appears after executing the previous commands. According to the output, the output of the “echo” command is written into the testdata2.txt file:


Run the following command to write the output of the “ls –l” command into a text file named list.txt using the redirection operator (‘>’):

$ ls -l  > list.txt

 
Run the following “cat” command to check the content of the list.txt file:

$ cat list.txt

 
Output:

The following output appears after executing the previous commands. According to the output, the output of the “ls –l” command is written into the list.txt file:


Example 3: Use of Stdin and Stdout

The method of using both stdin and stdout to take an input from a file and write it into a file is shown in this example.

Run the following “cat” command to take the content of the testdata.txt file and write it into the testfile.txt file and the terminal:

$ cat < testdata.txt > testfile.txt

 
Run the following “cat” command to print the content of the testdata.txt file:

$ cat testdata.txt

 
Run the following “cat” command to print the content of the testfile.txt file:

$ cat testfile.txt

 
Output:

The following output appears after executing the previous commands. According to the output, the content of the testdata.txt file is written into the testfile.txt file and printed in the terminal:


Example 4: Use of Stderr

The content of the standard error can be printed in the terminal or redirected into a file or sent to the /dev/null that works like the recycle bin. The different ways to pass the standard error are shown in this example.

The following command is valid and it prints the “Hello” string with the newline. So, no standard error is generated for the following command:

$ printf "%s\n" "Hello"

 
The following command is invalid because there is no command named “pirntf”. So, a standard error is generated and the error is printed in the terminal:

$ pirntf "%s\n" "Hello"

 
Output:

The following output appears after executing the previous command. According to the output, the standard error is printed in the terminal:


Sometimes, it requires printing the custom error by hiding the standard error to make the error more understandable for the users. This task can be done by redirecting the error into the /dev/null. The “2>” is used here to redirect the error into /dev/null.

Run the following command to redirect the error into the /dev/null which will not display any error if any error exists:

$ pirntf "%s\n" "Hello" 2> /dev/null

 
Output:

The following output appears after executing the previous command. According to the output, the standard error is not printed in the terminal to redirect to /dev/null:


Sometimes, the standard error requires storing into a file for future use. This task can be done by redirecting the error into a file using the “2>” operator.

Run the following command to redirect the standard error into a file named errorfile.txt:

$ pirntf "%s\n" "Hello" 2> errorfile.txt

 
Run the following command to check the content of the errorfile.txt file:

$ cat errorfile.txt

 
Output:

The following output appears after executing the previous commands. According to the output, the standard error is written properly into the errorfile.txt file:


The standard error can be redirected to both /dev/null and the errorfile.txt file using the following command:

$ pirntf "%s\n" "Hello" 2> /dev/null > errorfile.txt

 

Conclusion

The uses of stdin, stdout, and stderr are explained in this tutorial using multiple examples that will help the Linux users to understand the concept of these streams and use them properly when required.



from https://ift.tt/5G8amQt

Post a Comment

0 Comments