Today I Learned
Search…
Shell Redirections ↔ Quick Guide
File descriptors:
  • stdin : 0
  • stdout : 1
  • stderr : 2

Redirecting stdin

This trick can be used to take multi-line input in scripts.
1
#!/usr/bin/env bash
2
​
3
echo -e "Enter Commit Message (Ctrl+d when done):"
4
msg=$(</dev/stdin)
5
echo $msg
Copied!

Redirecting stderr

  1. 1.
    Use 2>. Compatible with both bash and sh

Redirecting both stderr & stdout

  1. 1.
    With bash, use some_command &> /dev/null
  2. 2.
    With sh,
    1
    some_command > where-to-redirect 2>&1
    2
    # or
    3
    some_command 2>&1 > stdout_and_err
    Copied!
  3. 3.
    If you want to capture standard output/error separately,
    1
    $ some_command 1>output.txt 2>error.txt
    Copied!