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

The `~` (tilde) operator triggers **tilde expansion**, a built-in shell parsing mechanism where Bash replaces a tilde-prefixed string with a specific absolute directory path. This expansion occurs strictly before variable expansion, command substitution, and pathname expansion, provided the tilde is unquoted and positioned at the beginning of a word or immediately following an unquoted `=` or `:` in a variable assignment.

During parsing, Bash identifies an unquoted `~` and reads all subsequent characters up to the first unquoted slash `/` (or the end of the word). This extracted string is evaluated as the **tilde-prefix**. If the tilde-prefix matches a known expansion rule, the shell substitutes the prefix with the corresponding path. If the prefix is invalid or unresolvable, the string is left completely unchanged.

## Syntax and Expansion Variants

**Current User Home Directory**

```bash theme={"dark"}
~
~/path
```

An un-suffixed tilde expands to the value of the `$HOME` shell variable. If `$HOME` is unset, Bash falls back to the home directory of the user executing the shell, resolved via the operating system's password database (e.g., `getpwuid()`).

**Specific User Home Directory**

```bash theme={"dark"}
~username
~username/path
```

Expands to the absolute home directory associated with the specified `username`, resolved directly from the system's password database (e.g., `getpwnam()`).

**Current Working Directory**

```bash theme={"dark"}
~+
~+/path
```

Expands to the value of the `$PWD` shell variable, representing the current working directory.

**Previous Working Directory**

```bash theme={"dark"}
~-
~-/path
```

Expands to the value of the `$OLDPWD` shell variable, representing the previous working directory. If `$OLDPWD` is unset, the expansion fails and the string remains literal.

**Directory Stack Expansion**

```bash theme={"dark"}
~+N
~-N
```

Expands to specific entries within the shell's directory stack (managed via `pushd` and `popd`).

* `~+N` expands to the string that would be displayed by the `dirs +N` command (the *N*th directory from the left of the stack, zero-indexed).
* `~-N` expands to the string displayed by `dirs -N` (the *N*th directory from the right of the stack, zero-indexed).

## Assignment Context Parsing

Bash applies special parsing rules for tilde expansion during variable assignment. The expansion is triggered immediately following an unquoted `=` sign, and subsequently after any unquoted `:` characters within the assigned value. This is specifically designed to support `PATH`-like variable structures.

```bash theme={"dark"}

# Tilde expansion occurs after '=' and ':'
VAR=~/dir1:~+/dir2
```

## Quoting and Suppression

Tilde expansion is strictly dependent on the `~` character remaining unquoted. If the tilde, or any character within the tilde-prefix, is enclosed in single quotes, double quotes, or escaped with a backslash, the expansion is suppressed and the tilde is treated as a literal character.

```bash theme={"dark"}

# Expansion suppressed
\~
"~"
'~'
~"username"
```

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