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