Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the historical extension function of bash

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about what the historical expansion function of bash is, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can gain something according to this article.

Bash's historical extension (History Expansion) is also known as Bang (!) Command, history extension is the process by which bash converts historical commands to executable commands. The History library under Bash provides a historical extension function similar to the historical extension under csh. The operation history command in the history extension generally has two parts:

First of all, we need to find the corresponding command from the history command. The selected command we call Event (entry), such as Bang Bang (!), is to select the last command.

Select some or all of the text of the selected line to include in the current line. The entry to be operated (Event) Bash splits it into Words (word), and the Words in the command is separated by spaces, so we can use the modifier (Modifiers) to adjust the Words to meet our requirements. Note: Words is not an English word, but a sequence of characters.

First, let's look at two orders. Do you know what the second order means?

Cat / tmpplash cat.cat.txthandl0! *: gs/cat./echo Entry Marker (Event Designators)

The item marker is a reference to a command line entity in the history list, and unless it is an absolute reference, the reference to the item is relative to the current position in the history list.

Item sign bar target sign description! Start a historical substitution, unless it is followed by spaces, tabs, line Terminators, "=", "(" (when the shell option of extglob is turned on using the built-in command shopt). ! n repeats the command numbered n in history-the historical number can be found in the history command.!-n executes the previous command, which can be used to execute the previous command! Or!-1, execute the third command before:!-3, and the list of backwards is history. !! Execute the previous command, which is the same as that of Ctrlmurp Prelli 1. ! string executes the most recent command that begins with the string string. This command means to repeat with! The last command at the beginning of the string, such as:! ca repeats the last command that begins with the character ca, such as cat ReadMe, (assuming the last ca begins with this command and ReadMe is followed by a newline)!? string [?] Start looking back at the current position in the history list (search back) the most recent command that contains the string string. If the string string you are looking for is followed by a newline character, the question mark after string can be omitted. For example:!? Read? It will still match cat ReadMe. (same as the environment above), if it is followed by a newline character such as:!? ReadMe, you do not have to enter [?] at the end. Replace ^ a ^ b quickly, replace a with b in the previous command, and execute the replaced command. Similar to ^ a ^ b ^. Note: this is just a replacement for a found instance, which is equivalent to:!: s/a/b. ^ abc deletes the abc from the previous command. ! # refers to all the strings currently entered, such as: more a! #; the final command is more a more a. Word Marker (Word Designators)

The word marker is used to select the desired word in the entry. Entry indicators and word indicators are generally separated by ":". ":" may also be omitted when the word indicator begins with "^", "$", "*", "-", "%". A word starts at the beginning of a line, and the first word is numbered 0. When inserted into the current row, these words are separated by a single space.

The word marker indicates the 0th word, which in many applications is the command itself. The nth word ^ the first parameter; that is, the first word. The last parameter. % recently "? string?" Matching words. The scope of the word xmury: if it is' 0mury', it can be abbreviated to all words except the 0th word, which is synonymous with '1mury'. If there is only one word in the entry, using'* 'will not return an error, just an empty string. The abbreviation x-similar to x * is the abbreviation of x-color, but it is important to note that this method ignores the last word.

It is important to note that when using the word indicator under Bash, there can be no entry indicator, and if the entry indicator is not used, the previous command will be used as the operation entry of the word indicator.

Modifier (Modifiers)

After the optional word indicator, you can add one or more of the following modifiers, each starting with':'.

The modifier modifier indicates that h removes the tail of the path name, leaving only the header. Only remove the contents after the last'/', which can be understood as the parent directory of the pathname. T remove everything in the pathname widget except the tail. Only the content after the last'/'is retained. R remove a trailing suffix of the form ".suffix" and keep the base name. Delete only the last dot'.' After the content. E only the suffix is retained. Keep only the last dot. And the content after the point. P prints the new command but does not execute it. Q quotes substituted words to prevent further substitutions. (translation Note, original: Quote the substituted words, escapin further substitutions.--Mitchell Chu) This reference directly puts single quotation marks on the referenced command to prevent further substitution. At first, I don't know how to translate this sentence. Later, Mitchell found that his translation was not wrong, because the word we quoted might be a variable, and if there were no quotation marks, it would lead to further substitution, but this parameter could be used to prevent this from happening. X, like Q, is a reference substitution word, but this is different from Q in that Q is an overall reference, and this one divides the replacement word into individual words using spaces, tabs, and newline characters. S/old/new/ replaces the contents of the first old position found in the entry line with the contents of the new position, and the'/ 'delimiter position can use any other character as the delimiter. If you want to use a delimiter in the old or new position, you need to use a backslash'\'to escape. If the character'& 'appears in the new position, it will be replaced with the content of the old position. If you want to use' &', use'\'to escape. The last delimiter can be omitted if it is the last character of the entire line. & repeat the last replacement. This is the last reference to s/old/new/ content. See below, the same an as a causes the substitution to be made throughout the entry, used with's', such as:!: gs/old/new/, or with'&'. G

The subsequent's' modifier is executed once for each word in the entry. This method is not reliable for testing under Bash 4.1.2.

Test $eee / tmp/test.logecho! test:Gs/t/a/;## the returned test is replaced twice # # but the following parameters are replaced only once

So Mitchell is wondering whether to execute the parameter only once and replace the command (word 0) globally. But another test refutes this view:

Aaaaaaaaaaa $aaaaaaa / tmp/aaaaaaaaaaaaa.logecho! aaaa:q:Gs/a/t/## at this time, the most substitutions appear in!: 0, twice!

However, according to the results of many tests, the zero word is replaced at most twice, and the other words are replaced only once. The specific reason is unknown for the time being!

With this in mind, let's reveal the meaning of the command at the beginning of the article:

First of all, we choose the command! (!: 0 can be written as!: 0 can be written as!! *)

With the command, we choose the second step, using 0 to select the word (!: 0 selected is cat)

The third step is to operate on the words, here is! * after the character substitution for the parameters.

Finally, it becomes a completed command: cat / tmp/echo.echo.txt

After reading the above, do you have any further understanding of the historical extension of bash? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report