> ## 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 String Length

The `#` symbol in Bash is a heavily overloaded lexical and syntactic operator. Depending on its context, it functions as a comment initiator, a parameter expansion operator (for length evaluation, prefix stripping, and front-anchoring), an array element counter, a base-radix indicator in arithmetic evaluation, a history expansion designator, a format flag in builtins, and a prompt string escape sequence.

## Lexical Parsing: Comments

When `#` appears as the first character of an unquoted and unescaped word (meaning it is located at the very beginning of a line or is immediately preceded by an unquoted metacharacter such as a space, tab, newline, `|`, `&`, `;`, `(`, `)`, `<`, or `>`), the shell treats it and all subsequent characters on that line as a comment, discarding them during the tokenization phase. In interactive shells, this behavior is strictly governed by the `interactive_comments` shell option, which is enabled by default but can be disabled via `shopt -u interactive_comments`.

```bash theme={"dark"}

# This entire line is ignored by the parser
echo "String" # This is ignored from the hash onward
echo "String"# This is NOT a comment because # is not preceded by a metacharacter
echo "String" ; # This IS a comment because ; is a metacharacter separating words
```

## Parameter Expansion: Length Evaluation

Within parameter expansion syntax, prepending a variable name with `#` evaluates to the length in characters of the expanded value of that parameter.

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

## Parameter Expansion: Prefix Substring Removal

When `#` is placed after a parameter name (or strictly after the subscript for arrays) and followed by a pattern, it acts as a prefix-stripping operator. The shell matches the pattern against the beginning of the parameter's value.

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

```bash theme={"dark"}
${parameter#pattern}      # Removes shortest matching prefix
${parameter##pattern}     # Removes longest matching prefix
${array[@]#pattern}       # Applies shortest prefix removal to each array element
```

## Parameter Expansion: Pattern Substitution Front-Anchor

In pattern substitution (`${parameter/pattern/replacement}`), prefixing the pattern with `#` acts as a front-anchor. This forces the pattern match to occur strictly at the beginning of the expanded string.

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

## Array Length Evaluation

When applied to an array using the `@` or `*` subscripts, the `#` operator evaluates to the total number of populated elements within that indexed or associative array, rather than the character length of a single string.

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

## Special Parameter: Positional Count

As a standalone special parameter, `#` evaluates to the decimal integer representing the total number of positional parameters passed to the current script or function context.

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

## Arithmetic Evaluation: Base/Radix Notation

Within arithmetic expansion contexts (such as `$(( ))`, `(( ))`, or the `let` builtin), the `#` operator separates a base (radix) from a numeric value. The base must be a literal decimal integer token between 2 and 64. Because the base must be a literal token, unexpanded variable names cannot be used directly with the `#` operator; variables must be explicitly expanded.

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

## History Expansion: Command Line Designator

In history expansion, the `!#` designator refers to the entire command line typed so far before the current point.

```bash theme={"dark"}
echo "arg1" "arg2" !#
```

## Builtin Formatting: `printf` Alternate Flag

Within the `printf` builtin, `#` acts as an alternate format flag modifying the output of numeric conversions:

* **Octal/Hexadecimal:** For `%o`, `%x`, or `%X` conversions, it forces the output to be prefixed with `0`, `0x`, or `0X`, respectively.
* **Floating-Point:** For `%f`, `%e`, `%E`, `%g`, and `%G` conversions, it forces the output to always contain a decimal point. For `%g` and `%G`, it additionally prevents the truncation of trailing zeros.

```bash theme={"dark"}
printf "%#x\n" 255
printf "%#g\n" 123
```

## Prompt String Escape Sequence

In Bash prompt strings (such as `PS1` or `PS2`), `\#` is a built-in escape sequence that evaluates to the command number of the current command.

```bash theme={"dark"}
PS1="[\#] $ "
```

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