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 are the shortcuts to manipulating Bash history

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what are the shortcuts to operate Bash history records, which are very detailed and have certain reference value. Friends who are interested must finish reading them.

1. The last parameter:! $

If you only want to learn one shortcut from this article, this is it. It replaces the last argument of the last command to your command line.

Look at this:

$mv / path/to/wrongfile / some/other/placemv: cannot stat'/ path/to/wrongfile': No such file or directory

AHA, I wrote the wrong file name "wrongfile" in the command. I should use the correct file name "rightfile" instead.

You can retype the previous command and completely replace "wrongfile" with "rightfile". However, you can also type:

$mv / path/to/rightfile! $mv / path/to/rightfile / some/other/place

This order can also work.

There are other ways in Bash to do the same thing through shortcuts, but this technique of reusing the last argument of the previous command is the one I use most often.

2. The nth parameter:!: 2

Have you ever done something like this:

$tar-cvf afolder afolder.tartar: failed to open

Like many others, I often get the wrong order of parameters for tar (and ln).

Xkcd comic

When you confuse the parameters, you can do this:

$!: 0! 1!: 3!: 2tar-cvf afolder.tar afolder

So you don't make a fool of yourself.

The index of each parameter of the previous command is zero-based, and you can use!: followed by the index number to represent each parameter.

Obviously, you can also use it to reuse specific parameters from the previous command, not all of them.

3. All parameters:!: 1 color $

Suppose I run a command like this:

$grep'(ping | pong) 'afile

The parameter is correct. However, I want to match "ping" or "pong" in the file, but I use grep instead of egrep.

I started typing egrep, but I didn't want to re-enter other parameters. Therefore, I can use the!: 1 color $shortcut to retrieve all the parameters of the previous command, from the second (remember that their index starts at zero, so it is 1) to the last (represented by $).

$egrep!: 1-$egrep'(ping | pong) 'afileping

You don't have to use 1Mel $to select all the parameters; you can also select a subset, such as 1-2 or 3-9 (if there are so many parameters in the previous command).

4. The last parameter on the penultimate line:!-2Rank $

The above shortcut is very useful when I know how to correct my command immediately after I make a mistake, but I often run another command after the original command, which means that the last command is no longer the one I want to quote.

For example, using the previous mv example, if I check the contents of the folder through ls to correct my error:

$mv / path/to/wrongfile / some/other/placemv: cannot stat'/ path/to/wrongfile': No such file or directory$ ls / path/to/rightfile

I can no longer use the! $shortcut.

In these cases, I can be in! Then insert-n: (where n is the number of commands to be traced back in the history) to get the final argument from the older command:

$mv / path/to/rightfile!-2:$mv / path/to/rightfile / some/other/place

Similarly, once you have learned it, you may be surprised at how often you need to use it.

5. Enter the folder:! $: h

On the face of it, this doesn't look very useful, but I use it dozens of times a day.

Imagine that the command I run looks like this:

$tar-cvf system.tar / etc/system tar: / etc/system: Cannot stat: No such file or directory tar: Error exit delayed from previous errors.

The first thing I might want to do is go to the / etc folder, look at its contents and find out what I did wrong.

I can do this in the following ways:

$cd! $: hcd / etc

This means: "get the last argument of the previous command (/ etc/system) and delete its last filename part, leaving only / etc."

6. Current line:! #: 1

Over the years, before I finally found it and learned it, I sometimes wondered if I could reference a parameter on the current line. I wish I could learn this shortcut early. I often use it to make backup files:

$cp / path/to/some/file! #: 1.bakcp / path/to/some/file / path/to/some/file.bak

But when I learned it, it was quickly replaced by the following shortcut.

7. Search and replace:!: gs

This searches for the referenced command and replaces the character between the first two / with the character between the last two /.

Suppose I want to tell someone that my s key doesn't work, but instead outputs f:

$echo my f key doef not workmy f key doef not work

And then I realized that the f bonds that appear here are all wrong. To replace all f with s, I can type:

$!: gs/f / s / echo my s key does not workmy s key does not work

It works not only on individual characters. I can also replace words or sentences:

$!!: gs/does/did/echo my s key did not workmy s key did not work test

To show you how to combine these shortcuts, do you know what these command fragments will output?

$ping! #: 0touch gspex txt$ cat!: $vi / tmpUniverse vi 0.txt $ls! $: h $cd!-2RV touch! $!-3RV $!! $. Txt$ cat!: 1 what are the contents of this article entitled "what are the shortcuts to manipulating the history of TMP?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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