JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Echoing to stderr in Bash

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

The default error message variable for built-in Bash scripts is called stderr. It is also called standard error and is the default output device for errors.

Sometimes we have to redirect the error to an output channel. The Linux environment identifies each file object and its description, called FD.

It is a positive integer value identifying an open file session. For stderr, the file descriptor has the value 2.

In this article, we will learn about stderr and its functions. Also, we will look at some examples that will make the topic easier to understand.


Echoing to stderr in Bash

The command stderr is mainly used to keep a recode of the errors during the execution of any command. The general syntax of the command is:

Your command here 2>error.log

In the example syntax shared above, you can find a symbol 2> and a file called error.log. Now 2> represents the value of FILE DESCRIPTOR, which represents the identity of stderr.

In our case, the value of FILE DESCRIPTOR is 2. Now the file name we provide is for the log file generated during the command execution.

You can also echo stderr messages using the following syntax format.

Your command here >&2 echo "error"

Let's look at an example to simplify the topic. Suppose we have created an error command and want to echo the error message to the output.

Our example code will now look like this:

echo $( 3 + 2 )) >&2 echo "error"

In the code shared above, we have made a mistake intentionally to understand how it works. We have removed one of the brackets here, so this might be a mistake.

After that, we use echo to display the error. Running the above code will display the output as shown below.

./example.sh: line 1: syntax error near unexpected token `)'
./example.sh: line 1: `echo $( 3 + 2 )) >&2 echo "error" '

Now we correct the code as follows and run the command again.

echo $(( 3 + 2 )) >&2 "No error has been found."

After running the code, you will get the output as shown below.

5 No error has been found.

All the codes used in this article are written in Bash. It will only work in Linux Shell environment.

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:https://www.jiyik.com/en/xwzj/opersys_9944.html

Related Articles

Solve R command not found on Bash (or Cygwin)

Publish Date:2025/03/21 Views:102 Category:OPERATING SYSTEM

Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not found or similar error messages, for example syntax error near unexpected toke

How to fix the error Make Command Not Found in Cygwin

Publish Date:2025/03/21 Views:119 Category:OPERATING SYSTEM

Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err

Error handling in Bash

Publish Date:2025/03/21 Views:93 Category:OPERATING SYSTEM

This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively. Exit Codes in Bash Handling errors based on exi

Getting the absolute path in Bash

Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM

In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute

Difference between Bash Nohup and &

Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM

This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr

Renaming Files in Bash

Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM

With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way

Open Emacs in Bash

Publish Date:2025/03/21 Views:141 Category:OPERATING SYSTEM

This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit

Clear the Terminal Screen in Bash

Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM

There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your

Reload .bash_profile from the command line

Publish Date:2025/03/21 Views:67 Category:OPERATING SYSTEM

In the shell, .bash_profile is used to customize the configuration of user settings. It is stored in the root directory or home directory and is mostly hidden from other users. This file holds all the configuration of the shell and is also

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial