BASH Shell (III)-command alias and History commands

Source: Internet
Author: User

One command alias settings: alias, unalias

Command alias is a very interesting thing, especially when your usual command is very long! In addition, you can add predefined attributes to some common commands to prevent accidental file killing! For example, if you want to query hidden files and list them on a long page, you need to issue the "ls-Al | more" command, I am bored! Several words are required! Can lm be used to simplify the process ?! Of course, you can issue the following command in the Command column:

[Root @ Linux ~] # Alias LM = 'LS-L | more'

Note: "The definition rules of Alias are almost the same as those of variables." In addition, the alias setting can replace the existing commands! For example, we know that root can remove (RM) any data! SoWhen you are working as a root user, you need to be very careful, but there is always a miss. rm provides a parameter for us to confirm whether to remove the file, that's the-I parameter! So you can do this:

[Root @ Linux ~] # Alias Rm = 'rm-I'

Hey! When using RM in the future, you don't have to worry too much about deletion errors! This is also the advantage of the command alias! So how do I know what command aliases are currently available? Use alias!

[Root @ Linux ~] # Alias
Alias L. = 'LS-D. * -- color = tty'
Alias LL = 'LS-l -- color = tty'
Alias LM = 'LS-Al | more'
Alias ls = 'ls -- color = tty'
Alias Vi = 'vim'
Alias which = 'Alias |/usr/bin/which -- tty-only -- read-alias -- show-dot -- show-tilde'

From the above materials, you will also find one thing. We mentioned in the VI document editor that VI is not the same as vim, and VI is relatively old, vim can be used to replace VI. Our FC4 clearly has VI/Vim at the same time. Why do I execute VI to access Vim? Haha! That's because the above table sets "alias Vi = 'vim! If you want to cancel the command alias, use unalias! For example, to remove the alias of the LM command, use:

[Root @ Linux ~] # Unalias LM

2. History commands: History

We mentioned above that Bash has a service that provides instruction history! How can we query the commands we once issued? Use history! Of course, if you think it is too troublesome to input too many characters in histsory, you can use the command alias to set it! Don't tell me it won't be set yet!

Alias H = 'History'

In this case, input H equals input history! Let's talk about the use of history!

[Root @ Linux ~] # History [N]
[Root @ Linux ~] # History [-C]
[Root @ Linux ~] # History [-raw] histfiles

Parameters:
N: Number, which indicates "List of the nearest n commands!
-C: removes all history content in the current shell.
-A: adds the newly added history command to histfiles. If histfiles is not added, it is written to ~ by default ~ /. Bash_history
-R: Read the content of histfiles to the History Memory of the current shell;
-W: Write the Current History Memory to histfiles!

Example 1: List all history memories in the current Memory
[Root @ Linux ~] # History
# Omitted earlier
1017 man bash
1018 LL
1019 history
1020 history
# The information listed is divided into two columns. The first column shows the code of this command in this shell,
#The other is the instruction content! As for how many Command records will be displayed, it is related to histsize!

Example 2: List the latest 3 Documents
[Root @ Linux ~] # History 3
1019 history
1020 history
1021 history 3

Example 3: immediately write the current data to histfile
[Root @ Linux ~] # History-W
# The historical records are written to ~ by default ~ /. Bash_history!
[Root @ Linux ~] # Echo $ histsize
1000

Normally, after we log on to the Linux host using bash, the system will take the initiative ~ /. Bash_history read commands that were previously issued, SO ~ /. How many pieces of data does bash_history record? This is related to the value of the variable histsize In Your bash! Under the preset FC4, 1000 pieces of data are recorded! Assume that after I log on to the host, a total of 100 commands are issued ~ 1100 A total of 1000 historical commands are updated ~ /. Bash_history .』 That is to say, when I log out, History commands will record the latest histsize to my record file! Of course, you can also use history-W to force immediate writing! So why the word "Update? Because ~ /. The number of records in bash_history will always be as large as histsize, and old messages will be automatically taken away! Only keep the latest! Can the history command of history only allow me to query the command? Haha! Of course! We can use related functions to execute commands! For example:

[Root @ Linux ~] #! Number
[Root @ Linux ~] #! Command
[Root @ Linux ~] #!!
Parameters:
Number: indicates the number of commands executed;
Command: the nearest command searches for the command starting with "command" and executes the command;
!! : Execute the previous command (equivalent to pressing enter after pressing the keys)

Example:
[Root @ Linux ~] # History
66 man RM
67 alias
68 man history
69 history
[Root @ Linux ~] #! 66 <= execute 66th commands
[Root @ Linux ~] #!! <= Execute the previous command. In this example, it is! 66
[Root @ Linux ~] #! Al <= execute the latest commands starting with Al (the first 67th commands listed above)

After the above introduction, what does it mean? More historical commands can be used! If you want to execute the previous command, in addition to using the up and down keys, you can directly run the command 『!! In addition, I can directly choose to issue the N command ,『! N. You can also use the command header, for example 『! VI to execute the latest command. It starts with the command column of Vi!

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.