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 -r operator is a unary file test operator used within Bash conditional expressions to evaluate file existence and read permissions. It returns an exit status of 0 (true) if the specified file exists and the executing process has read access to it, based on the effective user ID. Otherwise, it returns 1 (false).
[ -r FILE ]
[[ -r FILE ]]
test -r FILE

Evaluation Mechanics

  • Permission Resolution: The operator evaluates the standard POSIX file permissions (owner, group, other) and Access Control Lists (ACLs) against the effective user ID (EUID) and effective group ID (EGID) of the process executing the Bash script.
  • Symbolic Links: The -r operator dereferences symbolic links. If the target FILE is a symlink, the operator evaluates the existence and read permissions of the resolved target file, not the link itself.
  • Directories: In Unix-like systems, directories are treated as files. The -r operator will return true for a directory if the executing user has read permissions for it (which permits reading the directory’s contents/listing files).
  • Non-existent Files: If the file does not exist in the filesystem, the operator short-circuits and evaluates to false without throwing an error.

Syntax Contexts

The operator functions identically across the three primary Bash conditional constructs, though the parsing engine differs:
  1. test built-in: The POSIX-compliant command-line utility.
test -r /path/to/file
  1. [ ] (Single bracket): The POSIX-compliant syntactic sugar for the test command. Requires spaces around the brackets and operator.
[ -r /path/to/file ]
  1. [[ ]] (Double bracket): The Bash-specific extended test keyword. It handles word splitting and pathname expansion more safely than single brackets.
[[ -r $filename ]]

Disambiguation: read -r

While -r is a unary operator in the context of conditional tests, it is also frequently encountered as an option (flag) for the read built-in command (read -r). In the context of read, -r is not an operator; it is a flag that disables backslash escaping, forcing the command to treat backslashes as literal characters rather than escape sequences.
Master Bash with Deep Grasping Methodology!Learn More