> ## 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 Variable

A Bash variable is a named parameter representing a memory location within the shell execution environment used to store volatile data. In POSIX shell terminology, a variable is specifically a parameter denoted by a name (an identifier consisting solely of letters, numbers, and underscores, beginning with a letter or underscore). By default, Bash variables are untyped and treated as character strings; however, context-specific operations, such as arithmetic expansion, can temporarily coerce them into integers.

## Assignment Syntax

Variable assignment requires strict adherence to spacing rules. The assignment operator (`=`) must not be surrounded by whitespace.

```bash theme={"dark"}

# Correct syntax
IDENTIFIER="value"


# Incorrect syntax (Bash will attempt to execute IDENTIFIER as a command)
IDENTIFIER = "value"
```

## Dereferencing and Parameter Expansion

To retrieve the value stored in a variable, the shell uses parameter expansion, triggered by the dollar sign (`$`).

Unquoted variable expansions are subject to two critical shell operations: word splitting (where the shell divides the expanded value into multiple arguments based on the `IFS` variable) and filename expansion (globbing). To prevent these unintended behaviors, variable expansions should almost always be enclosed in double quotes.

```bash theme={"dark"}

# Standard expansion (passed as an argument to a command)
echo "$IDENTIFIER"


# Bracketed expansion (Strict boundary definition)
echo "${IDENTIFIER}appended_text"
```

Bracketed expansion is mandatory when the variable name is immediately followed by characters that could be interpreted as part of the identifier.

**Quoting Mechanics:**

* **Double Quotes (`" "`):** Preserves the literal value of all characters except `$`, `` ` ``, and `\`. Variable expansion occurs. *(Note: The exclamation mark `!` is only treated as a special character if history expansion is enabled via `set -H`, which is disabled by default in non-interactive scripts).*
* **Single Quotes (`' '`):** Preserves the literal value of every character. Variable expansion is strictly suppressed.

## Scope and Visibility

Variables in Bash operate under three primary scoping rules:

1. **Global Scope (Default):** Variables declared in a script are globally accessible to the current shell environment from the point of declaration downward.
2. **Local Scope:** Restricted to the execution context of a function. Declared using the `local` builtin.

```bash theme={"dark"}
function_name() {
    local LOCAL_VAR="scoped_value"
}
```

3. **Environment (Exported) Scope:** Variables passed down to the environment of child processes spawned by the shell. Declared using the `export` builtin.

```bash theme={"dark"}
export ENV_VAR="child_accessible_value"
```

## Type Attributes

While inherently untyped, Bash provides the `declare` (or `typeset`) builtin to apply specific attributes to variables, restricting their behavior or altering how their data is stored in memory.

```bash theme={"dark"}

# Integer attribute (forces arithmetic evaluation on assignment)
declare -i NUM_VAR=10


# Read-only attribute (immutable, cannot be unset or reassigned)
declare -r CONST_VAR="static_data"


# Indexed Array (zero-based integer indexing)
declare -a INDEXED_ARR=("val1" "val2")


# Associative Array (string-based key-value pairs, requires Bash 4.0+)
declare -A ASSOC_ARR=(["key1"]="val1" ["key2"]="val2")
```

## Special Parameters

Bash maintains a set of internal variables that are automatically populated by the shell execution environment. These are read-only and cannot be explicitly assigned via `=`.

* `$0`: The name of the shell or shell script.
* `$1` to `$9`, `${10}...`: Positional parameters representing arguments passed to the script or function.
* `$#`: The total count of positional parameters.
* `$@` and `$*`: Expands to all positional parameters. When enclosed in double quotes, their semantic behavior diverges significantly:
  * `"$@"` expands to individual arguments as separate words (e.g., `"$1"` `"$2"` `"$3"`).
  * `"$*"` expands to a single string containing all arguments, delimited by the first character of the Internal Field Separator (`IFS`) (e.g., `"$1c$2c$3"` where `c` is the separator).
* `$?`: The exit status of the most recently executed foreground pipeline.
* `$$`: The Process ID (PID) of the current shell.
* `$!`: The Process ID (PID) of the most recently executed background command.

<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>
