shell command

zhaozj2021-02-12  155

The shell is an interface between the user and the Linux operating system. There are a variety of shells in Linux, in which Bash is used by default. This chapter tells the working principle of Shell, the type of shell, the general operation of Shell and the characteristics of Bash. What is the shell of the shelllinux system as the housing of the operating system, providing users with an interface to use the operating system. It is a collective language, command interpreter, and quote from programming language. The shell is an interface program between the user and the Linux kernel. If the Linux kernel imagines into a sphere, the shell is the outer layer around the core. When commands from the shell or other programs to Linux, the core will make a corresponding response. Shell is a command language interpreter that has its own built-in shell command set, and the shell can be called by other applications in the system. The commands entered in the prompt are explained by the shell and then pass it to the Linux core. There are some commands, such as changing the working directory command CD, is included inside the shell. There are also some commands, such as copying commands CP and mobile command RM, which are separate programs existing in a directory in the file system. For users, don't have to care about an order is built within the shell or a separate program. Shell first checks if the command is internal command. If it is not checked if it is an application (here the application can be Linux itself, such as LS and RM, can also be a commercial program, such as XV, or freedom Software, such as Emacs. The shell is then looking for these applications in the search path (the search path is a list of directories that can find the executable program). If the typed command is not an internal command and does not find this executable file in the path, an error message will be displayed. If the command can be successfully found, the internal command or application will be decomposed into a system call and pass it to the Linux kernel. Another important feature of shell is that it itself is an interpreted programming language. The shell programming language supports most program elements that can be seen in advanced languages, such as functions, variables, arrays, and program control structures. The shell programming language is easy to learn, and any commands that can be typed in the prompt can be placed in an executable shell program. When the normal user is successfully logged in, a program called shell is executed. It is the shell process that provides a command line prompt. As the default value (the default shell of the TurboLinux system is Bash), the "$" prompt is used for ordinary users, and the "#" is prompt to the superuser (root). Once the Shell prompt appears, you can type the parameters you need to command the command name and command. The shell will execute these commands. If a command takes a long time to run, or generate a lot of output on the screen, you can issue an interrupt signal from the keyboard to issue an interrupt signal to interrupt it (before normal end, abort its execution). When the user is ready to end the login dialog process, you can type the logout command, the exit command, or the file end value (Press CTRL D implementation) to end the login. Let's intercrium how shell works. $ make Workmake: *** no rule to make target 'Work'. Stop. $ Note: make is the name of a command in the system, followed by command parameters. After receiving this command, the shell executes it. In this example, since the input command parameters are incorrect, the system returns the information to stop the execution of the command. In the example, the shell looks for a program called Make and performs it as a parameter with Work. Make is a program that is often used to compile a large program, which is compiled as a target as a goal.

