Input redirection in Linux allows commands to receive input from sources other than the keyboard (stdin). Below are the main methods, including tee for capturing output while redirecting.
Redirects input from a file instead of the keyboard.
command < input.txtExample with cat:
cat < file.txtThis is equivalent to cat file.txt and displays the file's contents.
A here document (<<) provides multiple lines of input inline.
cat << EOF
Hello
This is a test
EOFThis prints:
Hello
This is a test
EOF is a delimiter; you can use any word (e.g., END, DATA).
A here string (<<<) sends a single string as input.
wc -w <<< "Hello world"Output:
2
(wc -w counts words, and "Hello world" has 2 words.)
A pipe (|) sends the output of one command as input to another.
ls -l | wc -lls -llists files.wc -lcounts lines.
These refer to standard input and can be used as an alternative to <.
cat /dev/stdinType some text, then press Ctrl+D to end input.
The tee command reads input from stdin, writes it to a file, and also passes it to the next command.
ls -l | tee output.txttee output.txtwrites the output tooutput.txtand displays it.
ls -l | tee -a output.txt(-a appends instead of replacing.)
You can also use tee to store input while passing it:
cat << EOF | tee input_copy.txt | wc -w
Hello
Linux input redirection
EOF- Saves input to
input_copy.txt. - Counts words and prints the result.
| Method | Usage | Best for |
|---|---|---|
< file |
Redirect file input | Using a file as input |
<< EOF |
Here document | Multi-line input |
<<< "text" |
Here string | Single-line input |
| ` | ` | Pipe |
/dev/stdin |
Read from standard input | Alternative method |
tee file |
Save & pass output | Logging while executing |
Would you like more practical examples? 🚀