Bash tips are greatly subsidized. Now Linux's default shell

Source: Internet
Author: User
Bash tips are greatly subsidized. Now, Linux's default shell-general Linux technology-Linux programming and kernel information is shown below for details. Bash is a Shell program that we often deal with. This article provides a collection of its usage skills. I believe that after you read this content, it will certainly be able to work in the Bash world.
Execute commands from history

Sometimes, we need to repeat the previous commands in Bash. Of course, you can use the arrow keys to view commands that have been run before. But there is a better way: you can press Ctrl + r to enter the historical search mode. Once you find the command that needs to be repeatedly executed, press Enter.
Repeated Command Parameters

Let's look at an example:
Mkdir/path/to/exampledir
Cd! $

In this example, the first line of command will create a directory, and the second line of command will go to the directory just created. Here, "! $ "Is used to repeat the parameters of the previous command. In fact, not only can the command parameters be repeated, but the Command Options can also be. In addition, the Esc +. Shortcut Keys can switch between these command parameters or options.
Shortcut key used for editing

* Ctrl + a: move the cursor to the beginning of the command.
* Ctrl + e: opposite to the previous shortcut key, place the cursor at the end of the command
* Ctrl + u: Cut the content before the cursor
* Ctrl + k: opposite to the previous shortcut key, cut the content after the cursor
* Ctrl + y: paste the content cut by the preceding two shortcut keys.
* Ctrl + t: swap the order of the first two characters of the cursor
* Ctrl + w: Delete the parameter (option) or content on the left of the cursor
* Ctrl + l: clear screen

Process jobs

First, use the Ctrl + z shortcut to suspend the command being executed. If you want the process to be executed in the background, you can execute the bg command. The fg command allows the process to return to the foreground again. Run the jobs command to view which processes are executed in the background.

You can also use the job id in the fg or bg command, for example:
Fg % 3

Another example:
Bg % 7
Use replacement

* Command replacement

First look at the example:
Du-h-a-c $ (find.-name *. conf 2> &-)

Note the section in $ (), which tells Bash to run the find command and then use the returned result as the du parameter.
* Process replacement

Let's look at the example first:
Diff <(ps axo comm) <(ssh user @ host ps axo comm)

This command compares processes running in the Local System and remote system. Note the section in <.
* Xargs

Example:
Find.-name *. conf-print0 | xargs-0 grep-l-Z mem_limit | xargs-0-I cp {}. bak

This command backs up all. conf files in the current directory.

MPs queue

The following is a simple example of using pipelines:
Ps aux | grep init

Here, the "|" operator redirects the output of ps aux to grep init.

There are two more complex examples:
Ps aux | tee filename | grep init

And:
Ps aux | tee-a filename | grep init
Save the standard output as a file

You can save the standard output content of the command to a file, for example:
Ps aux> filename

Note the ">" symbol.

You can also append the output content to an existing file:
Ps aux> filename

You can also split a long line:
Command1 | command2 |... | commandN> tempfile1
Cat tempfile1 | command1 | command2 |... | commandN> tempfile2
Standard stream: redirection and combination

Example of redirection stream:
Ps aux 2> & 1 | grep init

The numbers here represent:

* 0: stdin
* 1: stdout
* 2: sterr

In the above command, "grep init" not only searches for the standard output of "ps aux", but also searches for the sterr output.

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.