In a UNIX-like OS, executing command foo from /home/bar displays bash: foo: command not found.
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
In a UNIX-like OS, executing command foo from /home/bar displays bash: foo: command not found. Which command resolves this by adding the directory to the execution path?
Show answer & explanation
The command not found error occurs because the shell cannot locate the executable foo in any directory listed in the PATH environment variable. Option A correctly exports an updated PATH that includes /home/bar at the beginning, allowing the shell to find and execute foo. Options B, C, and D are incorrect: B modifies PWD (working directory), not the search path; C removes all permissions making the file unexecutable; D changes ownership, which doesn't resolve the path issue.
Step-by-step Derivation:
Step 1: Understand the problem. The shell searches for executables only in directories listed in the PATH variable. Step 2: Analyze each option:
- A:
export PATH=/home/bar:$PATHprepends/home/barto the existing PATH, making the shell search there forfoo. - B:
export PWD=/home/baronly changes the printed working directory variable; it doesn't affect command search. - C:
chmod 000 fooremoves all permissions (read, write, execute); this makes the file unexecutable, worsening the problem. - D:
chown root foochanges file ownership; this doesn't affect whether the directory is in PATH. Step 3: Option A is the correct solution because it modifies the PATH variable to include the directory wherefooresides.