PATH is /opt/tools/bin:/usr/bin:/bin.
Executable analyze files exist in both /opt/tools/bin and /usr/bin.
Which path is normally selected when analyze is entered?
The shell searches PATH directories from left to right and uses the first executable match it finds.
Detailed explanation
/opt/tools/bin/analyzeCorrect. /opt/tools/bin is the first PATH directory containing an executable named analyze.
Correct. /opt/tools/bin is the first PATH directory containing an executable named analyze.
/usr/bin/analyzeIncorrect. A standard directory does not outrank an earlier PATH entry.
Incorrect. A standard directory does not outrank an earlier PATH entry.
/bin/analyzeIncorrect. /bin is searched after the two earlier entries and is not stated to contain the file.
Incorrect. /bin is searched after the two earlier entries and is not stated to contain the file.
Incorrect. Normal command resolution selects one first match rather than running both.
Incorrect. Normal command resolution selects one first match rather than running both.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); mkdir -p "$tmp/first" "$tmp/second"; printf '#!/bin/sh\necho first\n' >"$tmp/first/analyze"; printf '#!/bin/sh\necho second\n' >"$tmp/second/analyze"; chmod +x "$tmp/first/analyze" "$tmp/second/analyze"; PATH="$tmp/first:$tmp/second:$PATH" analyze; rm -rf "$tmp"Expected result
firstKey points
- PATH is searched left to right
- The first match is executed
- Order defines priority
Notes
- Environment: Bash 5.x / 一時実行ファイル
- 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.