Many commands invoked from Bash read their input from one or more files provided on the command line, particularly the classic Unix text filtering tools, such as grep and sort. When these commands are passed one or more filenames as arguments, they switch to reading their input from those files in the order they were specified, instead of from their own standard input:
$ grep pattern myfile1 myfile2 myfile3 $ sort -k1,1 myfile1 myfile2 myfile3
This tends to mean that you do not need to use Bash to redirect input as often as you need to redirect output, because well-designed Unix programs can usually change their input behavior by specifying filenames, without any shell syntax involved.
However, not all programs behave this way, and it will sometimes be necessary to use input redirection to specify the source for a command's input. For example, the tr Unix command is a simple way to translate characters in input to other characters, specified in sets. We can try it out on the command line to convert all lowercase ASCII letters to uppercase in each line we type:
$ tr a-z A-Z Hello, world! HELLO, WORLD!
If we wanted to do this with data from a file named mylines, we might try to write it like this:
$ tr a-z A-Z mylines
But that doesn't seem to work:
tr: extra operand ‘mylines’ Try 'tr --help' for more information.
This is because the tr program doesn't check its arguments for files; it only reads the standard input stream, which by default comes from your terminal, so we need another way to specify input for the command.
Many shell programmers try to solve this problem with a cat pipe first, like this:
$ cat mylines | tr a-z A-Z
This works, but it's what we refer to as a useless use of cat; because we only have one file, there's nothing to combine, and hence no need to make cat read it for us. Instead, we can use the input redirection operator, <, directly with the tr command:
$ tr a-z A-Z < mylines
In this case, the shell will open the mylines file and use it as the source for standard input for the tr command.
Like the output redirection operator, >, we can place the input redirection operator, <, elsewhere on the command line, including at the very start; this works, but it's not very commonly used:
$ < mylines tr a-z A-Z
Note also that input redirection, output redirection, and error redirection can all be performed simultaneously for the same command line:
$ tr a-z A-Z < mylines > mylines.capitalized 2> mylines.error
The preceding command runs capitalization on input read from mylines, writing the output to the mylines.capitalized file and any errors to mylines.error.