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?

  1. Arun.sh has no execute permission, and only when ./ is put in front can it be run without permission
  2. 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
  3. 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.
Practice this topic Back to topic

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Linux and Command-Line Basics questions

  1. When the commands above are run in order, which of the following is the pair of outputs of the two bash -c commands?
  2. Which of the following is the main function of a Linux package management system (apt on Debian-based systems, dnf on Red Hat-based systems and so on)?
  3. On a Linux system using systemd, which of the following is the difference between "systemctl start license.service" and "systemctl enable license.service" for the service license.service?
  4. The line "30 2 * * 1 /home/alice/backup.sh" was registered in cron. Which of the following is when backup.sh is run?
  5. A Linux server warns that disk space is running low. Which of the following is the correct pair of a command to check usage and free space for each file system and a command to total the space used by the files in a directory?