Summary of the unknown usage of "!" in Linux

Summary of the unknown usage of "!" in Linux

Preface

In fact, the humble "!" has many amazing uses in Linux that will amaze you. This article will detail the magical uses of "!". Let’s take a look at the detailed introduction.

Execute the previous command

For example, after executing the previous command, you can use the following method to execute the previous command again:

$ whereis bash #Execute command bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
$ !! # Execute the previous command again where is bash
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

!! represents the last executed command. As you can see, when two exclamation marks are entered, it displays the previous command and executes it at the same time. Of course, we usually think of using the "UP" key to accomplish this task. But if it is based on the previous command expansion, !! will be more convenient.
For example, you want to view a file, but forget to enter more:

$ /opt/user/test.txt #Forgot to enter more
$ more !! #Isn’t this much faster?

Use it!! Isn’t it much more convenient?

Execute a command using the first or last argument of the previous command

Use the last parameter of the previous command

For example, if you use ls to list the contents of a directory without any parameters, but want to execute it again with the -al parameter without having to enter a long parameter, you can use the following method:

$ ls /proc/1/task/1/net/tcp
/proc/1/task/1/net/tc
$ ls -al !$
ls -al /proc/1/task/1/net/tcp
-r--r--r-- 1 root root 0 12月22 17:30 /proc/1/task/1/net/tcp

Here !$ represents the last parameter of the previous command.

Use the first parameter of the previous command

To use the first parameter of the previous command, just use !^, for example:

$ ls -al !^

Remove the last parameter and execute the previous command

If you want to execute the previous command without the last parameter:

$ ls -al dir #Assume dir is a very long string $ !:-
ls -al

In what scenarios might it be used? For example, if the last parameter of your previous command is a long string, and you don’t want to use it, and the backspace key is slow to delete, you can use the above method.

Use all parameters of the previous command

We mentioned earlier that we use the last parameter of the previous command. What if it is not the last parameter? It's very simple, just use !*. For example, if we make a mistake in entering the find command and want to correct it:

$ fin -name "test.zip" #The find word here is wrong.
$ find !*
find ./ -name "test.zip"
./workspaces/shell/find/test.zip
./workspaces/shell/test.zip

Use the parameters specified in the previous command

Some readers may ask, what if I only want to use one of the parameters? Just follow the rule of ![command name]:[parameter number]. For example:

$ cp -rf dira dirb/ #Copy dira to dirb
$ ls -l !cp:2 #View the contents of dira ls -l dira
total 0
-rw-rw-r-- 1 hyb hyb 0 Dec 22 17:45 testfile

The effect is more obvious when the parameters of the previous command are very long and you need to use a parameter in the middle.

Execute commands in history

We all know that we can use the history command to view previously executed commands, but how do we execute the commands in history again? We can use the "UP" key to view, but when the history command is very long, it is not very convenient. At this time, "!" comes in handy:

$ history
(More content omitted here)
2043 touch ./dira/testfile
 2044 cp -rf dira dirb/
 2045 ls -al dira
 2046 ls -l dira
 2047 ls -al dira
 2048 ls -l dira
 2049 ls -al dira
 2050 ls -l dira
 2051 history

We can see that the history command can show the previously executed commands, and we can also see a value in front of it. If we want to execute the previous cp -rf dira dirb/ command, we can actually do it in the following way:

$ !2044 #2044 is the nth command executed cp -rf dira dirb/

That is, historical commands are executed through ![historical command value].

Of course, if we want to execute the second to last command, there is a way:

$ !-2 #The exclamation mark is followed by a negative number, and the negative number represents the last number of the item

Execute historical commands by keyword

!You can execute commands based on keywords.

Execute the previous command starting with the keyword

For example, execute the previous find command:

$ !find #Execute the previous command starting with find

Execute the previous command containing the keyword

For another example, execute the previous command containing name:

$ find ./ -name "test"
./test
./find/test
$ !?name?
find ./ -name "test"
./test
./find/test

Replace the parameters of the previous command

For example:

$ find ./ -name "old*" -a -name "*.zip"

If we need to replace old with new in this command:

$ !!:gs/old/new

The role of logical negation

This is its most familiar function, for example, to delete all files except those ending with cfg:

rm !(*.cfg) # Be careful when deleting

I will not go into details here.

Summarize

The effect of the exclamation mark "!" is indeed amazing sometimes. It may have just been mentioned before! Combined with other characters to represent a certain use of a specific meaning, in fact, we can combine or expand to discover more wonderful uses. This will not be elaborated here. What other good discoveries have you made? Welcome to leave a message!

Well, the above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

Author of this article: Watch this article link: https://www.yanbinghu.com/2018/12/22/40915.html
Copyright Statement: This article is an original article, the copyright belongs to Shouwang, and it adopts the CC BY-NC-SA 3.0 license agreement. Please contact me if you want to reprint!

You may also be interested in:
  • Summary of using the exclamation mark command (!) in Linux

<<:  In-depth understanding of the core principles of React Native (Bridge of React Native)

>>:  Mysql 5.6 adds a method to modify username and password

Recommend

Implementing Priority Queue in JavaScript

Table of contents 1. Introduction to priority que...

Application and implementation of data cache mechanism for small programs

Mini Program Data Cache Related Knowledge Data ca...

Vue.js implements simple timer function

This article example shares the specific code of ...

How to install MySQL and Redis in Docker

This article is based on the CentOS 7.3 system en...

How to view version information in Linux

How to view version information under Linux, incl...

The difference and usage of LocalStorage and SessionStorage in vue

Table of contents What is LocalStorage What is Se...

Common solutions for Mysql read-write separation expiration

The pitfalls of MySQL read-write separation The m...

Detailed explanation of MySQL transactions and MySQL logs

Transactional Characteristics 1. Atomicity: After...

MySQL cleverly uses sum, case and when to optimize statistical queries

I was recently working on a project at the compan...

Detailed explanation of Javascript basics

Table of contents variable Data Types Extension P...

How to implement Docker to dynamically pass parameters to Springboot projects

background Recently, some friends who are new to ...

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

MySQL slow query and query reconstruction method record

Preface What is a slow query and how to optimize ...