> ## 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 Remove Matching Prefix Pattern

The `#` (hash or pound) character in Bash is an overloaded lexical token that serves multiple distinct syntactic functions depending on its execution context. It operates as a comment initiator, a parameter expansion modifier (for prefix truncation, length calculation, and pattern substitution anchoring), a special parameter for argument counting, a radix indicator in arithmetic evaluation, and an event designator in history expansion.

## 1. Lexical Comments

When `#` appears as the first character of a word (unquoted and unescaped), the Bash parser treats it and all subsequent characters on that line as a comment. The parser discards the comment during the tokenization phase before command execution.

```bash theme={"dark"}

# This is a full-line comment
command argument # This is an inline comment
```

## 2. Parameter Expansion: Prefix Truncation

Within parameter expansion, `#` and `##` strip a matching prefix pattern from the value of a parameter. The pattern undergoes tilde expansion, parameter expansion, command substitution, and arithmetic expansion.

* **Shortest Match (`#`):** Deletes the shortest matching prefix.
* **Longest Match (`##`):** Deletes the longest matching prefix.

```bash theme={"dark"}
${parameter#pattern}
${parameter##pattern}
```

If the pattern matches the beginning of the expanded value, the result is the expanded value with the matching prefix deleted. If the parameter is an array subscripted by `@` or `*`, or if it is the positional parameters (`@` or `*`), the prefix truncation operation is applied to each element individually, expanding to a list of the modified elements.

```bash theme={"dark"}
${array[@]#pattern}
${@##pattern}
```

## 3. Parameter Expansion: Pattern Substitution Anchor

In pattern substitution, a `#` immediately following the first slash acts as a prefix anchor. It forces the pattern match to occur only at the beginning of the expanded value of the parameter.

```bash theme={"dark"}
${parameter/#pattern/replacement}
```

Similar to prefix truncation, if this expansion is applied to an array subscripted by `@` or `*`, or to the positional parameters, the anchored substitution is applied to each element individually.

## 4. Parameter Expansion: Length Evaluation

When `#` is placed immediately before a parameter name within parameter expansion braces, it evaluates the length of the parameter's contents rather than returning the contents themselves.

* **String Length:** Evaluates to the number of characters in the parameter. If the parameter is an array referenced without a subscript, it defaults to index `0` and evaluates the string length of the first element (`${parameter[0]}`).

```bash theme={"dark"}
${#parameter}
```

* **Array Element Count:** If the parameter is an array subscripted by `@` or `*`, it evaluates to the total number of populated elements in the array.

```bash theme={"dark"}
${#array_name[@]}
${#array_name[*]}
```

* **Positional Parameter Count:** If the parameter is `@` or `*`, it evaluates to the number of positional parameters currently set.

```bash theme={"dark"}
${#@}
${#*}
```

## 5. Special Parameter: Argument Count

In strict Bash terminology, `#` is a special parameter maintained by the shell, not a variable (variables are a specific subset of parameters requiring valid alphanumeric names). The parameter expansion `$#` evaluates to a decimal integer representing the total number of positional parameters passed to the current script or the current function scope.

```bash theme={"dark"}
$#
```

## 6. Arithmetic Evaluation: Base (Radix) Indicator

Within arithmetic expansion contexts (such as `$(( ))`, `(( ))`, or the `let` builtin), `#` acts as a delimiter separating an arithmetic base from its corresponding integer value.

```bash theme={"dark"}
$((base#number))
```

The `base` must be a decimal integer between 2 and 64. The `number` is evaluated according to the specified `base`. If the base is omitted, base 10 is assumed (unless prefixed by `0` for octal or `0x` for hexadecimal).

## 7. Interactive History Expansion

In interactive shells with history expansion enabled, `#` functions as an event designator when prefixed with `!`. The `!#` sequence evaluates to the entire command line typed so far before the current cursor position.

```bash theme={"dark"}
!#
```

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