Linux Advanced: Bash techniques to double your efficiency (i)

Source: Internet
Author: User

SOURCE : Tpircsboy's blog (@churechill123) Many people who have used Linux for a period of time have already been able to play the basics of Linux in some basic ways, but without systematic learning it is easy to lack some practical skills. This series of articles describes some of the techniques that can improve the effectiveness of bash, mainly about historical command operations and some shortcut keys, so that you double your productivity at the command line, and these skills are a powerful tool. History Command Operation Chapter
    • The most basic View History command
history

    • !n History command with number n

No more copying and pasting, or hitting the history record. Execute history command to record a command inside, just need! + The sequence number before this command is recorded, such as

!767

This allows you to perform a 767 sequence number corresponding to the command ping www.tpircsboy.com

    • !-n Countdown Nth Historical command

You can use it too! -(the last few commands) to execute a historical command, such as!-1 is the last command,!-3 is the last third command

!-3

    • !! Previous command

!! Represents the previous command, which is equivalent to!-1.

This is a very convenient and practical command, such as a very long command and need administrator privileges, but finally knocked out but forgot to add sudo, here can be directly with sudo!! To complete the complex command that was just added, sudo.

sudo!!

    • !keyword Find the history command that contains the keyword

If you want to find a history command that contains a keyword, you can do so

!keyword

Find a History command that contains keyword, and then enter to execute this historical command

But in fact, this operation is very dangerous, if you read the wrong or mixed up the history of command, in the car before you actually do not know what to look for the history of the command, and enter after the command is executed, there is no chance to show you the command to find out what the specific is executed. Very dangerous, not recommended, you can use Magicspace (see below) or use CTRL + R to reverse lookup (recommended)

    • Ctrl + R Reverse Lookup command

Shortcut CTRL + R, then enter the keyword you want to find, enter the same time, bash dynamically incremental search, find the desired history command can press ENTER to execute, or ESC pull the extraction command out but do not execute. Press CTRL + R again to continue searching for the qualifying command.

    • History | grep keyword lists all eligible commands

Ctrl + R is undoubtedly the most convenient way to search for historical records, but of course you can also use history | grep keyword to find all the qualifying records, and then combine just the same! method to complete the command.

Note that the above mentioned contains! Can be stitched together with other commands, such as

sudo!-3, time!472, sudo!apt

Here are some tips on the history parameters:

    • !$ the last parameter of the previous command

If you just want to use the parameters of the last command, one by one, it's too complicated, you can do that.

cd!$

Of course, there's a simpler way to do this, and when you talk about the shortcut key section,

    • !^ the first parameter of the previous command

$ represents the last argument, and ^ represents the first argument

!^ is very handy in a scenario like this: you just backed up a profile, and then you want to edit the configuration file.

    vim !^

    • : n Nth parameter

^ and $ represent the first argument and the last argument, while: N represents the nth parameter, such as!:2, which represents the 2nd argument of the previous command

cd!:2

Note that parameter symbols can be used not only!$,!^,!:n, but also the symbols of these parameters can be used in any combination with an expression, such as

CD!762:2 (represents the 2nd parameter of the NO. 762 history command)

LS!-3^ (represents the first parameter of the last 3rd command)

Dpkg-l!apt$ (indicates the last parameter to search for a command with APT)

    • Magic-space Let history expressions and parametric symbols instantly appear as prototypes

Although history expressions and parameter symbols are easy to use, you don't know exactly what these expressions and symbols represent until the command carriage returns that contain these expressions and symbols. To solve this problem, we can use Magic-space

bind Space:magic-space

With this setting, after entering history expressions and parameter symbols in bash, the expressions and symbols immediately become the historical commands and parameters they represent, known as magic space.

You can put this sentence into the. BASHRC and let the settings persist (Mac is in. bash_profile).

    • Add a space before the command to make it count toward the history

By adding a space before the command, you can avoid changing the command into history, and small partners can do something bad without emptying the history.

    • Histsize=0 Do not log commands

If you do not want to log commands, you can set histsize=0. If you want to recover, you can set Histsize to a value greater than 0 (the default is 500 or 1000)

    • Histcontrol=ignoredups Removing duplicate commands

Once this is set, successive executions of the same command will be logged only once.

This is a temporary introduction to the history chapter, and the next shortcut chapter will introduce more bash techniques.

If you know more about the history of BASH commands, please share them in the comments section.

Transfer from http://blog.jobbole.com/84460/

Linux Advanced: Bash techniques to double your efficiency (i)

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.