Linux and Command-Line Basics
An executable script run.sh in the current directory gives "command not found" when run by typing "run.sh", but runs when typed as "./run.sh". Which of the following is the reason?
- Arun.sh has no execute permission, and only when ./ is put in front can it be run without permission
- BA command name without a slash is searched for only in the directories of the PATH environment variable, which generally does not include the current directory
- CThe #!/bin/bash on the first line of run.sh (the shebang) is read only when it is run with ./ in front
Show answer and explanation
Answer: B. A command name without a slash is searched for only in the directories of the PATH environment variable, which generally does not include the current directory
When the shell receives a command name without a slash, it searches in order through the directories listed, separated by colons, in the PATH environment variable (/usr/local/bin:/usr/bin and so on) and runs what it finds. The current directory is generally not put in PATH, so that a malicious program with the same name placed elsewhere is not run by mistake. Writing a path containing a slash, such as ./run.sh, runs that file directly without searching PATH. The #!/bin/bash on the first line (the shebang) specifies the interpreter that runs the script, whichever way it is started.
- A. Without execute permission, ./run.sh also fails with "Permission denied".
- B. Correct. The current directory is not in PATH, so its location must be given with ./.
- C. Whether the script is found through PATH or started with ./, the kernel reads the shebang and runs the script with the specified interpreter.