Continue only when $CONFIG exists and is a regular file.
Which test condition is appropriate?
-f is true when the path exists and is a regular file; -d, -x, and -z test different properties.
Detailed explanation
[ -f "$CONFIG" ]Correct. [ -f path ] tests whether path is an existing regular file.
Correct. [ -f path ] tests whether path is an existing regular file.
[ -d "$CONFIG" ]Incorrect. -d tests for a directory.
Incorrect. -d tests for a directory.
[ -x "$CONFIG" ]Incorrect. -x tests executability, not regular-file type alone.
Incorrect. -x tests executability, not regular-file type alone.
[ -z "$CONFIG" ]Incorrect. -z tests whether a string has zero length.
Incorrect. -z tests whether a string has zero length.
Try it yourself
An example you can run in a temporary verification environment.
file=$(mktemp); if [ -f "$file" ]; then printf regular; fi; rm -f -- "$file"Expected result
regularKey points
- -f means regular file
- Quote path expansions
- -d means directory
Notes
- Environment: POSIX互換test / 一時ファイル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.