Problem Statement
What does 2>&1 do in shell scripting?
Explanation
Standard streams: stdin (0), stdout (1), stderr (2). Redirection operators: > redirects stdout to file (overwrites), >> appends stdout, 2> redirects stderr, 2>&1 redirects stderr to stdout's current destination. Example: command > output.txt 2>&1 sends both stdout and stderr to output.txt. Order matters: redirect stdout first, then stderr to stdout.
Common patterns: command > /dev/null 2>&1 discards all output (both stdout and stderr to null device), command 2> error.log logs only errors, command > output.log 2>&1 captures both in single file. Use &> or >& as shorthand for > file 2>&1 in bash (redirects both to file).
Input redirection: < file reads stdin from file, << EOF creates here document (multi-line input until EOF), <<< 'string' provides string as stdin. Pipe | connects stdout of one command to stdin of next: command1 | command2. Understanding redirection is crucial for handling script output, logging, error handling, and creating pipelines.
