The Command Line Interface (CLI) is actually an interactive session with the shell application configured for your account. For Linux, the default shell is BASH (/bin/bash). The terminal emulator starts bash, as does the console login. The CLI is a user interface, just as the graphical window manager presents a user interface. In the CLI, you can actually enter every line that you see in a shell script, one line at a time. The difference is that a shell script provides you a simple way to repeat the same commands.
The string of characters that appear on the screen is called the prompt. For example,
is a prompt. The default format of the prompt varies between different distros of Linux. In the chapter Configuring the Environment, we will learn how to customize the prompt. The convention is to use the $ to represent the prompt for a normal user, and the # as the prompt for the root user.
With the CLI, you interact with the shell through commands. The general format of the command is:
command [options] [arguments]
Each command has a default behavior and options that alter its behavior. Although there are exceptions, options begin with a - (traditional Unix option indicator) or -- (GNU option indicator). For example, -h for help Unix style, or --help for GNU style. The command may also require arguments that tell the command about data on which it is to perform its magic.
Notice that there is a space between each element of the command line. This is called whitespace, and the default values are the space character, the tab character, and blank lines. It makes no difference how many whitespace characters that you enter. The shell simply skips past them to the first non-whitespace character. When you press the <enter> key, the shell processes the command. Later, we will learn how to enter blank lines without terminating the command.
The cat command is probably one of the simplest, but most useful commands in Linux. The purpose of the cat command is to concatenate two or more files into a single output stream. Following is the syntax for the cat command:
cat [options] [files]
The following example illustrates the intended purpose of the cat command.
$ cat /proc/version /etc/motd Linux version 3.1.0-1.2-desktop (geeko@buildhost) (gcc version 4.6.2 (SUSE Linux) ) #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) Have a lot of fun...
The -n option for the cat command numbers each line, as shown below:
$ cat -n /proc/version /etc/motd 1 Linux version 3.1.0-1.2-desktop (geeko@buildhost) (gcc version 4.6.2 (SUSE Linux) ) #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) 2 Have a lot of fun...
You can also use the cat command to display a single:
$ cat /proc/version Linux version 3.1.0-1.2-desktop (geeko@buildhost) (gcc version 4.6.2 (SUSE Linux) ) #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0)
As mentioned in the section on Linux Architecture, a command does not directly access a file. Rather, the command issues system calls to the kernel, and the kernel returns the information requested. The /proc is not a real disk file system. The kernel makes it look like one, which makes it possible to retrieve kernel data through regular system calls. The kernel drivers that access kernel data only respond to a small set of system calls. Due it simplicity, the cat command is often used to display the information from these pseudo files.
The results of the cat command are displayed on the terminal. This is the default standard output (stdout). The I/O Redirection section shows how to redirect the output to another file or device. Commands act like a filter. They take input, massage the input data, to produce output. The cat command looks for its input from the arguments provided. If there are no arguments, as it is optional, the cat command looks for its input from Standard Input (stdin). The default input device is the keyboard. Thus, you could run the cat command with no options or arguments, as shown below:
$ cat this is an example of keyboard input this is an example of keyboard input another line another line <CTRL>D
From the output, we learn that the cat command process a file one line at a time. Every time you press the enter key, the cat command process the line and sends it to standard output. This is the normal behavior for commands that process text files. They read a line, process a line, and send the results to standard output. Since the cat command thinks it is reading a file, entering <CTRL>D, on a separate line, signals the cat command that it has reached the end of the file. There are a number of signals that can be sent from the keyboard, as shown in the following table:
|<CTRL>D||eof||Signals end-of-file for terminate input|
|<backspace>||erase||Erases the last character typed|
|<CTRL>W||werase||Erase the last word typed|
|<CTRL>U||kill||Erase the current line|
|<CTRL>S||stop||Stop the output, but still displays what has been sent to the kernel for output|
|<CTRL>Q||start||Start the output after stopping it|
|<CTRL>Z||susp||Suspend execution of the command (this is part of the shell jobs feature, and is covered in the Processes versus Jobs chapter)|
|<CTRL>C||intr||Send an interrupt signal (ANSI standard), which terminates the command being executed (process can ignore this signal)|
|<CTRL>\||quit||Send a quit signal (POSIX standard), which terminates the command being executed (process can ignore this signal)|
Signals are a way for one process (such as the keyboard driver) to communicate to another process (such as the process being executed). A process is an application being executed. The process can choose to ignore the signal, or act on it. The keyboard character that generates the signal is part of the system configuration. You can verify that your system uses the same conventions by typing the following command:
$ stty -a speed 38400 baud; rows 36; columns 125; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff -iuclc -ixany -imaxbel iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
You can ignore most of the output, as we are only concerned with keyboard actions that send signals to a process. In this course, we are not going to cover changing the keyboard driver. The ability to configure every input device, under every supported architecture, is what make Linux portable across so may platforms.