Bash Command Precedence Explained
Unlike some other operating systems, Linux does not, by default, check the current directory for an executable before searching the path. It immediately begins searching the path, and executables in the current directory are run only if they are in the PATH variable or you give their absolute (such as /home/scriptx.sh) or relative (for example, ./scriptx.sh) address.
The path directory order is important. Directories are checked from left to right. So, in this example, if there is a command called foo located in both the /bin and /usr/bin directories, the one in /bin is executed. To have the other foo command run, you either type the full path to the command or change your PATH variable. (Changing your PATH and adding directories to it are described later in this chapter.) Not all the commands that you run are located in directories in your PATH variable.
Some commands are built into the shell. Other commands can be overridden by creating aliases that define any commands and options that you want the command to run. There are also ways of defining a function that consists of a stored series of commands.
Here is the order in which the shell checks for the commands you type:
1. Aliases. Names set by the alias command that represent a particular command and a set of options. Type alias to see what aliases are set. Often, aliases enable you to define a short name for a long, complicated command.
2. Shell reserved word. Words reserved by the shell for special use. Many of these are words that you would use in programming-type functions, such as do, while, case, and else.
3. Function. A set of commands that are executed together within the current shell.
4. Built-in command. A command built into the shell. As a result, there is no representation of the command in the fi le system. Some of the most common commands you will use are shell built-in commands, such as cd (to change directories), echo (to output text to the screen), exit (to exit from a shell), fg (to bring a command running in the background to the foreground), history (to see a list of commands that were previously run), pwd (to list the present working directory), set (to set shell options), and type (to show the location of a command).
5. File system command. This command is stored in and executed from the computer’s file system. (These are the commands that are indicated by the value of the PATH variable.)
To find out where a particular command is taken from, you can use the type command.
(If you are using a shell other than bash, use which command instead.) For example, to fi nd out where the bash shell command is located, type the following:
$ type bash
bash is /bin/bash
If a command is not in your PATH variable, you can use the locate command to try to find it. Using locate, you can search any part of the system that is accessible to you (some files are only accessible to the root user).
$ locate lsof /usr/include/kde4/ksysguard/lsof.h /usr/lib64/liblsofui.so /usr/lib64/liblsofui.so.4 /usr/lib64/liblsofui.so.4.3.0 /usr/lib64/kde4/plugins/designer/ksysguardlsofwidgets.so /usr/sbin/lsof /usr/share/doc/lsof-4.82
In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.