Linux First Steps
computer software
Linux First Steps Linux First Steps
Linux First Steps   Home | Site Map | About Us | Products | Services | News | Contact Us | Links Linux First Steps
Linux First Steps Linux First Steps

Copyright © 2009

Linux® Bible


Some people disable the history feature for the root user by setting the HISTFILE to
/dev/null
or simply leaving HISTSIZE blank. This prevents information about the
root user's activities from potentially being exploited. If you are an administrative user with root
privileges, you may want to consider emptying your file upon exiting as well, for the same reasons.
Connecting and Expanding Commands
A truly powerful feature of the shell is the capability to redirect the input and output of commands
to and from other commands and files. To allow commands to be strung together, the shell uses
metacharacters. As noted earlier, a metacharacter is a typed character that has special meaning to
the shell for connecting commands or requesting expansion.
Piping Commands
The pipe (
|
) metacharacter connects the output from one command to the input of another com-
mand. This lets you have one command work on some data, and then have the next command
deal with the results. Here is an example of a command line that includes pipes:
$ cat /etc/password | sort | less
This command lists the contents of the
/etc/password
file and pipes the output to the
sort
command. The
sort
command takes the usernames that begin each line of the
/etc/password
file, sorts them alphabetically, and pipes the output to the
less
command (to page through the
output).
Pipes are an excellent illustration of how UNIX, the predecessor of Linux, was created as an operat-
ing system made up of building blocks. A standard practice in UNIX was to connect utilities in dif-
ferent ways to get different jobs done. For example, before the days of graphical word processors,
users created plain-text files that included macros to indicate formatting. To see how the document
really appeared, they would use a command such as the following:
$ gunzip < /usr/share/man/man1/grep.1.gz | nroff -c -man | less
In this example, the contents of the
grep
man page (
grep.1.gz
) are directed to the
gunzip
com-
mand to be unzipped. The output from
gunzip
is piped to the
nroff
command to format the
man page using the manual macro (-
man
). The output is piped to the
less
command to display
the output. Because the file being displayed is in plain text, you could have substituted any num-
ber of options to work with the text before displaying it. You could sort the contents, change or
delete some of the content, or bring in text from other documents. The key is that, instead of all
those features being in one program, you get results from piping and redirecting input and output
between multiple commands.
Sequential Commands
Sometimes you may want a sequence of commands to run, with one command completing before
the next command begins. You can do this by typing several commands on the same command
line and separating them with semicolons (
;
):
$ date ; troff -me verylargedocument | lpr ; date
NOTE
NOTE
54
Linux First Steps
Part I