{
    "slug": "shell_pipes_redirects",
    "term": "Shell Pipes & Redirects",
    "category": "linux",
    "difficulty": "intermediate",
    "short": "Pipes (|) connect stdout of one command to stdin of another; redirects (<, >, >>) send stdin/stdout to files — the foundation of composable Unix command-line workflows.",
    "long": "Standard streams: stdin (0), stdout (1), stderr (2). Redirect stdout: cmd > file (overwrite), cmd >> file (append). Redirect stderr: cmd 2> error.log. Redirect both: cmd > out.log 2>&1. Pipe: cmd1 | cmd2 connects cmd1's stdout to cmd2's stdin. Named pipes (mkfifo) persist as filesystem objects. Pipes are synchronous — cmd1 runs concurrently with cmd2, with a kernel buffer between them. Understanding pipes enables composing tools: cat access.log | grep '500' | awk '{print $1}' | sort | uniq -c | sort -rn",
    "aliases": [
        "pipes",
        "stdin",
        "stdout",
        "stderr",
        "redirects"
    ],
    "tags": [
        "linux",
        "bash",
        "shell"
    ],
    "misconception": "cmd > out.log 2>&1 and cmd 2>&1 > out.log are equivalent — order matters: the second form redirects stderr to the terminal stdout first, then redirects stdout to the file — stderr still goes to the terminal.",
    "why_it_matters": "Pipes and redirects are how PHP deployment scripts, log analysis, and server administration tasks are composed from simple tools — essential for writing correct bash scripts.",
    "common_mistakes": [
        "Losing stderr in deployment scripts — use 2>&1 to capture errors alongside stdout in logs.",
        "2>&1 > file vs > file 2>&1 — order matters; always put the file redirect first: > file 2>&1.",
        "Using cat file | grep instead of grep file — unnecessary pipe; grep reads files directly.",
        "Not quoting pipes in PHP exec() — shell special characters in arguments can break the pipe."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "bash_scripting",
        "linux_file_system",
        "cron_jobs",
        "php_cli"
    ],
    "prerequisites": [
        "bash_scripting",
        "linux_processes",
        "linux_file_system"
    ],
    "refs": [
        "https://www.gnu.org/software/bash/manual/bash.html#Redirections"
    ],
    "bad_code": "# Wrong order — stderr not captured in file:\ndeploy.sh 2>&1 > deploy.log\n# stderr goes to terminal (original stdout at time of 2>&1)\n# stdout goes to deploy.log\n# Errors missing from log!\n\n# PHP: exec without capturing stderr:\nexec('composer install', $output); // Errors from composer lost",
    "good_code": "# Correct order — both streams to file:\ndeploy.sh > deploy.log 2>&1\n# stdout → file, then stderr → same as stdout (now the file)\n\n# PHP: capture stderr too:\nexec('composer install 2>&1', $output, $exitCode);\nif ($exitCode !== 0) {\n    file_put_contents('/var/log/deploy_errors.log', implode(\"\\n\", $output));\n}",
    "quick_fix": "Understand pipes (|) and redirects (>, >>, 2>&1) to build PHP deployment scripts — grep errors in PHP logs, transform with awk, count with wc, all without writing PHP",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/shell_pipes_redirects",
        "html_url": "https://codeclaritylab.com/glossary/shell_pipes_redirects",
        "json_url": "https://codeclaritylab.com/glossary/shell_pipes_redirects.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Shell Pipes & Redirects](https://codeclaritylab.com/glossary/shell_pipes_redirects) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/shell_pipes_redirects"
            }
        }
    }
}