- What Exactly Are Commands?
- Identifying Commands
- Getting A Command’s Documentation
- Creating Our Own Commands With alias Command
Up to this point, we have seen a series of mysterious commands, each with its own options and arguments. In this chapter, we will attempt to remove some of that mystery and even create our own commands. This chapter will introduce the following commands:
type- Indicate how a command name is interpreted
which- Display which executable program will be executed
help- Get help for shell builtins
man- Display a command’s manual page
apropos- Display a list of appropriate commands
info- Display a command’s info page
whatis- Display one-line manual page descriptions
alias- Create an alias for a command
What Exactly Are Commands?
A command can be one of four different things:
- An executable program. It’s like all those files we saw in /usr/bin. Within this category, programs can be compiled binaries such as programs written in C and C++, or programs written in scripting languages such as the shell, Perl, Python, Ruby, and so on.
- A command built into the shell itself. bash supports a number of commands internally called shell builtins. The
cdcommand, for example, is a shell builtin.
- A shell function. Shell functions are miniature shell scripts incorporated into the environment.
- An alias. Aliases are commands that we can define ourselves, built from other commands.
It is often useful to know exactly which of the four kinds of commands is being used, and Linux provides a couple of ways to find out.
type command is a shell builtin that displays the kind of command the shell will execute, given a particular command name.
[user@linux ~]$ type type type is a shell builtin [user@linux ~]$ type ls ls is aliased to `ls --color=tty' [user@linux ~]$ type cp cp is /bin/cp
To determine the exact location of a given executable, the
which command is used.
[user@linux ~]$ which ls /bin/ls
Getting A Command’s Documentation
With this knowledge of what a command is, we can now search for the documentation available for each kind of command.
bash has a built-in help facility available for each of the shell builtins. To use it, type
help followed by the name of the shell builtin.
[user@linux ~]$ help cd cd: cd [-L|[-P [-e]] [-@]] [dir] Change the shell working directory. Change the current directory to DIR. The default DIR is the value of the HOME shell variable. ...
Many executable programs support a
--help option that displays a description of the command’s supported syntax and options.
[user@linux ~]$ mkdir --help Usage: mkdir [OPTION] DIRECTORY... Create the DIRECTORY(ies), if they do not already exist. -Z, --context=CONTEXT (SELinux) set security context to CONTEXT Mandatory arguments to long options are mandatory for short options too. ...
Most executable programs intended for command line use provide a formal piece of documentation called a manual or man page. A special paging program called
man is used to view them.
[user@linux ~]$ man ls
It is also possible to search the list of man pages for possible matches based on a search term. It’s crude but sometimes helpful. Note that the
man command with the
-k option performs the same function as
[user@linux ~]$ apropos partition addpart (8) - simple wrapper around the "add partition" ioctl all-swaps (7) - event signalling that all swap partitions have been ac... cfdisk (8) - display or manipulate disk partition table cgdisk (8) - Curses-based GUID partition table (GPT) manipulator delpart (8) - simple wrapper around the "del partition" ioctl
whatis program displays the name and a one-line description of a man page matching a specified keyword.
[user@linux ~]$ whatis ls ls (1) - list directory contents
The GNU Project provides an alternative to man pages for their programs, called
info. Info manuals are displayed with a reader program named, appropriately enough, info. Info pages are hyperlinked much like web pages. The info program reads info files, which are tree structured into individual nodes, each containing a single topic. Info files contain hyperlinks that can move you from node to node.
README And Other Program Documentation Files
Many software packages installed on your system have documentation files residing in the /usr/share/doc directory. Most of these are stored in ordinary text format and can be viewed with the
less command. Some of the files are in HTML format and can be viewed with a web browser. We may encounter some files ending with a .gz extension. This indicates that they have been compressed with the gzip compression program. The gzip package includes a special version of
zless that will display the contents of gzip-compressed text files.
Creating Our Own Commands With alias Command
Now let’s create our own command. We will create a command of our own using the
alias command. First we change the current working directory to /usr, then list the directory, and finally return to the original directory (by using
cd -) so we end up where we started. Now let’s turn this sequence into a new command using
[user@linux ~]$ alis boo='cd /usr; ls; cd -'
To remove an alias, the
unalias command is used.
[user@linux ~]$ unalias boo
Note: To see all the aliases defined in the environment, use the
alias command without arguments.
There is one tiny problem with defining aliases on the command line. They vanish when your shell session ends. In the next chapters, we will see how to add our own aliases to the files that establish the environment each time we log on, but for now, enjoy the fact that we have taken our first.
Now that we know how to find the documentation for commands, look up the documentation for all the commands we have encountered so far. Study their additional options and try them on your own.