Skip to main content

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.

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 [[ ]].
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).
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).
Master Bash with Deep Grasping Methodology!Learn More