10 shortcuts to bash

Source: Internet
Author: User
10 shortcuts to master bash-general Linux technology-Linux technology and application information. The following is a detailed description. If you have input commands in a Linux shell Command Prompt, you may have used bash. Because it is the default shell of most modern releases of GNU/Linux.

Bash shell is the main interface of the Linux operating system. It can accept, interpret, and execute the commands you enter, and provides you with shell scripts and output results after automatic task execution.

Bash's conservative appearance hides many useful tools and quick operations. If you are a user who frequently uses command lines, understanding these tools and shortcuts can save you a lot of input. This article introduces 10 useful shortcuts.

1. easy retrieval of previously entered commands

Bash records executed commands in a history buffer and allows you to use the up and down arrow keys to repeatedly recall previously entered commands in History commands.

Type the first few letters of the command and press Ctrl-R. bash will scan the History commands to match the entered commands and display them on the console. This allows you to quickly search for commands previously executed. Repeat Ctrl-R to repeat the entire list of matching commands.

2. Use command alias

If you often need to run a command with the same Parameter options, you can use bash to create an alias for the command.

Aliases can combine the required options. Therefore, you do not need to remember all parameters or manually enter them each time. For example, if you often need to run the ls command with the-l parameter to obtain a detailed directory list, you can use the following command: bash> alias ls = 'ls? L' to create an alias that automatically contains the-l parameter.

After an alias is created, input ls at the bash command prompt to call the alias and generate the ls? L output.

You can also call alias to create a list of available aliases without any parameters. You can also use unalias to delete an alias.

3. Use the file name auto-completion Function

Bash supports Automatic completion of file names at the command line prompt. To use this feature, we first enter the first few letters of the file name, followed by pressing the Tab key. Bash scans the current directory and all other directories in the search path to find matching files. If only one match is found, bash will automatically complete the file name. If there are multiple matches, you will be prompted to select one of them.

4. Use shortcuts to efficiently edit command lines

Bash supports a large number of keyboard shortcuts for command line navigation and editing. The shortcut key Ctrl-A can move the cursor to the beginning of the command line, while Ctrl-E can move the cursor to the end of the command line. The Ctrl-W shortcut can quickly delete the characters before the cursor, while Ctrl-K can immediately delete the characters after the cursor. Use Ctrl-Y to cancel the delete operation.

5. Get automatic notification of new emails

We can configure bash to automatically notify users of new emails. You can set the $ MAILPATH variable to point to your local mail pool. For example, the following command:

Bash> MAILPATH = '/var/spool/mail/john'

Bash> export MAILPATH

Every time a new message is added to john's mail pool, bash can output a notification on john's console.

6. Run the task in the background

Bash runs one or more tasks in the background and can selectively suspend or resume any current task (or job "). To run a task in the background, you must add a connector (&) at the end of the command (&). The following is an example: bash> tail-f/var/log/messages &

[1] 614

Each task running in the background in this way is assigned a job ID and printed and output to the console. You can use the command fg job number to call a background task back to the foreground for running. The job number here is the job ID number of the task you want to call back to the foreground. The following is an example:

Bash> fg 1

Enter jobs at the bash command prompt to obtain a list of active jobs.

7. Quickly jump to common Directories

You may already know that the $ PATH variable can list bash's "Search PATH". When the requested file cannot be found in the current directory, bash will automatically search for the directory. However, bash also supports the $ CDPATH variable. When trying to change the directory, this variable lists the directory to which the cd command is switched. To use this feature, we need to assign a directory list to the $ CDPATH variable, as shown in the following example:

Bash> CDPATH = '.:~ :/Usr/local/apache/htdocs:/disk1/backups'

Bash> export CDPATH

Now, whenever you use the cd command, bash checks all directories in the $ CDPATH list to find the directory name to be switched.

8. Execute computing

Bash can execute some simple arithmetic operations on the command prompt line. To use this feature, enter the arithmetic expression you want to calculate in a command prompt and enclose it in parentheses, as shown in the following example. Bash executes the computation and returns the result.

Bash & gt; echo $(16/2 ))

8

9. Custom Command Prompt

We can customize the bash command prompt to display, including the current user name and host name, current time, average load and current working directory. To achieve this, modify the $ PS1 variable as follows:

Bash> PS1 = 'U @ h: w @>'
Bash> export PS1
Root @ medusa:/tmp 0:01 PM>

The user name, host name, current working directory, and current time of the Current logon are displayed on the command line. You can obtain a list of symbols that bash can understand from the user guide page.

10. Get context-specific help

Bash also provides help for all built-in commands. Type help to view the list of built-in commands. Enter the help command name to get help for specific commands. The command name after Help is the command you want to view Help. The following is an example:

Bash> help alias

Help text

Obviously, you can get detailed help on bash commands at any time by typing man bash in the command line.

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.