πŸ” 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 from filename.txt instead 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