0
0
Linux CLIscripting~10 mins

stderr redirection (2>, 2>>) in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - stderr redirection (2>, 2>>)
Run command
Command outputs
stdout (normal output)
stderr (error output)
Redirect stderr?
Yes
Overwrite or Append file
End
When a command runs, it sends normal output to stdout and errors to stderr. Using 2> or 2>> redirects stderr to a file, overwriting or appending.
Execution Sample
Linux CLI
ls /notexist 2> error.txt
ls /notexist 2>> error.txt
Run a command that fails, redirect error output to a file, first overwriting then appending.
Execution Table
StepCommandstderr OutputRedirection OperatorFile Content After Step
1ls /notexist 2> error.txtls: cannot access '/notexist': No such file or directory 2>ls: cannot access '/notexist': No such file or directory
2ls /notexist 2>> error.txtls: cannot access '/notexist': No such file or directory 2>>ls: cannot access '/notexist': No such file or directory ls: cannot access '/notexist': No such file or directory
💡 After step 2, stderr is appended to error.txt, so file has two error messages.
Variable Tracker
VariableStartAfter Step 1After Step 2
error.txt content(empty)ls: cannot access '/notexist': No such file or directory ls: cannot access '/notexist': No such file or directory ls: cannot access '/notexist': No such file or directory
Key Moments - 2 Insights
Why does the file content change differently between 2> and 2>>?
2> overwrites the file with new stderr output (see step 1 in execution_table), while 2>> appends new stderr output to the existing file content (see step 2).
Does 2> redirect normal output (stdout)?
No, 2> only redirects stderr (error messages). Normal output goes to stdout unless redirected separately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of error.txt after step 1?
AEmpty file
BTwo error messages
COne error message
DNormal output of ls command
💡 Hint
Check the 'File Content After Step' column for step 1 in execution_table.
At which step does the file error.txt get appended instead of overwritten?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
Look at the 'Redirection Operator' column in execution_table to see which uses 2>>.
If we used > instead of 2> in step 1, what would happen?
Astdout would be redirected to error.txt
BBoth stdout and stderr would be redirected
Cstderr would be redirected to error.txt
DNo output would be redirected
💡 Hint
Remember 2> redirects stderr, > redirects stdout by default.
Concept Snapshot
stderr redirection:
- 2> filename : redirect stderr to filename (overwrite)
- 2>> filename : redirect stderr to filename (append)
- stdout is not affected
- Use to capture error messages separately
- Useful for debugging commands
Full Transcript
When you run a command in Linux, it sends normal output to stdout and error messages to stderr. Using 2> followed by a filename redirects the error messages to that file, replacing any existing content. Using 2>> appends error messages to the file instead of replacing it. Normal output is not redirected by these operators. This helps separate errors from normal output and is useful for troubleshooting.