{
    "slug": "bash_scripting",
    "term": "Bash Scripting",
    "category": "linux",
    "difficulty": "intermediate",
    "short": "Writing shell scripts in Bash to automate command-line tasks — from simple file operations to complex deployment pipelines.",
    "long": "Bash scripts combine shell commands with variables, conditionals, loops, and functions. Key practices: always start with #!/bin/bash, use set -euo pipefail to fail on errors, quote all variable expansions to handle spaces, use [[ ]] over [ ] for safer conditionals, and prefer $() over backticks for command substitution. ShellCheck is a static analyser that catches common Bash mistakes automatically.",
    "aliases": [
        "shell scripting",
        "bash script"
    ],
    "tags": [
        "linux",
        "bash",
        "scripting",
        "devops",
        "automation"
    ],
    "misconception": "Bash scripts always exit on error — by default, Bash continues after a failed command; use set -e or check return codes explicitly.",
    "why_it_matters": "Deployment and automation scripts that silently continue after errors cause partial deployments and data corruption — set -euo pipefail makes failures loud and visible.",
    "common_mistakes": [
        "Not using set -euo pipefail — scripts continue silently after errors.",
        "Unquoted variable expansions — $filename breaks if the filename contains spaces; use \"$filename\".",
        "Using [ ] instead of [[ ]] — [[ ]] handles empty variables and complex expressions safely.",
        "Not validating input arguments — scripts crash with cryptic errors when arguments are missing."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "cron_jobs",
        "php_deployment_pipeline",
        "linux_file_permissions"
    ],
    "prerequisites": [
        "linux_processes",
        "linux_file_permissions",
        "php_cli"
    ],
    "refs": [
        "https://www.gnu.org/software/bash/manual/bash.html",
        "https://www.shellcheck.net/"
    ],
    "bad_code": "#!/bin/bash\n# Missing set -e — continues on error\ncd /var/www/app\ngit pull\nphp artisan migrate  # If this fails, next line still runs\nphp artisan cache:clear\n# Partial deployment with failed migration",
    "good_code": "#!/bin/bash\nset -euo pipefail  # Exit on error, undefined vars, pipe failures\n\nAPP_DIR=\"/var/www/app\"\ncd \"$APP_DIR\"\n\ngit pull origin main\necho 'Running migrations...'\n/usr/bin/php artisan migrate --force\necho 'Clearing caches...'\n/usr/bin/php artisan cache:clear\necho 'Deploy complete'",
    "quick_fix": "Add set -euo pipefail at the top of every bash script — it exits on error, treats unset variables as errors, and pipes fail if any command fails",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/bash_scripting",
        "html_url": "https://codeclaritylab.com/glossary/bash_scripting",
        "json_url": "https://codeclaritylab.com/glossary/bash_scripting.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": "[Bash Scripting](https://codeclaritylab.com/glossary/bash_scripting) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/bash_scripting"
            }
        }
    }
}