Updated April 20, 2023
Introduction to Linux Error codes
In the Linux operating system, we are working on many components. While working, we are getting lots of errors. The same errors can be acknowledged by the Linux error codes. There are different error codes available as per the execution error. We can capture the error code with the help of the “echo” command also.
To fix the error message in the Linux level jobs/query or application-level job, it is mandatory that we need to understand the nature of the error, the error description, and the error code. Accordingly, we can fix the application or job on the Linux level.
The error code utility i.e., “errno” is written by the Lars Wirzenius.
Syntax of Error Codes
errno [ OPTION ] [ STRING ]
echo $?
- errno: We can use the “errno” keyword in the syntax or command. It will take the two arguments as access OPTION and the string. As per the provided arguments, it will provide the error information as per the string or the error code.
- OPTION: We can provide the different flags as options that are compatible with the “errno” command.
- STRING: We can provide the short string as per the “error” define. It will provide the error information concerning the provided string.
- echo $?: The echo command will also provide the error code. We need to use the “?” symbol with the echo command.
How Linux Error Codes Works?
In Linux, we are having a total of 134 error codes. Every error message or the failure in the Linux environment, it will have their error code. With the help of error code, we can fetch the error description and help to fix the issue or error message. If we will keep the same error message as it is then it will be a big impact on the server level.
When we are executing any command or job on the Linux level without any error then it will be fine. But if the Linux command will not be executed properly then the Linux compiler will notify with the relevant error message to the login user.
Below are the lists of error code information available,
Sr No | Error Code | Error Number | Error Description |
1 | EPERM | 1 | It will print the error message if the operation is not permitted. |
2 | ENOENT | 2 | It will print the error message if there are no such files or directory exists. |
3 | ESRCH | 3 | It will print the error message if there is no such process exists. |
4 | EINTR | 4 | It will print the error message if any interrupted system calls. |
5 | EIO | 5 | It will print the error message if it is any input/output error. |
6 | ENXIO | 6 | It will print the error message if there is no such device or address exists. |
7 | E2BIG | 7 | It will print the error message if the argument list is too long. |
8 | ENOEXEC | 8 | It will print the error message if there is an exec format error. |
9 | EBADF | 9 | It will print the error message in it is a bad file descriptor. |
10 | ECHILD | 10 | It will print the error message if there are no child process exits. |
Examples to implement Linux Error codes
Here are the following examples mention below
Examples #1 – Print the List
The “errno” utility is a very simple and common way to check the list of error codes in a Linux environment. It will print all the list of error codes with the error number and error description.
Command :
errno -l
Explanation :
As per the above command, we can print all the list of error codes available in the Linux environment. It will print the details information of error code like an error code name, error code number, and the description of the error code.
Output :
Example #2 – Information of Individual Error Number
By default, we are getting all the list of error code information. But we can also get the individual error number information as well.
Command:
errno 9
Explanation :
In the “errno” utility, we can get the specific information error information with the help of an error number. We have used the error no “9” and get the information of “error number 9” only.
Output :
Example #3 – Information of Individual Error Name
In the error codes, we can get the individual error number information. Similarly, we are having the functionality to print the individual error name information.
Command :
errno EBADF
Explanation :
As per the above command, we can get the specific information error information with the help of the error name. We have used the error name “EBADF” and get the information of “error name (EBADF)” only.
Output :
Example #4 – Error Code Information from the Input String
In the Linux environment, we are having the functionality to get the error code and error number information from the input string. We need to use the “-s” option with the “errno” utility.
Note: we need to provide the relevant input string as an input to the “errno” command.
Command :
errno -s access
Explanation :
When we don’t know the exact error number or name then we can use the relevant string to identify the error code and description of it. We have used the “access” string and get the related information of access.
Output :
Example #5 – Get the Error Code Information with “echo” Command
In the Linux ecosystem, we are having the functionality to check the quick error code information via the “echo” command. If it will return the “0” output then the command was executed fine. If it will return the output value except “0” then there are some issues in the command or job.
Command :
cat test_file.txt
echo $?
Explanation :
In screenshot 1 (a), we are getting the echo command output as “0”. Hence, it is an indication of the proper execution of the command. In screenshot 1 (b), we are getting the echo command output as “1”. Because the previous command was not executed successfully.
Output :
Screenshot 1 (a)
Screenshot 1 (b)
Conclusion
We have seen the uncut concept of “Linux Error Codes” with the proper example, explanation, and command with different outputs. Every error code is having its unique error name and the error number. The error codes are very important in terms of fixing the issues on command or job level.
Recommended Articles
We hope that this EDUCBA information on “Linux Error Codes” was beneficial to you. You can view EDUCBA’s recommended articles for more information.