Unix operating system supports three file descriptors. Those are stdin(0), stdout(1) and stderr(2). Here is an example that shows how to use them.
File descriptor means standard file ports. Precisely, you can redirect your script command result or you can get input from terminal to script. Input & Output – These two functions you can do with the file descriptors.
0 - stdin - Keyboard
1 - stdout - display
2 - stderr - display
Details of File Descriptors
What is a bit bucket in Unix?
It’s just a null file. It deletes data immediately after it receives, and sets the file status to end-of-file. Here is directory of Bit Bucket.
How to work with File Descriptors
The error messages routing bit bucket. Then sending stderr (2) to stdout(1). The ‘>&’ just copy the messages present in stderr to stdout. The ‘$?’ tells the status of previously executed command. Here is more on File Descriptors.
In the myfunction1, added a command. I am checking the command status by using $?. After closing curling bracket, I have given myfunction1 – why means this is how we call function in the script.
ls -0 > /dev/null 2>&1
Video – How bit bucket discards Error Messages
The status code is ‘2’. That means only you will get status codes, and you’ll not get error message, since it’s discarded.
Video – How to Display Error Messages
Here, I have used local directory path, instead of bit bucket directory. So you can see error message and status code as well.
ls -0 > /tmp/dev/null 2>&1
Get new content delivered directly to your inbox.