Part I
Exam LX0-103
Chapter 1
Exploring Linux Command-Line Tools
THE FOLLOWING EXAM OBJECTIVES ARE COVERED IN THIS CHAPTER:
103.1 Work on the command line
103.2 Process text streams using filters
103.4 Use streams, pipes, and redirects
103.7 Search text files using regular expressions
Linux borrows heavily from Unix, and Unix began as a text-based operating system (OS). Unix and Linux retain much of this heritage, which means to understand how to use and, especially administer Linux, you must understand at least the basics of its command-line tools. Using command-line tools requires the use of a shell. A shell is a program that accepts and interprets text-mode commands and provides an interface to the system.
This chapter begins with basic shell information, including the various shell programs available and the procedures for using them. From there, this chapter covers streams, pipes, and redirection, which you can use to move input and output between programs or between files and programs. These techniques are frequently combined with text processing using filters– commands you can use to manipulate text without the help of a conventional text editor. Sometimes you must manipulate text in an abstract way, using codes to represent several different types of text. This chapter, therefore, covers this topic as well.
Understanding Command-Line Basics
Before you do anything else with Linux, you should understand how to use a Linux shell. The shell allows you to enter commands as needed. Which commands can be entered depends on which shell program is running. Several of the available shell programs are briefly described.
In using shell commands, you should also understand shell environment variables, which are placeholders for data that may be useful to many programs. Finally, it is helpful to know how to get help with the shell commands you're trying to use.
Exploring Your Linux Shell Options
The shell to be used for entering commands is configured for each individual user, and Linux provides a range of available shells. A complete shell list would be quite long, but the following shells are among the more common choices:
bash
The GNU Bourne Again Shell (bash
) is based on the earlier Bourne shell for Unix but extends it in several ways. In Linux, bash
is the most common default shell for user accounts, and it's the one emphasized in this book and on the exam.
Sh
The Bourne shell upon which bash
is based goes by the name sh
. It's not often used in Linux and the sh
command is often a pointer to the bash
shell or other shells.
tcsh
This shell is based on the earlier C shell (csh
). It's a fairly popular shell in some circles, but no major Linux distributions make it the default shell. Although it's similar to bash
in many respects, some operational details differ. For instance, you don't assign environment variables the same way in tcsh
as in bash
.
csh
The original C shell isn't used much on Linux, but if a user is familiar with csh
, tcsh
makes a good substitute.
ksh
The Korn shell (ksh
) was designed to take the best features of the Bourne shell and the C shell and extend them. It has a small but dedicated following among Linux users.
zsh
The Z shell (zsh
) takes shell evolution further than the Korn shell, incorporating features from earlier shells and adding still more.
In addition to these shells, dozens more obscure ones are available. In Linux, most users run bash
because it is the most popular shell. Some other OSs use csh
or tcsh
as the default, so if your users have backgrounds on non-Linux Unix-like OSs, they may be more familiar with these other shells. You can change a user's default shell by editing their account, as described in Chapter 7, “Administering the System.”
Be aware that there are two types of default shells. The default interactive shell is the shell program a user uses to enter commands, run programs from the command line, run shell scripts, and so on. The other default shell type is a default system shell. The default system shell is used by the Linux system to run system shell scripts, typically at startup.
The file /bin/sh
is a pointer to the system's default system shell – normally /bin/bash
for Linux. However, be aware that, on some distributions, the /bin/sh
points to a different shell. For example, on Ubuntu, /bin/sh
points to the dash shell, /bin/dash
.
Using a Shell
Linux shell use is fairly straightforward for anybody who's used a text-mode OS before: You type a command, possibly including options to it, and the computer executes the command. For the most part, Linux commands are external – that is, they're programs that are separate from the shell.
A few commands are internal to the shell, though, and knowing the distinction can be important. You should also know some of the tricks that can make using the command shell easier – how to have the computer complete a long command or filename, retrieve a command you've recently run, or edit a command you've recently used (or haven't yet fully entered).
Starting a Shell
If you log into Linux using a text-mode login screen, you have logged into a virtual console terminal and, most likely, you'll be dropped directly into your default shell. The shell program is what presents the prompt and accepts subsequent commands.
If you log into Linux using a graphical user interface (GUI) login screen, you'll have to start a terminal emulator manually in order to reach your default shell. Some GUIs provide a menu option, such as xterm
or terminal
, to start a terminal emulator program. These programs enable you to run text-mode programs within Linux, and by default they come up running your shell. If you can't find such a menu option, look for a menu option that enables you to run an arbitrary command. Select it, and type xterm or konsole as the command name. This will launch a terminal emulator program that will run a shell.
Once you start a terminal or log into a virtual console terminal, the shell will provide you with a prompt for entering commands. Remember that the shell is a program providing you with an interface to the Linux system.
A good first command to try, uname
, will show what operating system is being run:
That's not too interesting. You can find out additional information by tacking on the -a
option to the command. Be sure to include the necessary space between the command and the option:
The uname – a
command provides a lot more information, including the current Linux kernel being used (2.6.32
) as well as the system's hostname (server01.class.com
). The uname
command is an external command. The shell also provides internal commands. It's important to know the difference between the two command types, as explained in the next section.
Using Internal and External Commands
Internal commands are, as you might expect, built into the shell program. Thus they are also called built-in commands. Most shells offer a similar set of internal commands, but shell-to-shell differences do exist. Internal commands that you're likely to use enable you to perform some common