Running Commands from the Shell
computer software
Running Commands from the Shell Running Commands from the Shell
Running Commands from the Shell   Home | Site Map | About Us | Products | Services | News | Contact Us | Links Running Commands from the Shell
Running Commands from the Shell Running Commands from the Shell

Copyright © 2009

Linux® Bible


In this example, I was formatting a huge document and wanted to know how long it would take.
The first command (
date
) showed the date and time before the formatting started. The
troff
command formatted the document and then piped the output to the printer. When the formatting
was done, the date and time was printed again (so I knew how long the
troff
command took to
complete).
Another useful command to add to the end of a long command line is the
mail
command. You
could add
mail -s "Finished the long command" chris@example·com
to the end of a
command line. Then, for example, a mail message is sent to the user you choose after the com-
mand completes.
Background Commands
Some commands can take a while to complete. Sometimes you may not want to tie up your shell
waiting for a command to finish. In those cases, you can have the commands run in the back-
ground by using the ampersand (
&
).
Text formatting commands (such as
nroff
and
troff
, described earlier) are examples of com-
mands that are often run in the background to format a large document. You also might want to
create your own shell scripts that run in the background to check continuously for certain events
to occur, such as the hard disk filling up or particular users logging in.
Here is an example of a command being run in the background:
$ troff -me verylargedocument | lpr &
Other ways to manage background and foreground processes are described in the section
"Managing Background and Foreground Processes" later in this chapter.
Expanding Commands
With command substitution, you can have the output of a command interpreted by the shell
instead of by the command itself. In this way, you can have the standard output of a command
become an argument for another command. The two forms of command substitution are
$(command)
and
`command`
(backticks, not single quotes).
The command in this case can include options, metacharacters, and arguments. Here is an example
of using command substitution:
$ vi $(find /home | grep xyzzy)
In this example, the command substitution is done before the
vi
command is run. First, the
find
command starts at the
/home
directory and prints out all files and directories below that point in
the file system. The output is piped to the
grep
command, which filters out all files except for
those that include the string
xyzzy
in the filename. Finally, the
vi
command opens all filenames
for editing (one at a time) that include
xyzzy
.
This particular example is useful if you want to edit a file for which you know the name but not
the location. As long as the string is uncommon, you can find and open every instance of a filename
55
Running Commands from the Shell
2