{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rednafi.com/misc/shell-redirection/",
  "description": "Complete guide to Bash redirection. Master stdout, stderr, pipes, tee, file descriptors, and shorthand syntax with practical examples for every case.",
  "path": "/misc/shell-redirection/",
  "publishedAt": "2024-09-12T00:00:00.000Z",
  "site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
  "tags": [
    "Shell",
    "Unix"
  ],
  "textContent": "I always struggle with the syntax for redirecting multiple streams to another command or a\nfile. LLMs do help, but beyond the most obvious cases, it takes a few prompts to get the\nsyntax right. When I know exactly what I'm after, scanning a quick post is much faster than\nwrestling with a non-deterministic kraken. So, here's a list of the redirection and piping\nsyntax I use the most, with real examples.\n\nRedirecting stdout and stderr\n\nRedirect stdout to a file\n\n- Standard way:\n\n    \n\n    This replaces the content of file with the stdout of command. For example:\n\n    \n\n- Print and redirect to file:\n\n    \n\n    Example:\n\n    \n\n    This prints \"Hello, world!\" to the terminal and also writes it to hello.txt.\n\nRedirect stderr to a file\n\n- Standard way:\n\n    \n\n    Sends all errors (stderr) to file. For example:\n\n    \n\n- Print and redirect stderr to file:\n\n    \n\n    Example:\n\n    \n\nRedirect both stdout and stderr to a file\n\n- Common approach:\n\n    \n\n    Combines stdout and stderr into one stream and saves them to file. For example:\n\n    \n\n- Print and redirect both to file:\n\n    \n\n    Example:\n\n    \n\n- Convenient shorthand:\n\n    \n\n    Example:\n\n    \n\nAppend instead of overwriting\n\n- Append stdout to a file:\n\n    \n\n    Example:\n\n    \n\n- Print and append stdout to file:\n\n    \n\n    Example:\n\n    \n\n- Append both stdout and stderr (explicit):\n\n    \n\n    Example:\n\n    \n\n- Print and append both stdout and stderr to file:\n\n    \n\n    Example:\n\n    \n\n- Convenient shorthand for appending both:\n\n    \n\n    Example:\n\n    \n\nPiping output\n\nPipe stdout to another command\n\n- Basic usage:\n\n    \n\n    This sends the stdout of command1 to the input of command2. For example:\n\n    \n\n- Print and redirect piped stdout to file:\n\n    \n\n    Example:\n\n    \n\nPipe both stdout and stderr\n\n- Common way:\n\n    \n\n    Combines stdout and stderr, then pipes the combined stream to command2. For example:\n\n    \n\n- Print and redirect both stdout and stderr to file:\n\n    \n\n    Example:\n\n    \n\nShorthand for piping both stdout and stderr (|&)\n\n- Shorthand syntax:\n\n    \n\n    This is equivalent to command1 2>&1 | command2, combining stdout and stderr. For\n    example:\n\n    \n\n- Print and redirect both stdout and stderr using |&:\n\n    \n\n    Example:\n\n    \n\nRedirecting file descriptors\n\nCustom file descriptors\n\n- Create a new file descriptor (e.g., 3) and redirect stdout to it:\n\n    \n\n    This sends the stdout of command to file descriptor 3, which points to outputfile.\n    For example:\n\n    \n\n- Print and redirect stdout to custom file descriptor:\n\n    \n\n    This prints \"Using FD 3\" to the terminal and simultaneously writes it to\n    custom_output.txt.\n\nRedirect stderr to a file descriptor\n\n- Common case:\n\n    \n\n    Redirects stderr to file descriptor 3. For example:\n\n    \n\n- Print and redirect stderr to custom file descriptor:\n\n    \n\n    Example:\n\n    \n\nRedirect both stdout and stderr to a file descriptor\n\n- Common way:\n\n    \n\n    Combines stdout and stderr, and redirects them to file descriptor 3.\n\n    Note: There's no shorthand equivalent for redirecting both stdout and stderr to a\n    file descriptor. You need to use the full syntax. For example:\n\n    \n\nDiscarding output\n\nSend stdout and stderr to /dev/null\n\n- Common:\n\n    \n\n    Silences all output (stdout and stderr). For example:\n\n    \n\n- Print and discard stdout and stderr (not sure why you'd ever need this):\n\n    \n\n    Example:\n\n    \n\n- Convenient shorthand:\n\n    \n\n    Example:\n\n    \n\nAt a glance\n\n- Redirect stdout: command > file\n- Redirect stderr: command 2> file\n- Redirect both stdout and stderr:\n    - Standard: command > file 2>&1\n    - Shorthand: command &> file\n\n- Append stdout: command >> file\n- Append both stdout and stderr:\n    - Standard: command >> file 2>&1\n    - Shorthand: command &>> file\n\n- Pipe stdout: command1 | command2\n- Pipe both stdout and stderr:\n    - Standard: command1 2>&1 | command2\n    - Shorthand: command1 |& command2\n\n- Custom file descriptors:\n    - Create and redirect stdout: exec 3> file; command >&3\n    - Redirect stderr: command 2>&3\n    - Redirect both stdout and stderr: command > /dev/fd/3 2>&1 (no shorthand available)\n\n- Discard stdout and stderr:\n    - Standard: command > /dev/null 2>&1\n    - Shorthand: command &>/dev/null",
  "title": "Shell redirection syntax soup"
}