In UNIX-like OS, when trying to execute a command called foo in the /home/bar directory,...
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
In UNIX-like OS, when trying to execute a command called foo in the /home/bar directory, the error -bash: foo: command not found was displayed. Which command is used to solve this error?
Show answer & explanation
The PATH environment variable determines which directories the shell searches for executable commands. When bash cannot find a command, it means the directory containing that executable is not in PATH. Option A adds /home/bar to the front of PATH, allowing the shell to locate the foo executable. Options B, C, and D modify unrelated variables: PWD tracks the current working directory, MANPATH is for manual pages, and SHLVL tracks shell nesting depth—none solve the command lookup issue.
Step-by-step Derivation:
- The error '-bash: foo: command not found' means the shell cannot locate the
fooexecutable in any directory listed in thePATHvariable. - The
PATHvariable is a colon-separated list of directories the shell searches when resolving commands. - To make
fooexecutable from anywhere, we must add/home/bartoPATH. export PATH=/home/bar:$PATHprepends/home/barto the existingPATH, preserving existing system directories while adding the new one.- This allows the shell to find and execute
foofrom/home/bar.