Working with Commands
Chapter 05
So far, we have witnessed a sequence of enigmatic directives, each accompanied by its own perplexing choices and parameters. In this chapter, our aim is to unveil some of that enigma and even forge our own directives. The directives presented in this chapter include:
type– Indicate how a command name is interpretedwhich– Display which executable program will be executedhelp– Get help for shell builtinsman– Display a command's manual pageapropos– Display a list of appropriate commandsinfo– Display a command's info entrywhatis– Display a very brief description of a commandalias– Create an alias for a command
What Exactly Are Commands?
A command can be one of four different things:
An executable program falls under the same category as those files we encountered in
/usr/bin. Within this classification, programs can exist as compiled binaries, such as those coded in C and C++, or as programs written in scripting languages like shell, perl, python, ruby, and more.A command integrated directly into the shell itself is referred to as a shell builtin. bash, specifically, provides support for several such commands internally. An illustration of a shell builtin is the
cdcommand, which is directly embedded within the shell.A shell function is a small-scale shell script that is integrated into the environment. In upcoming chapters, we will delve into configuring the environment and crafting shell functions. However, for the time being, it is important to acknowledge their existence.
An alias refers to custom commands that we can personally define, constructed using other existing commands.
Identifying Commands
Having a clear understanding of the specific type of command being utilized is frequently beneficial, and Linux offers a few methods to determine this information.
type – Display A Command's Type
type – Display A Command's TypeThe type command, which is a shell builtin, showcases the type of command that will be executed by the shell when provided with a specific command name. Its usage follows this pattern:
where command represents the name of the command you wish to inspect. Here are a few examples:
Below are the outcomes displayed for three distinct commands. Observe the particular case of ls (taken from an Ubuntu system) and how it is actually an alias for the ls command with the additional --color=auto option. This clarifies why the output of ls appears in color!
which – Display An Executable's Location
which – Display An Executable's LocationOccasionally, multiple versions of an executable program may be installed on a system. While this occurrence is infrequent in desktop systems, it is not uncommon in large servers. To ascertain the precise location of a particular executable, the which command is employed:
The which command exclusively functions for executable programs and does not apply to builtins or aliases that serve as substitutes for actual executable programs. If which is used on a shell builtin, such as cd, either no response or an error message will be returned.
Getting A Command's Documentation
Armed with this understanding of what constitutes a command, we can proceed to explore the available documentation for each type of command.
help – Get Help For Shell Builtins
help – Get Help For Shell BuiltinsThe bash shell provides an intrinsic help feature accessible for each of the shell builtins. To utilize it, simply enter help followed by the name of the shell builtin. Here's an example:
Regarding notation, when you encounter square brackets in a command's syntax description, they signify optional components. On the other hand, a vertical bar character denotes mutually exclusive items. In the case of the aforementioned cd command:
The provided notation indicates that the cd command can be followed by either the optional -L or -P options. Additionally, if the -P option is specified, the -e option may also be included, followed by the optional argument dir.
Although the help output for the cd commands is concise and accurate, it does not serve as a tutorial. Moreover, it may reference several aspects that have not yet been discussed. However, there is no need to worry as we will cover those topics in due course.
--help – Display Usage Information
--help – Display Usage InformationNumerous executable programs offer a --help option, which presents a description of the supported syntax and available options for the command. Here's an example:
While certain programs do not support the --help option, it is still worth attempting. In many cases, it will generate an error message that discloses the same usage information.
man – Display A Program's Manual Page
man – Display A Program's Manual PageThe majority of executable programs designed for command line usage come with a formal documentation known as a manual or man page. To view these pages, a dedicated paging program called man is utilized. Here's how to use it:
where program is the name of the command to view.
Man pages exhibit slight variations in format, but typically encompass a title, a synopsis outlining the command's syntax, a description clarifying the command's purpose, and a listing with explanations of each available option. It is important to note that man pages usually lack examples and are primarily designed as a reference rather than a tutorial. Let's try viewing the man page for the ls command as an illustration:
In the majority of Linux systems, the man command utilizes less to present the manual page, allowing you to employ all the familiar less commands while viewing the page.
The "manual" displayed by the man command is organized into sections, encompassing not only user commands but also system administration commands, programming interfaces, file formats, and more. The table below elucidates the structure of the manual:
1
User commands
2
Programming interfaces kernel system calls
3
Programming interfaces to the C library
4
Special files such as device nodes and drivers
5
File formats
6
Games and amusements such as screen savers
7
Miscellaneous
8
System administration commands
There are instances when we specifically need to search within a particular section of the manual to find the information we seek. This is particularly relevant if we are searching for a file format that shares its name with a command. By omitting the section number, we will typically retrieve the first matching instance, usually in section 1. To specify a section number, we utilize the man command in the following manner:
For example:
This will display the man page describing the file format of the /etc/passwd file.
apropos – Display Appropriate Commands
apropos – Display Appropriate CommandsFurthermore, it is possible to conduct a search within the list of man pages to identify potential matches based on a search term. Although rudimentary, this feature can be quite useful. Here's an example of performing a search for man pages using the search term "disk":
In the output, the first field of each line represents the name of the man page, while the second field indicates the corresponding section. It is important to note that the man command with the -k option fulfills the same purpose as the apropos command.
whatis – Display A Very Brief Description Of A Command
whatis – Display A Very Brief Description Of A CommandThe whatis program exhibits the name and provides a brief one-line description of a man page that matches a given keyword:
info – Display A Program's Info Entry
info – Display A Program's Info EntryThe GNU Project offers an alternative to man pages for their programs known as info. Info pages are presented using a reader program aptly named info, and they feature hyperlinks similar to web pages. Here is an example:
The info program is designed to read info files that possess a tree structure, consisting of individual nodes, each dedicated to a specific topic. Info files incorporate hyperlinks that enable navigation from one node to another. Hyperlinks are indicated by a leading asterisk and can be activated by placing the cursor on them and pressing the enter key.
To launch the info program, simply enter info followed by the optional name of a program. Presented below is a table of commands utilized to navigate and control the reader while viewing an info page:
?
Display command help
PgUp or Backspace
Display previous page
PgDn or Space
Display next page
n
Next - Display the next node
p
Previous - Display the previous node
u
Up - Display the parent node of the currently displayed node, usually a menu.
Enter
Follow the hyperlink at the cursor location
q
Quit
The majority of command line programs we have discussed thus far are included in the GNU Project's coreutils package. Therefore, if you type:
will display a menu page with hyperlinks to each program contained in the coreutils package.
README And Other Program Documentation Files
On your system, numerous software packages are installed, and their corresponding documentation files are located in the /usr/share/doc directory. Most of these files are stored in plain text format, accessible for viewing using the less command. However, some files are in HTML format and can be opened with a web browser. It is possible to come across files with a ".gz" extension, indicating that they have been compressed using the gzip compression program. For viewing the contents of such gzip-compressed text files, the gzip package includes a specialized version of less called zless.
Creating Your Own Commands With alias
aliasNow, let's embark on our first programming endeavor! We will construct our own command using the alias command. However, before we begin, let's unveil a handy command line technique. You can execute multiple commands on a single line by separating each command with a semicolon character. Here's how it functions:
Here's the example we will use:
As observed, we have merged three commands into a single line. Initially, we switch to the /usr directory, followed by listing its contents. Finally, we return to the original directory using cd - to ensure we end up where we began. Now, let's transform this sequence into a new command using the alia feature. The first step is to come up with a suitable name for our command. Let's try test. However, before proceeding, it is wise to determine whether the name "test" is already in use. We can verify this by utilizing the type command once again:
Oops! The name test is already taken. Let's try foo:
Great! foo is not taken. So let's create our alias:
Notice the structure of this command:
Following the alias command, we provide a name for the alias, immediately followed (without any whitespace) by an equals sign. Subsequently, we enclose the assigned meaning within quotes. Once the alias is defined, it can be employed anywhere the shell anticipates a command. Let's put it into practice:
We can also use the type command again to see our alias:
To remove an alias, the unalias command is used, like so:
Although we intentionally refrained from assigning our alias a name that matches an existing command, it is not uncommon to do so. This practice is frequently employed to apply commonly desired options to every invocation of a widely used command. As we saw earlier, the ls command is frequently aliased to include color support:
To view all the aliases defined in the environment, you can execute the alias command without any arguments. Below are some of the aliases that come pre-defined on an Ubuntu system. Take a moment to decipher their functionalities:
There is a minor inconvenience when defining aliases directly on the command line—they disappear once your shell session concludes. However, in a subsequent chapter, we will explore how to add our own aliases to the files responsible for setting up the environment each time we log on. For now, let's appreciate the fact that we have taken our initial, albeit small, stride into the realm of shell programming!
Summary
Now that we have acquired the knowledge of locating command documentation, I encourage you to explore the documentation for all the commands we have encountered thus far. Delve into the details of additional options that are available and feel free to experiment with them!
Last updated