The first sh.e.l.l variable of interest in this topic is called PS1, which simply stands for Prompt String 1 Prompt String 1. This special variable holds the contents of the command prompt that are displayed when bash bash is ready to accept commands (there is also a PS2 variable, used when is ready to accept commands (there is also a PS2 variable, used when bash bash needs multiple-line input to complete a command). You can easily display the contents of PS1, or any other sh.e.l.l variable, by using the needs multiple-line input to complete a command). You can easily display the contents of PS1, or any other sh.e.l.l variable, by using the echo echo command with the variable name preceded by the command with the variable name preceded by the $ $ symbol: symbol: $echo$PS1 $ The $ $ output tells us that PS1 contains the two characters output tells us that PS1 contains the two characters and and $ $. The backslash character tells the sh.e.l.l not to interpret the dollar symbol in any special way (that is, as a metacharacter metacharacter, described later in this section). A simple dollar sign was the default prompt for sh sh, but bash bash offers options to make the prompt much more informative. On your system, the default prompt stored in PS1 is probably something like: offers options to make the prompt much more informative. On your system, the default prompt stored in PS1 is probably something like: [[email protected]]$ Each of the characters preceded by backslashes has a special meaning to bash bash, whereas those without backslashes are interpreted literally. In this example, u u is replaced by the username, is replaced by the username, h h is replaced by the system"s hostname, is replaced by the system"s hostname, W W is replaced by the unqualified path (or basename) of the current working directory, and is replaced by the unqualified path (or basename) of the current working directory, and $ $ is replaced by a is replaced by a $ $ character (unless you are character (unless you are root root, in which case $ $ is replaced by is replaced by # #). This yields a prompt of the form: [[email protected]]$ How your prompt is formulated is really just a convenience and does not affect how the sh.e.l.l interprets your commands. However, adding information to the prompt, particularly regarding system, user, and directory location, can make life easier when hopping from system to system and logging in as multiple users (as yourself and root root, for example). See the online doc.u.mentation on bash for more information on customizing prompts, including many more options you can use to display system information in your prompt. for more information on customizing prompts, including many more options you can use to display system information in your prompt.

Another sh.e.l.l variable that is extremely important during interactive use is PATH PATH, which contains a list of all the directories that hold commands or other programs you are likely to execute. A default path is set up for you when bash bash starts. You may wish to modify the default to add other directories that hold programs you need to run. starts. You may wish to modify the default to add other directories that hold programs you need to run.

NoteEvery file in the Linux filesystem can be specified in terms of its location. The less less program, for example, is located in the directory program, for example, is located in the directory /usr/bin /usr/bin. Placing /usr/bin /usr/bin in your in your PATH PATH enables you to execute enables you to execute less less by simply typing by simply typing less less rather than the explicit rather than the explicit /usr/bin/less /usr/bin/less.Also be aware that "." (the current directory) is not included in the PATH PATH either implicitly (as it is in either implicitly (as it is in DOS DOS) or explicitly for security reasons. To execute a program named foo foo in the current directory, simply run in the current directory, simply run ./foo ./foo.

For bash bash to find and execute the command you enter at the prompt, the command must be one of the following: to find and execute the command you enter at the prompt, the command must be one of the following: A bash bash built-in built-in command that is part of command that is part of bash bash itself itself An executable program located in a directory listed in the PATH PATH variable variable An executable program whose filename you specify explicitly The sh.e.l.l holds PATH PATH and other variables for its own use. However, many of the sh.e.l.l"s variables are needed during the execution of programs launched from the sh.e.l.l (including other sh.e.l.ls). For these variables to be available, they must be and other variables for its own use. However, many of the sh.e.l.l"s variables are needed during the execution of programs launched from the sh.e.l.l (including other sh.e.l.ls). For these variables to be available, they must be exported exported, at which time they become environment variables environment variables. Environment variables are pa.s.sed on to programs and other sh.e.l.ls, and together they are said to form the environment environment in which the programs execute. in which the programs execute. PATH PATH is always made into an environment variable. Exporting a sh.e.l.l variable to turn it into an environment variable is done using the is always made into an environment variable. Exporting a sh.e.l.l variable to turn it into an environment variable is done using the export export command: command: $exportMYVAR Do not include a preceding dollar sign when defining or exporting a variable (because in this command, you don"t want the sh.e.l.l to expand the variable to its value). When a variable is exported to the environment, it is pa.s.sed into the environment of all child processes. That is, it will be available to all programs run by your sh.e.l.l. Here is an example that displays the difference between a sh.e.l.l variable and an environment variable: $echo$MYVAR No output is returned, because the variable has not been defined. We give it a value, and then echo its value: $MYVAR="h.e.l.lo"

$echo$MYVAR h.e.l.lo We"ve verified that the variable MYVAR MYVAR contains the value "h.e.l.lo". Now we sp.a.w.n a subsh.e.l.l (or child process) and check the value of this variable: contains the value "h.e.l.lo". Now we sp.a.w.n a subsh.e.l.l (or child process) and check the value of this variable: $bash $echo$MYVAR Typing bash bash sp.a.w.ned another copy of the bash sh.e.l.l. This child process is now our current environment, and as you can see from the blank line that ends the example, the variable sp.a.w.ned another copy of the bash sh.e.l.l. This child process is now our current environment, and as you can see from the blank line that ends the example, the variable MYVAR MYVAR is not defined here. If we return to our parent process and export the variable, it becomes an environment variable that can be accessed in all child processes: is not defined here. If we return to our parent process and export the variable, it becomes an environment variable that can be accessed in all child processes: $exit $exportMYVAR $bash $echo$MYVAR h.e.l.lo Typing the export export command without any arguments will display all of the exported environment variables available to your sh.e.l.l. The command without any arguments will display all of the exported environment variables available to your sh.e.l.l. The env env command will accomplish the same thing, just with slightly different output. command will accomplish the same thing, just with slightly different output.

Along the same lines are the bash built-in commands set set and and unset unset. The command set set with no arguments will display of list of currently set environment variables. The command with no arguments will display of list of currently set environment variables. The command unset unset will allow you to clear the value of an environment variable (a.s.suming it is not read-only). The will allow you to clear the value of an environment variable (a.s.suming it is not read-only). The set set command also gives you the ability to change the way command also gives you the ability to change the way bash bash behaves. The following are some examples of using behaves. The following are some examples of using set set to modify your interactive sh.e.l.l. to modify your interactive sh.e.l.l.

To change to vi vi-style editing mode: $setovi This example automatically marks variables that are modified or created for export to the environment of subsequent commands: $setoallexport To view the current settings for the variables that set set can modify, run can modify, run set o set o.

Entering commands at the command prompt Commands issued to the sh.e.l.l on a Linux system generally consist of four components: A valid command (a sh.e.l.l built-in, program, or script found among directories listed in the PATH PATH, or an explicitly defined program) Command options, usually preceded by a dash Arguments Line acceptance (i.e., pressing the Enter key), which we a.s.sume in the examples Each command has its own unique syntax, although most follow a fairly standard form. At minimum, a command command is necessary: is necessary: $ls This simple command lists the contents of the current working directory. It requires neither options nor arguments. Generally, options options are letters or words preceded by a single or double dash and are added after the command and separated from it by a s.p.a.ce: are letters or words preceded by a single or double dash and are added after the command and separated from it by a s.p.a.ce: $ls-l The -l -l option modifies the behavior of option modifies the behavior of ls ls by listing files in a longer, more detailed format. In most cases, single-dash options can be either combined or specified separately. To ill.u.s.trate this, consider these two equivalent commands: by listing files in a longer, more detailed format. In most cases, single-dash options can be either combined or specified separately. To ill.u.s.trate this, consider these two equivalent commands: $ls-l-a $ls-la By adding the -a -a option, option, ls ls displays files beginning with a dot (which it hides by default). Adding that option by specifying displays files beginning with a dot (which it hides by default). Adding that option by specifying -la -la yields the same result. Some commands offer alternative forms for the same option. In the preceding example, the yields the same result. Some commands offer alternative forms for the same option. In the preceding example, the -a -a option can be replaced with option can be replaced with --all --all: $ls-l--all These double-dash, full-word options are frequently found in programs from the GNU project. They cannot be combined like the single-dash options can. Both types of options can be freely intermixed. Although the longer GNU-style options require more typing, they are easier to remember and easier to read in scripts than the single-letter options.

Adding an argument argument further refines the command"s behavior: further refines the command"s behavior: $ls-l*.c Now the command will give a detailed listing only of C program source files, if any exist in the current working directory.

NoteUsing the asterisk in *.c *.c allows any file to match as long as it ends with a allows any file to match as long as it ends with a .c .c extension. This is known as extension. This is known as file globbing file globbing. More information on file globbing and using wildcards can be found later in this chapter.

Sometimes, options and arguments can be mixed in any order: $ls--all*.c-l In this case, ls ls was able to determine that was able to determine that -l -l is an option and not another file descriptor. is an option and not another file descriptor.

Some commands, such as tar tar and and ps ps, don"t require the dash preceding an option because at least one option is expected or required. To be specific, ps ps doesn"t require a dash when it is working like BSD"s version of doesn"t require a dash when it is working like BSD"s version of ps ps. Since the Linux version of ps ps is designed to be as compatible as possible with various other versions of is designed to be as compatible as possible with various other versions of ps ps, it sometimes does need a dash to distinguish between conflicting options. As an example, try ps -e ps -e and and ps e ps e. The first version invokes a Linux-specific option that shows everyone"s processes, not just your own. The second version invokes the original BSD option that shows the environment variables available to each of your commands.

Also, an option often instructs the command that the subsequent item on the command line is a specific argument. For example: $sortname_list

$sortk2name_list These commands invoke the sort sort command to sort the lines in the file command to sort the lines in the file name_list name_list. The first command just sorts beginning with the first character of each line, whereas the second version adds the options -k 2 -k 2. The -k -k option tells the command to break each line into fields (based on whites.p.a.ce) and to sort the lines on a particular field. This option requires a following option to indicate which field to sort on. In this case, we have told option tells the command to break each line into fields (based on whites.p.a.ce) and to sort the lines on a particular field. This option requires a following option to indicate which field to sort on. In this case, we have told sort sort to sort on the second field, which is useful if to sort on the second field, which is useful if name_list name_list contains people"s names in a "Joe Smith" format. contains people"s names in a "Joe Smith" format.

Just as any natural language contains exceptions and variations, so does the syntax used for GNU and Unix commands. You should have no trouble learning the essential syntax for the commands you need to use often. The capabilities of the command set offered on Linux are extensive, making it highly unlikely that you"ll memorize all of the command syntax you"ll ever need. Most system administrators are constantly learning about features they"ve never used in commands they use regularly. It is standard practice to regularly refer to the doc.u.mentation on commands you"re using, so feel free to explore and learn as you go.

Entering commands not in the PATH Occasionally, you will need to execute a command that is not in your path and not built into your sh.e.l.l. If this need arises often, it may be best to simply add the directory that contains the command to your path. However, there"s nothing wrong with explicitly specifying a command"s location and name completely. For example, the ls ls command is located in command is located in /bin /bin. This directory is most certainly in your PATH PATH variable (if not, it should be!), which allows you to enter the variable (if not, it should be!), which allows you to enter the ls ls command by itself on the command line: command by itself on the command line: $ls The sh.e.l.l looks for an executable file named ls ls in each successive directory listed in your in each successive directory listed in your PATH PATH variable and will execute the first one it finds. Specifying the literal pathname for the command eliminates the directory search and yields identical results: variable and will execute the first one it finds. Specifying the literal pathname for the command eliminates the directory search and yields identical results: $/bin/ls Any executable file on your system may be started in this way. However, it is important to remember that some programs may have requirements during execution about what is listed in your PATH PATH. A program can be launched normally but may fail if it is unable to find a required resource due to an incomplete PATH PATH.

Entering multiple-line commands interactively In addition to its interactive capabilities, the sh.e.l.l also has a complete programming language of its own. Many programming features can be very handy at the interactive command line as well. Looping constructs, including for for, until until, and while while, are often used this way. (Sh.e.l.l syntax is covered in more detail in Chapter13 Chapter13.) When you begin a command such as these, which normally spans multiple lines, bash bash prompts you for the subsequent lines until a valid command has been completed. The prompt you receive in this case is stored in sh.e.l.l variable PS2, which by default is prompts you for the subsequent lines until a valid command has been completed. The prompt you receive in this case is stored in sh.e.l.l variable PS2, which by default is > >. For example, if you wanted to repet.i.tively execute a series of commands each time with a different argument from a known series, you could enter the following: $var1=1 $var2=2 $var3=3 $echo$var1 1 $echo$var2 2 $echo$var2 3 Rather than entering each command manually, you can interactively use bash bash"s for for loop construct to do the work for you. Note that indented style, such as what you might use in traditional programming, isn"t necessary when working interactively with the sh.e.l.l: loop construct to do the work for you. Note that indented style, such as what you might use in traditional programming, isn"t necessary when working interactively with the sh.e.l.l: $forvarin$var1$var2$var3 >do >echo$var >done 1 2 3 You can also write this command on one line: $forvarin$var1$var2$var3;doecho$var;done 1 2 3 The semicolons are necessary to separate the variables from the built-in bash functions.

Entering command sequences There may be times when it is convenient to place multiple commands on a single line. Normally, bash bash a.s.sumes you have reached the end of a command (or the end of the first line of a multiple-line command) when you press Enter. To add more than one command to a single line, separate the commands and enter them sequentially with the a.s.sumes you have reached the end of a command (or the end of the first line of a multiple-line command) when you press Enter. To add more than one command to a single line, separate the commands and enter them sequentially with the command separator command separator, a semicolon. Using this syntax, the following commands: $ls $ps are, in essence, identical to and will yield the same result as the following single-line command that employs the command separator: $ls;psOn the ExamCommand syntax and the use of the command line are very important topics. Pay special attention to the use of options and arguments and how they are differentiated. Also be aware that some commands expect options to be preceded by a dash, whereas other commands do not. The LPI exams do not concentrate on command options, so don"t feel like you need to memorize every obscure option for every command before taking the exams.

Command History and Editing If you consider interaction with the sh.e.l.l as a kind of conversation, it"s a natural extension to refer back to things "mentioned" previously. You may type a long and complex command that you want to repeat, or perhaps you need to execute a command multiple times with slight variation.

If you work interactively with the original Bourne sh.e.l.l, maintaining such a "conversation" can be a bit difficult. Each repet.i.tive command must be entered explicitly, each mistake must be retyped, and if your commands scroll off the top of your screen, you have to recall them from memory. Modern sh.e.l.ls such as bash bash include a significant feature set called include a significant feature set called command history command history, expansion expansion, and editing editing. Using these capabilities, referring back to previous commands is painless, and your interactive sh.e.l.l session becomes much simpler and more effective.

The first part of this feature set is command history. When bash bash is run interactively, it provides access to a list of commands previously typed. The commands are stored in the history list is run interactively, it provides access to a list of commands previously typed. The commands are stored in the history list prior prior to any interpretation by the sh.e.l.l. That is, they are stored before wildcards are expanded or command subst.i.tutions are made. The history list is controlled by the to any interpretation by the sh.e.l.l. That is, they are stored before wildcards are expanded or command subst.i.tutions are made. The history list is controlled by the HISTSIZE HISTSIZE sh.e.l.l variable. By default, sh.e.l.l variable. By default, HISTSIZE HISTSIZE is set to 1,000 lines, but you can control that number by simply adjusting is set to 1,000 lines, but you can control that number by simply adjusting HISTSIZE HISTSIZE"s value. In addition to commands entered in your current bash bash session, commands from previous session, commands from previous bash bash sessions are stored by default in a file called sessions are stored by default in a file called ~/.bash_history ~/.bash_history (or the file named in the sh.e.l.l variable (or the file named in the sh.e.l.l variable HISTFILE HISTFILE).

NoteIf you use multiple sh.e.l.ls in a windowed environment (as just about everyone does), the last sh.e.l.l to exit will write its history to ~/.bash_history ~/.bash_history. For this reason you may wish to use one sh.e.l.l invocation for most of your work.

To view your command history, use the bash bash built-in built-in history history command. A line number will precede each command. This line number may be used in subsequent command. A line number will precede each command. This line number may be used in subsequent history expansion history expansion. History expansion uses either a line number from the history or a portion of a previous command to re-execute that command. History expansion also allows a fair degree of command editing using syntax you"ll find in the bash bash doc.u.mentation. doc.u.mentation. Table6-1 Table6-1 lists the basic history expansion designators. In each case, using the designator as a command causes a command from the history to be executed again. lists the basic history expansion designators. In each case, using the designator as a command causes a command from the history to be executed again.

Table6-1.Command history expansion designators

Designator Description !!

Spoken as bang-bang, this command refers to the most recent command. The exclamation point is often called bang bang on Linux and Unix systems. on Linux and Unix systems.

!n Refer to command n from the history. Use the from the history. Use the history history command to display these numbers. command to display these numbers.

!-n Refer to the current command minus n from the history. from the history.

!string Refer to the most recent command starting with string.

!?string Refer to the most recent command containing string.

^string1^string2 Quick subst.i.tution. Repeat the last command, replacing the first occurrence of string1 with with string2 string2.

While using history subst.i.tution can be useful for executing repet.i.tive commands, command history editing is much more interactive. To envision the concept of command history editing, think of your entire bash bash history (including that obtained from your history (including that obtained from your ~/.bash_history ~/.bash_history file) as the contents of an editor"s buffer. In this scenario, the current command prompt is the last line in an editing buffer, and all of the previous commands in your history lie above it. All of the typical editing features are available with command history editing, including movement within the "buffer," searching, cutting, pasting, and so on. Once you"re used to using the command history in an editing style, everything you"ve done on the command line becomes available as retrievable, reusable text for subsequent commands. The more familiar you become with this concept, the more useful it can be. file) as the contents of an editor"s buffer. In this scenario, the current command prompt is the last line in an editing buffer, and all of the previous commands in your history lie above it. All of the typical editing features are available with command history editing, including movement within the "buffer," searching, cutting, pasting, and so on. Once you"re used to using the command history in an editing style, everything you"ve done on the command line becomes available as retrievable, reusable text for subsequent commands. The more familiar you become with this concept, the more useful it can be.

By default, bash bash uses uses key bindings key bindings like those found in the Emacs editor for command history editing. (An editing style similar to the like those found in the Emacs editor for command history editing. (An editing style similar to the vi vi editor is also available.) If you"re familiar with Emacs, moving around in the command history will be familiar and very similar to working in an Emacs buffer. For example, the key command Ctrl-p (depicted as editor is also available.) If you"re familiar with Emacs, moving around in the command history will be familiar and very similar to working in an Emacs buffer. For example, the key command Ctrl-p (depicted as C-p C-p) will move up one line in your command history, displaying your previous command and placing the cursor at the end of it. This same function is also bound to the up-arrow key. The opposite function is bound to C-n C-n (and the down arrow). Together, these two key bindings allow you to examine your history line by line. You may re-execute any of the commands shown simply by pressing Enter when it is displayed. For the purposes of Exam 101, you"ll need to be familiar with this editing capability, but detailed knowledge is not required. (and the down arrow). Together, these two key bindings allow you to examine your history line by line. You may re-execute any of the commands shown simply by pressing Enter when it is displayed. For the purposes of Exam 101, you"ll need to be familiar with this editing capability, but detailed knowledge is not required. Table6-2 Table6-2 lists some of the common Emacs key bindings you may find useful in lists some of the common Emacs key bindings you may find useful in bash bash. Note that C- C- indicates the Ctrl key, and indicates the Ctrl key, and M- M- indicates the Meta key, which is usually Alt on PC keyboards (since PC keyboards do not actually have a Meta key). indicates the Meta key, which is usually Alt on PC keyboards (since PC keyboards do not actually have a Meta key).

NoteIn circ.u.mstances where the Alt key is not available, such as on a terminal, using the Meta key means pressing the Escape (Esc) key, releasing it, and then pressing the defined key. The Esc key is not a modifier, but applications will accept the Esc key sequence as equivalent to the Meta key.

Table6-2.Basic command history editing Emacs key bindings

Key Description C-p Previous line (also up arrow) C-n Next line (also down arrow) C-b Back one character (also left arrow) C-f Forward one character (also right arrow) C-a Beginning of line C-e End of line C-l Clear the screen, leaving the current line at the top of the screen M-< top="" of="" history="" m-=""> Bottom of history C-d Delete character from right C-k Delete (kill) text from cursor to end of line C-y Paste (yank) text previously cut (killed) M-d Delete (kill) word C-rtext Reverse search for text C-stext Forward search for text

Command subst.i.tution bash offers a handy ability to do offers a handy ability to do command subst.i.tution command subst.i.tution. This feature allows you to replace the result of a command with a script. For example, wherever $( $(command) is found, its output will be subst.i.tuted. This output could be a.s.signed to a variable, as in the system information returned by the command is found, its output will be subst.i.tuted. This output could be a.s.signed to a variable, as in the system information returned by the command uname a uname a: $SYSTEMSTRING=$(uname-a) $echo$SYSTEMSTRING Linuxlinuxpc.oreilly.com2.6.24.7-92.fc8#1SMPWedMay716:50:09 EDT2008i686athloni386GNU/Linux Another form of command subst.i.tution is " "command". The result is the same, except that the back quote back quote (or (or backtick backtick) syntax has some special rules regarding metacharacters that the $( $(command) syntax avoids. Refer to the syntax avoids. Refer to the bash bash manual at manual at for more information. for more information.

Applying commands recursively through a directory tree There are many times when it is necessary to execute commands recursively recursively. That is, you may need to repeat a command throughout all the branches of a directory tree. Recursive execution is very useful but also can be dangerous. It gives a single interactive command the power to operate over a much broader range of your system than your current directory, and the appropriate caution is necessary. Think twice before using these capabilities, particularly when operating as the superuser.

Some of the GNU commands on Linux systems have built-in recursive capabilities as an option. For example, chmod chmod modifies permissions on files in the current directory: modifies permissions on files in the current directory: $chmodg+w*.c In this example, all files with the .c .c extension in the current directory are given the group-write permission. However, there may be a number of directories and files in hierarchies that require this change. extension in the current directory are given the group-write permission. However, there may be a number of directories and files in hierarchies that require this change. chmod chmod contains the contains the -R -R option (note the uppercase option letter; you may also use option (note the uppercase option letter; you may also use --recursive --recursive), which instructs the command to operate not only on files and directories specified on the command line, but also on all files and directories contained beneath beneath the specified directories. For example, this command gives the group-write permission to all files in a source-code tree named the specified directories. For example, this command gives the group-write permission to all files in a source-code tree named /home/adam/src /home/adam/src: $chmod-Rg+w/home/adam/src Provided you have the correct privileges, this command will descend into each subdirectory in the src src directory and add the requested permission to each file and directory it finds. Other example commands with this ability include directory and add the requested permission to each file and directory it finds. Other example commands with this ability include cp cp (copy), (copy), ls ls (list files), and (list files), and rm rm (remove files). (remove files).

A more general approach to recursive execution through a directory is available by using the find find command. command. find find is inherently recursive and is intended to descend through directories executing commands or looking for files with certain attributes. At its simplest, is inherently recursive and is intended to descend through directories executing commands or looking for files with certain attributes. At its simplest, find find displays an entire directory hierarchy when you simply enter the command and provide a single argument of the target directory. If no options are given to displays an entire directory hierarchy when you simply enter the command and provide a single argument of the target directory. If no options are given to find find, it prints each file it finds, as if the option -print -print were specified: were specified: $find/home/adam/src ...filesanddirectoriesarelistedrecursively...

As an example of a more specific use, add the -name -name option to search the same directories for C files (this can be done recursively with the option to search the same directories for C files (this can be done recursively with the ls ls command as well): command as well): $find/home/adam/src-name"*.c"

....cfilesarelistedrecursively...

find also can be used to execute commands against specific files by using the also can be used to execute commands against specific files by using the -exec -exec option. The arguments following option. The arguments following -exec -exec are taken as a command to run on each are taken as a command to run on each find find match. They must be terminated with a semicolon ( match. They must be terminated with a semicolon (;), which needs to be escaped (;, for example) because it is a sh.e.l.l metacharacter. The string {} {} is replaced with the filename of the current match anywhere it is found in the command. is replaced with the filename of the current match anywhere it is found in the command.

To take the previous example a little further, rather than execute chmod chmod recursively against all files in the recursively against all files in the src src directory, directory, find find can execute it against the C files only, like this: can execute it against the C files only, like this: $find/home/adam/src-name"*.c"-execchmodg+w{}; The find find command is capable of much more than this simple example and can locate files with particular attributes such as dates, protections, file types, access times, and others. Although the syntax can be confusing, the results are worth some study of command is capable of much more than this simple example and can locate files with particular attributes such as dates, protections, file types, access times, and others. Although the syntax can be confusing, the results are worth some study of find find.

Manpages Traditional computer manuals covered everything from physical maintenance to programming libraries. Although the books were convenient, many users didn"t always want to dig through printed doc.u.mentation or carry it around. So, as s.p.a.ce became available, the man man ( (manual) command was created to put the books on the system, giving users immediate access to the information they needed in a searchable, quick-reference format.

There is a manpage manpage for most commands on your system. There are also manpages for important files, library functions, sh.e.l.ls, languages, devices, and other features. for most commands on your system. There are also manpages for important files, library functions, sh.e.l.ls, languages, devices, and other features. man man is to your system what a dictionary is to your written language. That is, nearly everything is defined in detail, but you probably need to know in advance just what you"re looking for. is to your system what a dictionary is to your written language. That is, nearly everything is defined in detail, but you probably need to know in advance just what you"re looking for.

Name man Syntax man[options][section]name Description Format and display system manual pages from section section on the topic of on the topic of name name. If section section is omitted, the first manpage found is displayed. is omitted, the first manpage found is displayed.

Frequently used options -a Normally, man man exits after displaying a single manpage. The exits after displaying a single manpage. The -a -a option instructs option instructs man man to display all manpages that match to display all manpages that match name name, in a sequential fashion.

-d Display debugging information.

-k Search for manpages containing a given string.

-w Print the locations of manpages instead of displaying them.

Example 1 View a manpage for mkfifo mkfifo: $manmkfifo ...

Results for the first manpage found are scrolled on the screen.

Example 2 Determine what manpages are available for mkfifo mkfifo: $man-wamkfifo /usr/share/man/man1/mkfifo.1 /usr/share/man/man3/mkfifo.3 This shows that two manpages are available, one in section 1 (mkfifo.1) of the manual and another in section 3 (mkfifo.3). See the next section for a description of manpage sections.

Example 3 Display the mkfifo mkfifo manpage from manual section 3: manpage from manual section 3: $man3mkfifo

Manual sections Manpages are grouped into sections sections, and there are times when you should know the appropriate section in which to search for an item. For example, if you were interested in the mkfifo mkfifo C-language function rather than the command, you must tell the C-language function rather than the command, you must tell the man man program to search the section on library functions (in this case, section 3, program to search the section on library functions (in this case, section 3, Linux Programmer"s Manual Linux Programmer"s Manual): $man3mkfifo An alternative would be to have the man man program search all manual sections: program search all manual sections: $man-amkfifo The first example returns the mkfifo(3) mkfifo(3) manpage regarding the library function. The second returns pages for both the command and the function. In this case, the pages are delivered separately; terminating the pager on the first manpage with Ctrl-C causes the second to be displayed. manpage regarding the library function. The second returns pages for both the command and the function. In this case, the pages are delivered separately; terminating the pager on the first manpage with Ctrl-C causes the second to be displayed.

Manual sections are detailed in Table6-3 Table6-3.

Table6-3.Man sections

Section Description 1 User programs 2 System calls 3 Library calls 4 Special files (usually found in /dev) 5 File formats 6 Games 7 Miscellaneous 8 System administration

NoteSome systems might also have sections 9 9, n n, and others, but only sections 1 1 through through 8 8 are defined by the FHS. are defined by the FHS.

The order in which man man searches the sections for manpages is controlled by the searches the sections for manpages is controlled by the MANSECT MANSECT environment variable. environment variable. MANSECT MANSECT contains a colon-separated list of section numbers. If it is not set, contains a colon-separated list of section numbers. If it is not set, man man (as of version 1.5k) behaves as if it were set to (as of version 1.5k) behaves as if it were set to 1:8:2:3:4:5:6:7:9:tcl:n:l:p:o 1:8:2:3:4:5:6:7:9:tcl:n:l:p:o.

Manpage format Most manpages are presented in a concise format with information grouped under well-known standard headings such as those shown in Table6-4 Table6-4. Other manpage headings depend on the context of the individual manpage.

Table6-4.Standard manpage headings

Heading Description Name The name of the item, along with a description Synopsis A complete description of syntax or usage Description A brief description of the item Options Detailed information on each command-line option (for commands) Return values Information on function return values (for programming references) See also A list of related items that may be helpful Bugs Descriptions of unusual program behavior or known defects Files A list of important files related to the item, such as configuration files Copying or copyright A description of how the item is to be distributed or protected Authors A list of those who are responsible for the item

man mechanics System manpages are stored mostly in /usr/share/man /usr/share/man, but may exist in other places as well. At any time, the manual pages available to the man man command are contained within directories configured in your command are contained within directories configured in your man man configuration file, configuration file, /etc/man.config /etc/man.config. This file contains directives to the man man, telling it where to search for pages (the MANPATH MANPATH directive), the paging program to use ( directive), the paging program to use (PAGER), and many others. This file essentially controls how man man works on your system. To observe this, use the debug ( works on your system. To observe this, use the debug (-d) option to man man to watch as it constructs a to watch as it constructs a manpath manpath (a directory search list) and prepares to display your selection: (a directory search list) and prepares to display your selection: $man-dmkfifo

Objective 2: Process Text Streams Using Filters Many of the commands on Linux systems are intended to be used as filters filters, meaning that multiple commands can be piped together to perform complex operations on text. Text fed into the command"s standard input or read from files is modified in some useful way and sent to standard output or to a new file, leaving the original source file unmodified. Multiple commands can be combined to produce text streams text streams, which modify text at each step. This section describes basic use and syntax for the filtering commands important for Exam 101. Refer to a Linux command reference for full details on each command and the many other available commands.

Name cat Syntax cutoptions[files]

Description Concatenate files and print on the standard output. Cat is often used as the first command in a text stream, as it simply sends the contents of a file (or multiple files) to the standard output.

Frequently used options -s Never output more than one single blank line.

-v Display nonprinting characters (these usually are not displayed).

-A Display nonprinting characters, display $ $ at the end of each line, and display Tab characters as at the end of each line, and display Tab characters as ^I ^I.

Example Send the contents of the file /etc/pa.s.swd /etc/pa.s.swd to the file to the file /tmp/pa.s.swd /tmp/pa.s.swd: $cat/etc/pa.s.swd>/tmp/pa.s.swd

Name cut Syntax cutoptions[files]

Description Cut out (that is, print) selected columns or fields from one or more files files. The source file is not changed. This is useful if you need quick access to a vertical slice of a file. By default, the slices are delimited by a Tab character.

Frequently used options -blist Print bytes in list list positions. positions.

-clist Print characters in list list columns. columns.

-ddelim Set field delimiter (default is ; ;).

-flist Print list list fields. fields.

Example Show usernames (in the first colon-delimited field) from /etc/pa.s.swd /etc/pa.s.swd: $cut-d:-f1/etc/pa.s.swd

Name expand Syntax expand[options][files]

Description Convert Tabs to s.p.a.ces. Sometimes the use of Tab characters can make output that is attractive on one output device look bad on another. This command eliminates Tabs and replaces them with the equivalent number of s.p.a.ces. By default, Tabs are a.s.sumed to be eight s.p.a.ces apart.

© 2024 www.topnovel.cc