Linux tutorial-operations in Bash

Source: Internet
Author: User
Tags echo command
Article title: Linux tutorial-operations in Bash. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Command and file name extension features
  
The Bash command line provides command and file name extension features. When you enter a command or file name that has not been completed, you only need to enter the Tab key to activate the command and file name extension feature, so as to complete the remaining input of the command. If multiple commands or files have the same prefix, Bash will ring and wait for the user to enter enough characters to select a unique command or file name, the system will automatically complete the searched command or file name. after you press the Enter key, the system will execute this command. For example:
  
$ Cat pre
  
$ Cat preface
  
Bash can also list some matching file names in the current directory to complete file name extension. If you type Esc, then type ?, Shell will list all file names that match the input string. For example, in the following example, type Esc ?, Shell will list all strings that match the input string, and then shell echo command line, according to the name of the list, you can type the name of the file to be input or press the Tab key to complete the extension of the file name. For example:
  
$ Ls
  
Document docudrama
  
$ Cat doc
  
Document
  
Docudrama
  
$ Cat docudrama
  
[Example] the following is a list of files contained in a directory:
  
Firebird2.7.tgz Firebird. README Firebird2.60.tgz
  
FireBird Firebird2.60.tgz. README
  
To delete the Firebird2.60.tgz. README file, type:
  
$ Rm? F Fi
  
The system will generate an alarm and automatically complete the command line as follows:
  
$ Rm? F Fire
  
And wait for the user to further enter the part after the file name. Now, type:
  
B
  
The system then sends an alarm again and automatically includes the following command line:
  
$ Rm? F Firebird
  
And wait for the user to further enter the part after the file name. Now, type:
  
2.6
  
The system then sends an alarm again and automatically includes the following command line:
  
$ Rm? F Firebird2.60.tgz
  
And wait for the user to further enter the part after the file name. Now, type:
  
.
  
In this case, the command is supplemented as follows:
  
$ Rm? F Firebird2.60.tgz .. README
  
As shown in the preceding example, bash always tries its best to complete the command based on the information entered by the user. If the command cannot be supplemented based on the existing information, the system prompts the user to provide more information, and then further complements the command based on the user's prompt. As a user, it is best to provide enough information at a time for bash to complete the command; otherwise, the time will be consumed after several times.
  
Edit command line
  
In Bash, you can edit the command line so that you can modify the command you typed before executing the command you typed. If a spelling error occurs when you type a command, you only need to use the edit command to correct the editing error before running the command, and then run it without re-entering the entire command line. This function is particularly useful for commands that use a long path file name as a parameter.
  
Table 10-2 is a summary of the command line editing operation.
   
Command History
  
In Bash, the history Command can save the recently executed command. The history number of these commands starts from 1, and only a limited number of commands can be saved, up to 500, that is, the default history Number of the history Command is 500. To view the most recently executed commands, just type the history Command and enter the enter key. recently executed commands are displayed in sequence (the numbers before each command are historical records ).
  
[Example]
  
$ History
  
1 cp mydata today
  
2 vi mydata
  
3 mv mydata reports
  
4 cd reports
  
5 ls
  
...
  
All these commands are called events. an event indicates that an operation has occurred, that is, a command has been executed. These events are identified by numbers in the order they are executed. This identifier is called a historical event number. The event number of the last historical event is the maximum. Each event can be determined by its historical event number, initial character of the command, or string.
  
You can use the history command to query previous events and display them on the command line to execute this event. The easiest way is to use the up and down arrow keys to display previous events to the command line one by one. This operation can be executed without running the history Command. Press the up arrow key, and the last execution event will appear on the command line. click again, and the last event will appear on the command line; by pressing the down arrow, the next event of the current event appears on the command line.
  
Bash can also extend the characters of historical events by entering the Esc and Tab keys. As with the standard command line extension feature, type some strings of historical events, type Esc, and then enter the Tab key, historical events that match the string you just typed will be automatically extended and displayed to the command line. If more than one event matches the input string, the shell will uniquely identify the historical event you want to type when you continue typing a character or string.
  
There is also a command to query and execute historical events --! Command. In! After the command, enter the character associated with the historical event. the associated character can be the historical event number or the first few characters of the event. In the following example, the event with the historical event number 3 is queried, and then matched with several characters starting with it. the command is also found.
  
[Example]
  
$! 3
  
Mv mydata reports
  
$! Mv
  
Mv mydata reports
  
You can also use an offset (relative to the last event in the historical event list) to query historical events. The negative offset is offset forward from the end of the history event list table. In the following example, the event "vi mydata" with the historical event number 2 is queried using a negative offset. It must be noted that this offset is relative to the last event in the historical event list. In this example, the last event in the history event list is event 5, and the first event in the history event list is 1. Offset 4 from the event with the historical event number 5 to the forward, that is, the event with the historical event number 2.
  
[Example]
  
$! -4
  
Vi mydata
  
If you type !!, The system defaults to the previous event. In the following example, you can type !! Command, the system will execute the previous event: "ls" command.
  
[Example]
  
$ !!
  
Ls
  
Mydata today reports
  
You can also use the "mode" to search for a historical event. The search "mode" must use the symbol "?" . In the following example, the "mode" is used. Myd ?" To search for the historical event "vi mydata" with the event number 3 ".
  
[Example]
  
$ !? Myd?
  
Vi mydata
  
Query historical events
  
You can edit events in the historical event list on the command line. Table 10-3 lists the operations performed to query the historical event list.
  
Table 10-3 query history event operations
  
Configure history: HISTFILE and HISTSIZE
  
The number of historical events saved by the system is stored in a specific system variable, which 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 HISTSIZE to 10.
  
Historical events are saved in a file. the file name is specified by the HISTFILE variable. The default name of this file is. bash_history. You can assign a value to the variable HISTFILE to specify a new file name.
  
[Example]
  
$ Echo $ HISTFILE
  
/Home/lisa/. bash_history
  
$ HISTFILE = "/home/lisa/newhist"
  
$ Echo $ HISTFILE
  
/Home/lisa/newhist
  
The preceding operations first display the value of the variable HISTFILE, and then assign it the new value "/home/lisa/newhist". all historical events will be saved in the newhist file in the future.
  
From Turbolinux
  
  
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.