0
0
Linux CLIscripting~10 mins

stdin redirection (<) in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - stdin redirection (<)
Start command expecting input
Check for '<' operator
Yes
Open file specified after '<'
Redirect file content to command's stdin
Command reads input from file
Command executes using file input
Output
This flow shows how the shell redirects input from a file to a command using '<', letting the command read from the file instead of keyboard.
Execution Sample
Linux CLI
cat < input.txt
This command uses stdin redirection to make 'cat' read from 'input.txt' instead of keyboard.
Execution Table
StepActionFile OpenedInput Source for CommandCommand Output
1Shell sees 'cat < input.txt'input.txtstdin redirected from input.txt
2Shell opens 'input.txt' for readinginput.txtstdin redirected from input.txt
3Command 'cat' starts, reads from stdininput.txtreads content of input.txt
4Command outputs content read from input.txtinput.txtreads content of input.txtContent of input.txt displayed
5Command finishes executioninput.txtstdin closedOutput complete
💡 Command finishes after reading all input from 'input.txt' redirected via '<'
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
stdinkeyboardfile input.txt openedreading from input.txtclosed
command_outputnonenonebuffering contentcontent displayed
Key Moments - 2 Insights
Why does the command read from the file instead of waiting for keyboard input?
Because the shell redirects stdin from the file using '<', as shown in execution_table step 2 and 3, so the command reads from the file, not keyboard.
What happens if the file after '<' does not exist?
The shell cannot open the file (step 2 fails), so the command does not run and an error is shown.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the command start reading input from the file?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Input Source for Command' column in execution_table row for Step 3
According to variable_tracker, what is the state of 'stdin' after Step 5?
Akeyboard
Bclosed
Cfile input.txt opened
Dreading from input.txt
💡 Hint
Look at the 'stdin' row in variable_tracker after Step 5
If the file 'input.txt' is empty, what will the command output be?
AContent of input.txt displayed
BError message
CNo output (empty)
DCommand waits for keyboard input
💡 Hint
Refer to execution_table Step 4 output when reading file content
Concept Snapshot
stdin redirection (<):
Use '<' to send file content as input to a command.
Syntax: command < filename
The command reads from the file instead of keyboard.
If file missing, command errors.
Useful to automate input from files.
Full Transcript
This visual trace shows how stdin redirection works in Linux shell. When you run a command like 'cat < input.txt', the shell sees the '<' operator and opens the file 'input.txt'. It then redirects the command's standard input to read from this file instead of the keyboard. The command reads the file content as if typed by the user and outputs it. After reading all content, the command finishes. If the file does not exist, the shell will show an error and the command won't run. This lets you automate commands that normally wait for keyboard input by feeding them file data instead.