Shell Redirections ↔ Quick Guide
January 15, 2025 · View on GitHub
File descriptors:
stdin: 0stdout: 1stderr: 2
Redirecting stdin
This trick can be used to take multi-line input in scripts.
#!/usr/bin/env bash
echo -e "Enter Commit Message (Ctrl+d when done):"
msg=$(</dev/stdin)
echo $msg
Redirecting stderr
- Use
2>. Compatible with bothbashandsh
Redirecting both stderr & stdout
- With
bash, usesome_command &> /dev/null - With
sh,some_command > where-to-redirect 2>&1 # or some_command 2>&1 > stdout_and_err - If you want to capture standard output/error separately,
$ some_command 1>output.txt 2>error.txt