In "make Work", the target of Make is Work. Because Make could not find a target named by Work, it gives an error message indicates that the run fails, and the user returns to the system prompt. In addition, the user will give an error message if the user can't find the program named by the command line. For example, if the user type: $ myProgbash: myprog: command not found, the user gets an error message that does not find the command. After the user is knocking the wrong command, the system generally gives such an error message. There are many types in the shell's type Linux, with the most commonly used Bourne Shell (SHELL (CSH) and Korn Shell (KSH). The three shells have advantages and disadvantages. The Bourne Shell is the starting shell for UNIX and can be used on each UNIX. Bourne Shell is quite excellent in shell programming, but doing less than other shells in handling the user's interaction. The default shell of the Linux operating system is Bourne Again Shell, which is an extension of Bourne Shell, is referred to as Bash, compatible with Bourne Shell, and increases many features on the basis of the Bourne Shell. BASH is placed in / bin / bash, it has many features that provide functions such as command completion, command editing, and command history tables, which also contains the advantages in many c shells and Korn shells, flexible and powerful programming Interface, and there is a very friendly user interface. C shell is a shell that is more programmed than the Bourne Shell, which is similar to the C language. Linux provides TCSH for people who like to use C shell. TCSH is an extension version of C shell. TCSH includes command line editing, programmable words, spell correction, historical command replacement, job control, and similar C language syntax, not only compatible with the Bash Shell, but also provides more prompt parameters than Bash Shell . The Korn Shell combines the advantages of C shell and Bourne Shell and is fully compatible with the Bourne Shell. The Linux system provides PDKSH (Extension of KSH) that supports task control, which can be hang on the command line, the background execution, wake up or terminate the program. Linux does not have a cold other shell users, as well as some popular shells such as ASH, ZSH, etc. Every shell has its use, some shells are patented, and some can be obtained from the Internet or other sources. To decide which shell to use, just read the online help of the various shells, and try it. Users determine which shell to use by the / etc / passwd file when logging in to Linux. For example: # fgrep lisa / etc / passwdlisa: x: 500: 500: TurboLinux User: / Home / Lisa: / bin / Bashshell is column each end (/ bin / bash). Since Bash is the default shell on Linux, this chapter mainly introduces Bash and its related knowledge. When the shell command command line C logs in to the Linux system, you can see a shell prompt and identify the beginning of the command line. Users can enter any commands and parameters later. For example: $ DATE II 11 23 01:34:58 CST 1999 When the user logs in, I actually entered the shell, which follows a certain syntax to explain the input command and transmit it to the system.

The first word entered in the command line must be a command name, the second word is the option or parameter of the command, and each word in the command line must be separated by spaces or TAB, the format is as follows: $ comMand option arguments1. The options and parameter options are code including one or more letters, with a minus sign in front (decimal, Linux to distinguish options and parameters), options can be used to change the type of action executed by the command. For example: $ lsmotd passwd $ This is an LS command without options, which can list all files in the current directory, only listing the names of each file without displaying other more information. $ LS -LTOTAL 2-RW-R - R - 2 WZH Book 22 APR 20 20:37 MOTD-RW-R - R - 2 WZH Book 796 APR 20 20:37 Passwd $ Add -L Option, Will list a line of lines for each file, such as data size and data last modified. Most commands are designed to accept parameters. The parameter is one or more words typed after the option in the command line, for example: $ ls -l text-rw-r - r - 2 WZH Book 22 APR 20 20:37 MOTD-RW-R - R - 2 WZH Book 796 APR 20 20:37 Passwd $ will display all files and their information in the Text directory. Some commands, such as the LS can be used, and some commands may require some minimum number of parameters. For example, the CP command requires at least two parameters, and if the number of parameters does not match the command, the shell will give an error message. For example: $ cp -i mydata newdata Note: The options in the command line are first in parameter input. 2. The command line characteristic command line is actually a text buffer that can be edited. You can edit the input text before pressing Enter. For example, using the Backspace key to delete the characters that just typed, you can delete it, you can also insert characters, so that when you enter commands, especially complex commands, if you type errors, you don't need to re-enter the entire command, just use the editing operation, Correct the error. Using the up arrow can re-display the just executed command, use this feature to repeat the previously performed command without retrofitting the command. Bash saves a list of previously typed commands, which is called command history table. Press the arrow to display each command on the command line. Similarly, pressing the arrow to move down in the command list, which can be displayed on the command line in the command line, and the user can modify and execute these commands. This feature will be discussed in detail in Section 10.4. In a command line, you can also place multiple commands, separated by the semicolons. For example: $ LS -F; CP -I MyData NewData can also enter a command in several command lines, with a backslash to continue to the next line. The CP command on the $ cp -ImyDataNewData is entered in the three rows. The starting two lines ends with the backslash, and the three lines are used as a command line. In addition to using normal characters in the special character shell in the shell, special characters with special meaning and feature can also be used. At the time of use, you should pay attention to its special meaning and scope of action. These special characters are described below. 1. Wildcard wildcard is used for pattern matching, such as file name matching, road scripping search, string lookup, etc. Commonly used wildcards are * ,? and character sequences in square brackets []. The user can include these wildcards in the file name as the command parameter, constitute a so-called "mode string" and perform mode matches during the execution process.

* Represents any string (length can be inequal), for example: "f *" matches any string of the head head. However, it should be noted that the dots (.) And slash (/) in the path before the file must be explicitly matched. For example, "*" cannot match .file, and ". *" Can match .file. ? Represents any individual characters. [] Represents a specified character range, as long as the [] location in the file name is within the [] location specified in [], then this file name matches this mode string. The character range in square brackets can be composed of a direct-given character, or consist of a starting character representing a defined range, a termination character, and an intermediate hyphen (-). For example, F [A- D] is the same as F [ABCD]. The shell will use all the file names that match the mode strings specified in the command line to form the final command, and then execute this command. Here we give the table 10-1 to explain the specific meaning of these wildcards. Table 10-1 Wildcard Meaning Example Mode Strings * The name of all files in the current directory. * Text * The name of the file containing text in all file names in the current directory. [AB-DM] * The name of all files starting with A, B, C, D, and M. [AB-DM]? All the names of all the files starting with A, B, C, D, M and the back of one character with a character. / usr / bin / ?? Directory / USR / bIN name of all files named two characters. It is particularly important to note that even characters "-" are only valid in square brackets, indicating the character range, as in square brackets, it is a normal character. And * and? Only in square brackets are wildcards, if they appear within square brackets, they have lost the ability of wildcards and become ordinary characters. For example, only one pair of brackets in the pattern "- a [*?] Abc" are wildcards, * and? All ordinary characters, so it matches the string that can only be - a * ABC and - a? ABC. Finally, some issues that need to be careful when using wildcards. Since * ,? and [] have a relatively special meaning for the shell, these characters should not appear in the normal file name. Especially in the directory name, do not appear, otherwise shell matches the endless recursive. It is also a point to pay attention to: If there is no file name that matches the specified mode string in the directory, the shell will use this mode string itself as a parameter to pass the relevant command. This may be the reason why a special character appears in the command. 2. Quotation marks are divided into three types: single quotes, double quotes and reverse numbers. * Single quotes' characters from single quotes appear as ordinary characters. After the special characters are enclosed in single quotes, they will lose their original meaning, but only as a normal character interpretation. For example: $ String = '$ PATH' $ Echo $ String $ PATH $ visible $ Maintaining its own meaning, as a normal character appears. * Double quotation marks "The characters enclosed by double quotes, except for $,, ', and" these characters are still special characters and retain their special features, and the rest are treated as ordinary characters. For $, it is used to replace this variable and $ with the value of the variables, which is the escape character. It tells the shell not to specially handle the characters behind it, only as a normal character can. It can be imagined that only four characters $,, 'and "itself are required in the double quotes," it is not added in front of it, and the shell matches it. "

For example, we assume that the value of PATH is: / usr / bin: / bin, enter the following command: $ teststring = "$ PATH /" $ PATH "$ Echo $ teststring.: / Usr / bin: / bin" $ PATH $ Readers can try itselves before the second double quotation. * Anti-quotation number `reverse quotation number (`) This character is generally located on the upper left corner of the keyboard, not confused with single quotation marks ('). The string of the reincarnation is interpreted as a command line by the shell. When executed, the shell first executes the command line, and replaces the entire retrieval (including two reverse number) in its standard output result. For example: $ PWD / HOME / XYZ $ String = "CURRENT DIRECTORY IS` PWD` "$ ECHO $ ​​STRINGCURENT DIRECTOUR IS / HOME / XYZ $ SHELL When performing the echo command, first perform the command PWD in the` PWD`, and will output the result / home / xyz replaces the `pwd. This part, the last output of the replacement is replaced. This function using the reverse number can be commanded, that is, assigns the execution result enclosed in the rejoice to the specified variable. For example: $ today = `date` $ echo Today IS $ TODAYTODAY IS MON APR 15 16:20:13 CST 1999 $ DIRK can also be nested. However, it is important to note that the reverse number of the inner layer must be essentially used in the nested use. For example: $ abc = `echo the number of users is` WHO | WC-L`` $ Echo $ Abcthe Number of Users IS 5 $ The special character of the shell can also use the shell's special characters in the command line between the reverse number. Shell is the result of the `` in the command, it actually wants to execute the command specified in ``. At the time of execution, the special characters, such as $, ", etc. will have a special meaning, and` `included can be any legal shell command, such as: $ lsnote readme.txt NOTIX.DIR $ TESTSTRING = "` Echo $ HOME `` LS [NN] * `" $ Echo $ teststring / home / yXZ Note Notice $ Other situations, readers can try themselves. 1. Note in the shell programming is often necessary for certain texts Comment to increase the readability of the program. During the front of the shell, the head is commented on the front of the character "#". There are also special characters such as: used for input / output redirection and pipeline <,>, < <, >> and |; Execute the Background Command &; Command Execution Operator & || and the {} indicating the command group will be introduced in the following sections.

Standard Input / Output and Redirect 1. Standard Input and Output We know that three standard files are automatically opened when performing a shell command line, ie standard input file (stdin), usually corresponding to the keyboard; standard output file (stdout ) And standard error output file (stderr), both files correspond to the screen of the terminal. The process will get input data from the standard input file, and output the normal output data to the standard output file, and send the error message to the standard error file. In the case of a CAT command, the function of the CAT command is to read the data from the file given by the command line and send them directly to the standard output. If you use the following command: $ Cat Config will display the contents of the file config to the screen. However, if there is no parameters in the command line of the CAT, it will read the data from the standard input and send it to the standard output. For example: $ CATHELLO WorldHello WorldByeBye $ user enters each line to be output from the CAT command to the screen. Another example, the command sort reads the file body by line (when the file name is given in the command line, it is sorted from the standard input), and the result is sent to the standard output. The following example is to read a purchase order from the standard input and sort it. $ Sortbanascarrotsapples AppleSbanAascarrots $ At this time we got sorted procurement list on the screen. The following issues exist directly using standard input / output files: When entering data from terminal input, the user can only use the data that is high in half a day. The next time you want to use this data to re-enter. And when entering the terminal, it is not very convenient to modify the input. The information output to the terminal screen can only be seen. We cannot output more processing on this, such as further processing as an input of another command, and so on. In order to solve the above problem, the Linux system is input, and the output is introduced into two other mechanisms, namely the input / output redirection, and the pipe. 2. Enter the redirected input redirection refers to the standard input of the command (or executable program) to the specified file. That is, the input may not be from the keyboard, and from a specified file. Therefore, the input redirection is mainly used to change the input source of a command, especially the input source that requires a large number of inputs. For example, the command WC statistics specifies the number of rows, words, and characters of the file. If you type only on the command line: $ WCWC will wait for the user to tell you what, then the shell is like a dead, all the text typed from the keyboard appears on the screen, but there is no result until it presses < CTRL D>, WC will write command results on the screen. If a file name is given as a parameter of the WC command, as shown in the following example, the WC will return the number of rows, words, and character number included in the file. $ wc / etc / passwd20 23 726 / etc / passwd $ another method to pass the / etc / passwd file content to the WC command is to redirect the input of the WC. The general form of the input redirection is: command

$ WC << Delim> this text forms the content> of the here document, Which> Continues Until the end of> text delimter> Delim4 17 98 In the << Operator, any character can be used as a separator before the beginning of the text, This example uses DELIM as a separator. The body of the Here document has continued until the other separator is met. The second separator should appear at the beginning of the new line. At this time, the body of the Here document (not including the start and end separator) will reordforward to the command WC as its standard input. Since most commands specify the file name of the input file on the command line in the form of parameters, the input redirection is not often used. Despite this, when a command that does not accept file name as an input parameter is used, the input content is used to solve the problem with an input redirection. 1. Output Redirection Output Redirects Refiguring the standard output or standard error output of the command (or executable program) to the specified file. Thus, the output of the command is not displayed on the screen, but writes to the specified file. Output redirection is more common than input redirects, and this function can be used in many cases. For example, if there is a lot of output, you can't display it on the screen, then redirect to a file, then use the text editor to open this file, you can view the output information; if you want to save the output of a command This method can also be used. Also, the output redirection can be used to use the output of a command as an input to another command (there is a simpler method, the use of pipes, will be described below). The general form of output redirection is: command> file name. For example: $ ls> Directory.Out $ cat directory.outch1.doc ch2.doc ch3.doc chimp config mail / test / $ saving the output of the ls command as a file called Directory.out. Note: If the file behind the symbol already exists, then this file will be rewritten. To avoid the output redirection of the specified file can only store the output redirection of the current command, the shell provides an additional means of output redirection. Output additional redirection is very similar to the output redirection, the difference is only that the function of outputting the additional redirection is to append the output of the command (or executable program) to the final result of the specified file, and the original content of the file is not destroyed. . If you want to add a command to the output of the command to the specified file, you can use additional redirect operators >>. Form is: Command >> File Name. For example: $ ls * .doc >> Directory.Out $ cat directory.outch1.doc ch2.doc ch3.doc chimp config mail / test / ch1.doc ch2.doc ch3.doc $ and the standard output redirection, The error output of the program can also be reordered. Use symbol 2> (or appended symbol 2 >>) to represent the redirection of the error output device. For example, the following command: $ LS / USR / TMP 2> Err.file can see the normal output result of the program on the screen, but send any error information of the program to file err.file to check for future check. You can also use another output redirect operator (&>) to send the standard output and the error output to the same file. For example: $ LS / USR / TMP &> OUTPUT.FILE uses the redirection to combine the command, you can implement new features that the system is unable to provide.

For example, using the following command sequence: $ LS / USR / BIN> / TMP / DIR $ WC -W $ CAT PrefaceBash can also list partial file names in the current directory to complete file name extension. If you type an ESC, type it?, The shell will list all file names that match the input string. For example, in the next example, type ESC after the input is not completed?, The shell will list all the strings matching the input string, then the shell returns the command line, according to the list of files, you can type the file you want to enter. Name or press the TAB button to complete the file name extension.

For example: $ lsdocument docudrama $ cat doc Documentdocudrama $ cat docudrama [Example] The following is a list of files in a directory included: Firebird2.7.tgz Firebird.README Firebird2.60.tgzFireBird Firebird2.60.tgz.README now Delete the firebird2.60.tgz.readme file, type: $ rm -f fi system will issue an alert, and automatically make the command line to: $ rm -f fire and wait for the user to further enter the back section of the file name . Now type: b system once again issues the sound and automatically makes the command line to: $ RM -F Firebird and wait for the user to further enter the back section of the file name. Now type: 2.6 system once again issues an alert voice, and automatically makes the command line to: $ RM -F Firebird2.60.tgz and wait for the user to further enter the back section of the file name. Now type:. This command will be completed: $ rm -f firebird2.60.tgz..ready can be seen from the previous example, BASH always try to complete the command according to the information entered by the user. When it is impossible to complete the command according to the existing information, the user is prompted to give more information, then further complement the command according to the user's prompt. As a user, it is best to give sufficient information at a time to make the BASH to make a command completion; otherwise it will take a few times , and the time will be consumed. The command line editing can edit the command line in BASH so that the user can modify the typed command before performing the typed command. If you have a spelling error while typing commands, just use the editing command to correct the editing error before running the typed command, then execute it without re-enter the entire line command. This feature is especially useful for commands with long path file names. Table 10-2 is a summary of the command line editing operation. Table 10-2 Command line Edit Operation command line Editing Operation function Ctrl b or left arrow key left shift a character (moving to the previous character) Ctrl f or right arrow key to move a character (moving to the latter) Ctrl A Move to the head Ctrl E Move to the line ESC B left shift a word ESC F right shift a word DEL Delete the character Ctrl D delete the cursor in the Ctrl H to remove the left side of the cursor Character Ctrl K Delete to the row command history In Bash, the history command saves the most recently executed command. The history number of these commands starts with 1, only limited commands can be saved, up to 500, namely the history number of the history command is 500. To view the recently executed command, just type the History command, then type the Enter key, the recently performed command is displayed in the order of order (the number before the command is history number). [Example] $ History1 CP MyData Today2 VI MyData3 MV MyData Reports4 CD Reports5 LS ... All of these commands are called events, and an event indicates that an operation has occurred, that is, a command has been executed. These events use digital identity based on their success, this identification is called the historical event number. The event number of the last historical event is the largest. Each event can be determined by its historical event number or initial character or string of commands. Use the history command to query the previous event and display them to the command line to execute this event.

The easiest way is to use the up and down arrow keys to display the previous events to the command line. This action is not required to run the History command. Press the up arrow key, then an event on the last execution will appear on the command line, then press the previous event, will appear on the command line; press the down arrow key, will make The next incident of the current event appears on the command line. BASH can also complete the character extension of historical events by typing ESC, Tab keys. Like the standard command line extension characteristics, type some strings of historical events, then type ESC, and type the Tab key, and the historical events that match the string that just typed will be automatically extended and returned to the command line. If more than one event matches the input string, he will hear a ring, continue to type characters or strings, and the shell will only determine the historical event to be typed. There is also a command to query and execute historical events -! Command. Type characters associated with historical events after the command, this associated character can be the historical event number of the historical event, or the first few characters of the event. In the example below, query the event number of the historical event number 3, and then match the characters you start, and query the command. [Example] $! 3mv MyData Reports $! MVMV MyData Reports can also query historical events with an offset (relative to the last event in the list of historical events). The negative offset will be ordered from the historical event list. In the example below, the event number "VI MyData" is queried with a negative offset. It must be noted that this offset is the last event relative to the historical event list. In this example, the last event in the list of historical events is event 5, and the first event in the historical event list is 1. From the incident of the historical incident number 5, it is shifted forward 4, which is an event of the historical event number 2. [Example] $! -4vi mydata If you type !!, the system defaults to the previous event. In the following example, the user typed on the command line !! Command, the system will execute the previous event: "LS" command. [Example] $ !! LSMYDATA Today Reports can also search for a historical event with "Mode". Search "Mode" must be used to enclose. The following example uses "Mode" "" MYD? "To search for historical events" VI MyData ". [Example] $!? Myd? Vi mydata1. Query history events can edit the events in the historical event list on the command line. Table 10-3 lists the various operations of the query historical event list. Table 10-3 Query History Event Operation Query History Event Operation Function Ctrl N or down the cursor button to move the next historical event in the list of current events Ctrl P or up the cursor button to the current event in the historical event list Previous history ESC Move to historical event list tail! Event_num to locate a historical event with historical event numbers! Characters uses historical events with historical events!? Pattern "Mode" to query the events in the historical event list! -Event_num locates historical events through the offset 2. Configure the History: Histfile and HISTSIZE system Save the number of historical events being saved in a specific system variable, this variable is HISTSIZE . The default value of this variable is usually set to 500. This value can be modified. For example: $ HISTSIZE = 10 Reset the value of the HISTSIZE to 10. Historical events are saved in a file, and the file name is specified by the variable histfile. Usually the default name of this file is .bash_history. You can specify a new file name by assigning a value of the Variable Histfile.

[Example] $ Histfile / Home / Lisa / .bash_history $ histfile = "/ home / lisa / newhist" $ HISTFILE / HOME / LISA / NewHist The value of the variable histfile is displayed, and then gives it a new value "/ Home / lisa / newhist", all historical events will be saved in the newhist file.

Alias ​​is another way to make your work easier is to use the command alias. Command alias is usually an abbreviation for other commands to reduce keyboard input. The command format is: alias [alias-name = 'original-command'] where alias-name is an alias for the user, oriGinal-command is the original command and parameters. It should be noted that because the Bash is a space or carriageway to identify the original command, if the quotation is not used, it may cause BASH to only intercept the first word, thereby incorporated. If any parameters are not used after the alias command, display the unknown commands that are currently being used. The alias taking the order is always valid during the login. If the user needs an alias to be valid at each login, then the alias command is written to the initialized script file. [Example] If you want to type the following command, it is best to build an alias for it to reduce workload. $ CD / USR / X11 / LIB / X11 If you create an alias called GoConfig, type the following command at the Bash prompt: $ alias goconfig = 'CD / usr / x11 / lib / x11' Now, unless You exit Bash, type GoConfig, will have the same role in the original long command. If you want to cancel an alias, you can use the following command: $ UNALIAS GOCONFIG This is some people who think useful, you can write them into initialized scripts to improve work efficiency: alias ll = 'ls -l'alias log =' Logout'Alias ​​ls = 'ls -f' If you are a DOS user and get used to the DOS command, you can define the following alias to make Linux performance like DOS: Alias ​​Dir = 'ls'alias copy =' CP'Alias Rename = 'mv'Alias ​​md =' mkdir'Alias ​​rd = 'rmdir' Note: When defining alias, the equal sign cannot have spaces on both sides, otherwise shell cannot decide what you need to do. Quotation is required only when the command is included in the command. If you type an alias command that does not have any parameters, all defined alias will be displayed. The prompt BASH has two levels. The first level prompt is the situation that the BASH that is often seen is waiting for the command to enter. The default value of the first level prompt is a symbol. If the user does not like this symbol, or willing to define the prompt himself, simply modify the value of the PS1 variable. For example, it will be changed to: ps1 = "Enter A Command:" The second level prompt is displayed when the BASH is required to enter more information to execute a command. The second level prompt defaults to>. If you need to define your prompt, you only need to change the value of the PS2 variable. For example, it will be changed to: ps2 = "more information:" The above two examples are the case where the set prompt is a static string. In fact, users can also use some special characters that have been defined in advance. These special characters will enable the prompt that contain information such as the current time. Table 10-4 lists some of the most common special characters and its meaning. Table 10-4 BASH prompt often uses special character special character descriptions! Show history number # Show the shell activation, the current command history number $ displays a $ symbol, if the current user is root, display # symbol / display an anti Slash D Displays the current date H Displays the computer host name n to run the shell, which will cause the prompt cross-line SHELL name T Dis display the current time u Displays the current user's username W to display the current work. Directory Benchmark W Display The current work directory These special characters can be combined to provide users with some prompts, providing very useful information.

Let's take a few practical examples: PS1 = "t" will make the prompt as follows: 02: 16: 15 and PS1 = T will make the prompt becomes as follows: T If ps1 = "t /" The prompt will be made as follows: 02: 16: 30 This example is the combination of two special characters. Controlling the operating mode BASH has some special variables that control the shell work in different ways. For example, variable NoClobber prevents accidentally overwriting a file unexpected when redirecting output. You can set the valid or invalidation of the NOCLOBBER variable through the set command. The set command has two parameters: one is an option to specify the variable open (ON) or off (OFF), one is a variable name of a special variable. To make a special variable (effective), use the -o option to make it (invalid), with O options. For example: $ set -o noclobber // Make Noclobber variables to set O Noclobber // Three most common shell special variables for Noclobber variables are: IgnoreEOF, NOCLOBBER, and NOGLOB. The IgnoreEofignoreEOF variable is used to disable the use of Ctrl D to exit Shell (Ctrl D is not only used to exit the shell, and the user can terminate the input to the standard output. This operation is often used in some shell utility commands, such as utility command CAT In these utility operations, it is very easy to operate and unexpectedly exits shell.ignoreEOF special variables are used to prevent this unexpected exit. After $ set -o ignoreeof, users can only exit with logout or exit commands. Shell.noClobbernoclobber variables can protect existing files when redirect output, prevent accidentally override. In the following example, the user sets NOCLOBBER to be valid. When redirects, the user tries to override the existing file MyFile, at which time the system An error message will be returned. [Example] $ set -o noclobber $ cat preface> myfilebash: myfile: Cannot Overwrite existing file $ noglob Set Noglob variables, the shell will not extend some special characters or strings in the file name. Such as characters *,?, [], Etc. will no longer be used as a wildcard. If the user wants to list file names answer ?, can pass the following steps: First, the user makes the Noglob variable to be invalid, and then list the file name. You can see At present, the question mark on the current command line is considered to be a character in the file name, instead of being regarded as a wildcard. $ Set -o noglob $ ls answer? Answer? Child shell with the export command User Log in to the Linux system, The system will start a user shell. In this shell, you can use the shell command or declared variable, you can also create and run the shell script. When you run the shell script, the system will create a child shell. At this time, there will be two A shell, one is the shell that is launched when logging in, and the other is the shell that the system creates by the system. When a scriptor is running, its script shell will terminate, you can return to the shell before executing the script. From this In terms of meaning, users can have many shells, each shell is derived from a shell (called the parent shell). The variables defined in the sub-shell are only valid within the sub-shell.

转载请注明原文地址:https://www.9cbs.com/read-7002.html

New Post(0)