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

The `^` (caret) character in Bash is a context-dependent operator that performs five distinct operations based on the parsing environment: parameter expansion, arithmetic evaluation, bracket expression negation, regular expression anchoring, and history expansion.

**1. Parameter Expansion (Case Modification)**
Introduced in Bash 4.0, the `^` operator within parameter expansion modifies lowercase alphabetic characters to uppercase.

* A single `^` modifies only the first character of the expanded parameter.
* A double `^^` modifies all characters of the expanded parameter.
* An optional pattern can follow the operator to restrict the modification to characters matching the pattern. If omitted, the pattern defaults to `?` (matches any single character).
* When applied to an array subscripted with `@` or `*`, or to positional parameters (`$@` or `$*`), the case modification applies to each element individually rather than treating the expanded array as a single string.

```bash theme={"dark"}
string="hello world"
echo "${string^}"          # Output: Hello world
echo "${string^^}"         # Output: HELLO WORLD
echo "${string^^[aeiou]}"  # Output: hEllO wOrld

array=("apple" "banana" "cherry")
echo "${array[@]^}"        # Output: Apple Banana Cherry
```

**2. Arithmetic Expansion (Bitwise XOR)**
Within an arithmetic context (`$((...))` or `((...))`), the `^` operator functions as a bitwise Exclusive OR (XOR) operator. It evaluates the binary representations of two integer operands, returning `1` in each bit position where the corresponding bits of either, but not both, operands are `1`.

```bash theme={"dark"}

# 5 (binary 0101) ^ 3 (binary 0011) = 6 (binary 0110)
result=$(( 5 ^ 3 ))
echo "$result"             # Output: 6

(( value = 10 ^ 2 ))
echo "$value"              # Output: 8
```

**3. Bracket Expression Negation (Pattern Matching & Globbing)**
When used as the first character immediately following the opening bracket `[` in a bracket expression, `^` negates the character class. It instructs the parser to match any single character *not* enclosed in the brackets. This applies to both filename expansion (globbing) and pattern matching constructs (such as `case` statements or the `[[ == ]]` operator). In Bash, `^` is synonymous with `!` in this context.

```bash theme={"dark"}
char="X"

# Matches because 'X' is not a lowercase letter
[[ $char == [^a-z] ]] && echo "Match"  # Output: Match


# Filename expansion (globbing)
touch file1.txt fileA.txt

# Expands to files that do not have a numeric digit after 'file'
ls file[^0-9].txt                      # Output: fileA.txt
```

**4. Regular Expression Anchor**
When utilized on the right-hand side of the `=~` binary operator within a `[[ ... ]]` conditional construct, `^` functions as a POSIX Extended Regular Expression (ERE) anchor. It acts as a zero-width assertion that binds the subsequent pattern strictly to the beginning of the string.

```bash theme={"dark"}
string="bash script"
[[ $string =~ ^bash ]] && echo "Starts with bash"  # Output: Starts with bash
[[ $string =~ ^script ]] || echo "Does not start with script" # Output: Does not start with script
```

**5. History Expansion (Quick Substitution)**
The `^` operator acts as a history expansion designator strictly within an interactive shell environment. It is a syntactic shorthand for the `!!:s/string1/string2/` history substitution command. It retrieves the immediately preceding command from the history list, replaces the first occurrence of the first string with the second string, and evaluates the modified command. Because history expansion is disabled by default in non-interactive shells, this syntax will fail or be treated as a literal string if placed in a standard Bash script.

```bash theme={"dark"}

# INTERACTIVE SHELL CONTEXT ONLY
$ echo "testing one"
testing one

$ ^one^two^
echo "testing two"
testing two
```

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