π What is Redirection in Linux?
Redirection is a shell feature that lets you change the standard input/output behavior of commands. By default:
- Standard Input (stdin) comes from the keyboard.
- Standard Output (stdout) goes to the screen.
- Standard Error (stderr) also goes to the screen.
Redirection allows you to:
- Redirect input: Feed input to a command from a file instead of the keyboard.
- Redirect output: Save a commandβs output to a file instead of showing it on screen.
π Redirecting Input (stdin)
Use the < operator to redirect input from a file to a command.
Syntax:
command < inputfileπ Instead of typing input manually, the command reads from the file.
π§ͺ Example: Redirecting Input with tr
The tr (translate) command reads from standard input, so we often redirect input to it using <.
Syntax:
tr 'a-z' 'A-Z' < filename.txtπ What this does:
tr 'a-z' 'A-Z'β converts lowercase letters to uppercase.< filename.txtβ reads fromfilename.txtinstead of waiting for you to type input.
β
Example:
Letβs say greeting.txt contains:
hello world
You can run:
tr 'a-z' 'A-Z' < greeting.txtπ€ Output:
HELLO WORLD