This article will teach you the basics behind input and output redirection in Bash.
This is a very cool piece of Bash that allows you to get data from a file, put data into a file, or even string several commands together. The two Bash Basics that allow us to accomplish this are called Standard Input and Standard Output. Most Bash commands support both of these.
1. What is Standard I/O?
Standard input and standard output are two protocols that Bash commands use. Normally, they are both run from the command line; the commands will send their output to the command line and then look for input from the command line. Most of the time, this works fine and we have no need for them to look anywhere else. However, there are times when we want to save the output in a more permanent fashion, or get input that we don’t have to manually type in. To do this, we use something called redirection.
2. Redirection? What’s that?
I/O redirection is quite simple. You simply redirect where the command is looking. Instead of checking the command line for input, it will look wherever you specify. The same principle holds true for output. The simplest way to redirect I/O in Bash is through the use of brackets, specifically either < or >. Using < will redirect the input of the command (the smaller side of the bracket indicates where the information is headed). This, > will in turn redirect the output of the command.
3. Redirection to Files
One of the most common uses of redirection is the redirection to and from plaintext files. So you could redirect the output of an
ls command to a file by typing:
ls > file_lists.txt
You could then load this plaintext file later or perform other actions on it.
Two important things to note:
- If the file you are redirecting your output to doesn’t exist when you call the command, it will be created for you.
- Every time you redirect using > the file will be overwritten starting from the beginning. If you want your output to begin at the end of the file and be appended you must use two brackets (>>). So if you type:
ls >> file_list.txt
every time you run that command, it will end up at the end of the file.
4. Redirection from files
Redirection from files is just as easy as redirecting to files. You just type the command you want and use a < bracket. For example, if you typed:
sort < sortable_list.txt
This would be outputted to the command line, because you are not redirecting its output. However, if you typed:
sort < sortable_list.txt > the_sorted_list.txt
Both the input and the output would be redirected.
5. Redirection Pipes
Using I/O redirection you can redirect from one command to another. You can redirect the output from one command and make it the input for another, as in this example:
ls -lt | head
The | in the middle of the command is the redirection. You can string many commands together to perform pretty cool functions. (Some of you may notice the |’s in our Mac OS X password cracking article).
Remember, the best way to get it down is to go out and experiment by yourself. Try redirecting output and input and stringing things together.
In the next Bash 101 article, we’ll be covering basic math computations in Bash, and we’ll go over a few example scripts to work on.