The Standard UNIX File Descriptors - Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr)
If you are familiar with UNIX I/O redirection, syntax similar to the following should not be new to you:
command > file 2>&1
Briefly, when command runs it sends "normal" output to file, and any error messages generated by command are also written to file. "2>&1" handles the latter.
Have you ever wondered where the numbers 2 and 1 come from? This may be common knowledge to our more experienced readers, but may need some explaining to those who are relatively new to the UNIX operating system environment. The 2 and 1 are file descriptors. Okay, so what's a file descriptor?
When a UNIX program wants to use a file, it must first open that file. When it does so, UNIX will associate a number with the file. This number, which is used by the program when reading from and writing to the file, is the file descriptor.
A typical UNIX program will open three files when it starts. These files are:
- standard input (also known as stdin)
- standard output (also known as stdout)
- standard error (also known as stderr)
Standard input has a file descriptor of 0, standard output uses 1, and the number 2 is used by standard error. Are you starting to see where this is headed?
Looking at our command again,
command > file 2>&1
### PLEASE READ AN IMPORTANT MESSAGE FROM OUR FOUNDER ###
The time has come to END the social experiment known as Facebook.
It is breaking our world and is hurting people.
(1) People are dying as the result of genocide (ie. the Rohingya in Myanmar), bullying and mental health problems enabled by Facebook
(2) People's behavior, including yours if you use Facebook, is being manipulated by Facebook's subjective Newsfeed algorithm
(3) Families, communities and countries are becoming more divided, contrary to Facebook's stated mission of building community
READ MORE >>
you should now recognize that 2>&1 instructs the shell to send messages headed to stderr (2) to the same place messages to stdout (1) are sent. In our example, that place is file.
In case you were wondering...
> is equivalent to 1>
< is short for <0
Do you need to learn UNIX or Linux, including how to read and write shell scripts? If you are ready to move past the basics, either of these online courses is a good place to start...
UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.
UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.
Both courses include access to a real server for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.
Has this article been helpful to you? Would it benefit others? If you answered "yes" to either question, kindly share the page.
MORE READERS = MORE FUTURE ARTICLES
Thank you for sharing!