Shell
shellshell-scriptingGuideBeginner
Shell I/O Redirection (>, >>, <, 2>&1)
Redirect standard input, overwrite files with >, append with >>, capture stderr with 2>, and merge streams with 2>&1.
Verified: 2026-09-30
Tested on:bash 5.x
Platforms:
linuxmacos
#shell#bash#redirection#io#cli
Redirection Operators Reference
| Operator | Action | Example |
|---|---|---|
> | Redirect stdout, overwriting destination file | echo "Hello" > output.txt |
>> | Redirect stdout, appending to destination file | echo "New Line" >> logs.txt |
< | Redirect stdin from a file | mysql -u root < dump.sql |
2> | Redirect stderr (errors only) | ls /root 2> error.log |
2>> | Append stderr to a file | backup.sh 2>> backup_errors.log |
2>&1 | Merge stderr into stdout | app.sh > full.log 2>&1 |
&> | Modern Bash shorthand for > file 2>&1 | app.sh &> full.log |
<<< | Here-string (passes string as stdin) | grep "foo" <<< "foobar" |
Practical Examples
1. Separate Standard Logs from Error Logs
python3 train_model.py > output.log 2> errors.log2. Capture Both Outputs to Single File
# Classic POSIX syntax
./build.sh > build.log 2>&1
# Modern Bash shorthand
./build.sh &> build.log3. Multi-Line Here-Documents (<< EOF)
cat << 'EOF' > /etc/nginx/conf.d/api.conf
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost:3000;
}
}
EOFShell Pipes & Pipeline Processing
Chain Linux commands together using the pipe operator (|) to construct powerful data processing pipelines.
Shell Variables & Parameter Expansion
Define shell variables, understand double vs single quoting rules, command substitution $(), and default parameter expansion in Bash.