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

A string in Bash is a contiguous sequence of characters treated as a single unit of data. Because Bash is fundamentally a macro processor and untyped by default, all variables are inherently treated as character strings unless explicitly declared with an integer attribute via `declare -i`. String manipulation in Bash is primarily handled through parameter expansion rather than dedicated functions or methods.

## Quoting Mechanisms

The parsing of a string depends entirely on the quoting mechanism used to define it.

* **Unquoted:** Subject to word splitting (based on `IFS`) and pathname expansion (globbing).
* **Strong Quoting (`'...'`):** Suppresses all interpolation. Every character within single quotes is treated as a literal.
* **Weak Quoting (`"..."`):** Suppresses word splitting and globbing but permits parameter expansion (`$`), command substitution (`$()`), and arithmetic expansion (`$((...))`).
* **ANSI-C Quoting (`$'...'`):** Evaluates backslash-escaped characters according to the ANSI C standard (e.g., `\n` for newline, `\t` for tab, `\xNN` for hexadecimal).

```bash theme={"dark"}
str_unquoted=HelloWorld
str_strong='Literal $var \n'
str_weak="Interpolated $var"
str_ansi=$'Line1\nLine2'
```

## Concatenation

Bash does not use a dedicated concatenation operator. Strings are concatenated via juxtaposition (placing them adjacent to one another).

```bash theme={"dark"}
str1="Hello"
str2="World"
concat_str="${str1} ${str2}!"
```

## Parameter Expansion (String Operations)

Bash provides built-in string manipulation capabilities through parameter expansion syntax `${...}`.

### Length

Returns the number of characters in the string.

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

### Substring Extraction

Extracts a portion of the string starting at a zero-based `offset` for a specified `length`. If `length` is omitted, it extracts to the end of the string. Negative offsets must be separated by a space to avoid conflict with default-value expansions.

```bash theme={"dark"}
${string:offset:length}
${string: -offset:length}
```

### Substring Removal (Stripping)

Removes a substring matching a shell pattern (glob) from either the beginning (prefix) or the end (suffix) of the string.

* **Shortest Prefix:** `${string#pattern}`
* **Longest Prefix:** `${string##pattern}`
* **Shortest Suffix:** `${string%pattern}`
* **Longest Suffix:** `${string%%pattern}`

```bash theme={"dark"}

# Syntax visualization
${var#*.}   # Removes shortest match of "*." from the front
${var##*.}  # Removes longest match of "*." from the front
${var%.*}   # Removes shortest match of ".*" from the back
${var%%.*}  # Removes longest match of ".*" from the back
```

### Search and Replace

Replaces occurrences of a shell pattern with a replacement string.

* **First Match:** `${string/pattern/replacement}`
* **All Matches:** `${string//pattern/replacement}`
* **Prefix Match:** `${string/#pattern/replacement}` (Pattern must match the beginning)
* **Suffix Match:** `${string/%pattern/replacement}` (Pattern must match the end)

```bash theme={"dark"}

# Syntax visualization
${var/foo/bar}   # Replaces first 'foo' with 'bar'
${var//foo/bar}  # Replaces all 'foo' with 'bar'
```

### Case Modification (Bash 4.0+)

Modifies the alphabetic case of characters matching an optional pattern (defaults to `?`, matching any character).

* **Uppercase First Character:** `${string^pattern}`
* **Uppercase All Characters:** `${string^^pattern}`
* **Lowercase First Character:** `${string,pattern}`
* **Lowercase All Characters:** `${string,,pattern}`

```bash theme={"dark"}

# Syntax visualization
${var^}   # Capitalizes the first letter
${var^^}  # Converts entirely to uppercase
${var,,}  # Converts entirely to lowercase
```

## String Comparison Operators

String comparison is performed within test commands (`[ ... ]` or `[[ ... ]]`). The extended test construct `[[ ... ]]` is preferred for strings as it prevents word splitting and supports advanced pattern matching.

* **Equality:** `==` or `=` (True if strings are identical)
* **Inequality:** `!=` (True if strings are not identical)
* **Lexicographical Less Than:** `<` (Evaluates based on the current locale's sorting order)
* **Lexicographical Greater Than:** `>`
* **Zero Length:** `-z` (True if string length is 0)
* **Non-Zero Length:** `-n` (True if string length is greater than 0)
* **Regular Expression Match:** `=~` (True if the string on the left matches the POSIX Extended Regular Expression on the right; only valid inside `[[ ... ]]`)

```bash theme={"dark"}

# Syntax visualization
[[ "$str1" == "$str2" ]]
[[ "$str1" < "$str2" ]]
[[ -z "$str1" ]]
[[ "$str1" =~ ^[A-Za-z]+[0-9]$ ]]
```

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