Shell Basics

Now that you’ve seen what the shell is, and how to interact with it, let’s take a look at some of the shell features. This section walks through some of the features available in most Linux shells.

Commands

As mentioned earlier, the Linux shell allows you to perform two types of functions—run a built-in command and launch an external program. The external programs that you can launch must be accessible from the Linux system—either installed directly on a hard drive or removable media device or available on a network shared drive.

If you’re ever in doubt about a program, you can use the type shell built-in program. The type program provides quick information about a command or application, such as whether or not it’s a shell builtin command or an external program:

Line 1: dollar type c d. Line 2: c d is a shell builtin. Line 3: dollar.

That’s a handy way to get basic information about a file.

If you launch a text-based application, the virtual terminal from where you launched the program becomes the interface with the program. Output from the program appears on the virtual terminal monitor, and input for the program is taken from the keyboard. These are called the standard output and standard input for the shell. You can run multiple programs from different virtual terminals, each one using the input and output for its respective virtual terminal. Later on you’ll see how to redirect input and output with other sources, which can come in handy when you run programs in background mode.

You can launch a graphics-based application from the CLI, but the results depend on how the CLI was started. When you launch a graphics-based application, it looks for a graphical desktop environment to run in. In a text-mode console environment, no graphical desktop environment is available, so usually the program will terminate and complain. If you’re using a terminal emulation package from inside a graphical desktop, the graphical program you launch will appear in the graphical desktop, just as if you had launched it directly from the desktop.

Getting Help

The Linux shell has several layers of help available to help make things easier for you. The first layer is the help command. The help command displays information about the built-in shell commands. You can get a list of the available built-in commands in your shell by typing help itself at the command prompt, as shown in FIGURE 6-3.

An output shows a list of the available built in commands using the help command.

FIGURE 6-3 The help command output.

Description

You can get help on a specific built-in command by entering the command name after the help command in the CLI:

Help space c p.

The next layer of help is the man program. The man program displays canned documentation provided by the application developers, as long as the documentation is installed on your Linux system. There’s an official format for man pages, which helps you quickly find the information you’re looking for. The information in the man page is divided into separate headings. While there are some standard headings defined, there’s no requirement for each man page to include information in all of the headings. You may see any of these common headings in a man page:

  • Name—Provides the programs name, and any aliases that it may have

  • Synopsis—Provides a quick review of what the program does

  • Description—Provides a longer explanation of how the program works

  • Options—Describes any command line options available

  • Arguments—Describes any option arguments required

  • Files—Lists the files required for the program, including configuration files

  • Examples—Shows an example of how to use the program

  • See Also—Refers to similar programs

  • History—Provides details on when the program was created and the different versions

  • Copyright—Lists the copyright status of the program

  • Bugs—Lists any known bugs

The man pages are also organized into separate sections, numbered from 1 through 8. Each section stores man pages for different types of programs. The main sections to know are section 1, which stores user-level program pages, section 2 for system calls, section 3 for library functions, and section 8 for administrator-level program pages. To specify a section, just list it in the man command:

Dollar space man space 1 space grep.

A command related to the man command is apropos. This fancy-named command looks up the specified term in all of the man pages and returns the man pages that reference the term.

The third layer of help available is the info program. The info program is the replacement for the man program, and has advanced features, such as linked pages. It displays information in a tree-like structure, allowing you to peruse through separate branches in the information tree. Each article can have links to related info pages for other commands, similar to browsing webpages. Implementation of info pages has been somewhat slow, so you may not find any information in the info pages for some applications. FIGURE 6-4 shows the example of the info page for the Linux grep command.

A screenshot shows the info page for the Linux grep command.

FIGURE 6-4 The info page for the Linux grep command.

Description

Shell Features

A few features that are built into the shell can help out with your command line operations. One is the history file. The history file keeps track of the commands you enter into the shell.

You can recover, modify, and use commands from the history file using the arrow keys. The Up arrow key retrieves the most recently used command from the history file. Hitting the Up arrow key again retrieves the second-most recently used command. If you continue hitting the Up arrow key, the shell continues retrieving commands from the history file in reverse order from when you entered them. Likewise, you can use the Down arrow to go back up through the history file list.

Once you enter a command or retrieve a previous command in the command line, you can modify the command using several different key combinations. Numerous editing commands are available in bash; TABLE 6-3 shows the more popular ones to remember:

TABLE 6-3 Shell command line editing commands.

COMMANDDESCRIPTION
Left arrowMove the cursor to the left in the command text
Right arrowMove the cursor to the right in the command text
Ctrl+aMove the cursor to the beginning of the command text
Ctrl+eMove the cursor to the end of the command text
DeleteRemove the character at the cursor location
BackspaceRemove the character before the cursor location
Ctrl+tTranspose two characters
Ctrl+kRemove (kill) the characters from the cursor position to the end of the command text into a buffer
Ctrl+yYank the characters from the buffer back into the command text

You can also jump directly to a specific previously entered command if you know what position it has in the history file using the history command. Just specify the command location as a number after the history command. For example, to retrieve the command you ran three commands ago, use:

Dollar space history space 3.

Finally, command completion is another handy shell feature. It searches for commands that match the text that you’re already typed in the command prompt interface. For example, if you’re not sure about the spelling of a command or program, you can type in a few letters and then just hit the Tab key. The shell searches the matching commands and programs and displays them, allowing you to select the command you want to use.

Running Multiple Commands

One exciting feature of the Linux command line is that you can enter multiple commands on the same command line and Linux will process them all! Just place semicolons between each command you enter:

Semicolon is entered between multiple commands.
Description

The Linux shell runs the first command (date) and displays the output, then it runs the second command (who) and displays the output from that command, immediately following the output from the first command. While this may seem trivial, that is the basis of how shell scripts work and can be a very powerful tool to have!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.111