> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Bash Local Variable

A local variable in Bash is a variable whose scope is restricted to the execution context of the function in which it is declared, including any child functions invoked within that call stack. Declaring a local variable shadows any globally scoped variable or outer-scoped local variable sharing the same identifier until the declaring function terminates.

## Syntax and Declaration

Local variables are instantiated using the `local` shell builtin. The `local` command is functionally equivalent to `declare` but is strictly limited to function scope. It accepts the same attribute flags as `declare`.

```bash theme={"dark"}
function define_locals() {
    # Standard local declaration and assignment
    local var_name="string_value"
    
    # Local declaration with attribute flags
    local -i int_var=42       # Integer attribute
    local -r const_var="foo"  # Readonly attribute
    local -a array_var=(1 2)  # Indexed array attribute
    local -A dict_var         # Associative array attribute
}
```

## Technical Mechanics

**Dynamic Scoping**
Unlike languages that utilize lexical (static) scoping, Bash employs dynamic scoping. A local variable is visible not only to the function that declares it but also to any subsequent functions called from within that function's execution block.

```bash theme={"dark"}
function parent_func() {
    local shared_var="parent_scope"
    child_func
}

function child_func() {
    # Inherits access to shared_var due to dynamic scoping
    # Modifying it here modifies the parent_func's local instance
    shared_var="modified_by_child"
}
```

**Execution Context Restriction**
The `local` builtin is strictly bound to function execution. Attempting to invoke `local` in the global scope (outside of a function definition) results in a runtime error: `bash: local: can only be used in a function`.

**Shadowing and Call Frame Restoration**
When a local variable is declared, Bash allocates a new memory space for the identifier within the current call frame. Any existing variable with the same name in a higher scope is shadowed. Once the function returns, the local call frame is destroyed, and the shadowed variable's original value and attributes are restored to the environment.

**Exit Code Masking**
A critical mechanical behavior of `local` occurs during command substitution. Because `local` is a builtin command, combining declaration and command substitution on a single line causes the exit status (`$?`) to reflect the success of the `local` assignment (which is `0`), thereby masking the exit status of the substituted command.

```bash theme={"dark"}
function masked_exit_code() {
    # The exit code of 'some_command' is masked. $? will evaluate to 0.
    local result=$(some_command)
    
    # Strict mechanical approach: Declare first, assign second.
    local result
    result=$(some_command) # $? will correctly reflect some_command's exit status
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
