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 -f operator is a unary file test operator in Bash used within conditional expressions to determine if a specified path exists and is a regular file. It evaluates to true (returns an exit status of 0) only if the target is a standard data file, explicitly excluding directories, block special files, character special files, named pipes (FIFOs), and sockets.
test -f <filepath>
[ -f <filepath> ]
[[ -f <filepath> ]]

Technical Mechanics

  • Exit Status: The operator yields an exit status of 0 (true) if the condition is met. It yields 1 (false) if the path does not exist, or if the path exists but points to an inode that is not a regular file.
  • Symlink Dereferencing: The -f operator follows (dereferences) symbolic links. If <filepath> is a symlink, the operator evaluates the ultimate target of the link. It returns 0 if the symlink resolves to a regular file, and 1 if it resolves to a directory, another non-regular file type, or is a broken (dangling) link.
  • Command Context:
    • When used with test or [ (the POSIX-compliant test command), the <filepath> operand is subject to word splitting and pathname expansion. If <filepath> contains spaces, it must be quoted (e.g., [ -f "$FILE" ]) to prevent syntax errors.
    • When used with [[ (the Bash extended test keyword), word splitting and pathname expansion are suppressed, making quoting optional but recommended for consistency.
  • Permissions: The operator only checks the file type and existence in the filesystem metadata (via the stat() system call). It does not evaluate read, write, or execute permissions for the current user, though the user must have execute (search) permissions on all parent directories in the path to successfully resolve the file.
Master Bash with Deep Grasping Methodology!Learn More