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

The `-d` operator is a unary file test operator in Bash used to determine whether a specified file path exists and is a directory. It performs a `stat` system call on the provided path, evaluating the file mode metadata to return a standard POSIX exit status.

## Syntax

The operator requires a single string argument representing the path. It can be used with the standard `test` builtin, the POSIX test command `[ ]`, or the Bash extended test keyword `[[ ]]`.

```bash theme={"dark"}
test -d "$path"
[ -d "$path" ]
[[ -d "$path" ]]
```

## Evaluation and Exit Status

The operator yields the following exit codes:

* **`0` (True):** The path exists in the filesystem **and** its file type is a directory.
* **`1` (False):** The path does not exist, **or** the path exists but is a different file type (e.g., regular file, character device, block device, socket, or FIFO).

## Symbolic Link Resolution

The `-d` operator dereferences (follows) symbolic links. If the evaluated path is a symlink, the operator checks the file type of the link's ultimate target.

* If the symlink resolves to a directory, `-d` returns `0`.
* If the symlink resolves to a non-directory, or if it is a broken (dangling) symlink, `-d` returns `1`.

## Technical Nuances

* **Quoting and Word Splitting:** When using the POSIX `[ ]` construct, the path variable must be double-quoted (`"$path"`). Failing to quote introduces critical evaluation pitfalls:
  * **Empty Variables:** If `$path` is empty and unquoted, `[ -d $path ]` expands to `[ -d ]`. Under POSIX `test` rules for a single argument, this evaluates whether the literal string `"-d"` is non-null. Since `"-d"` is not empty, the command silently evaluates to `0` (True). This is a major pitfall, as an empty path check will falsely report that a directory exists.
  * **Variables with Spaces:** If `$path` contains spaces, Bash performs word splitting. A variable with exactly one space (e.g., `path="a b"`) expands to `[ -d a b ]`, resulting in the error `bash: [: a: binary operator expected`. A variable with two or more spaces (e.g., `path="a b c"`) expands to `[ -d a b c ]`, resulting in `bash: [: too many arguments`.
* **Extended Test Construct:** The Bash-specific `[[ ]]` construct suppresses word splitting and pathname expansion. `[[ -d $path ]]` safely handles unquoted empty strings (evaluating to `1`) and spaces without syntax errors, though quoting remains a strict best practice.
* **Permissions:** The operator strictly evaluates the file type. It does not verify whether the executing user has read (`-r`), write (`-w`), or execute (`-x`) permissions on the directory itself. However, standard filesystem traversal rules apply; the user must have execute permissions on all parent directories in the path to successfully perform the underlying `stat` operation.
* **Explicit Empty Strings:** Passing an explicitly quoted empty string (e.g., `[ -d "" ]` or `[[ -d "" ]]`) always evaluates to `1` (False).

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