10 Linux Bash Tips __linux

Source: Internet
Author: User
Tags aliases arithmetic clear screen

Bash contains some powerful tools and conveniences. If you are a frequent user of the command line, these can reduce the amount of input you have. This article provides 10 of the most useful tools:

1. Easy to invoke previous commands
Bash retains the commands you execute in a history buffer and allows you to loop through the previous commands with the cursor keys up and down to call them again. Even for a faster recall, you can "quickly find" a previously executed command by entering a small number of command characters and then ctrl-r with the key combination, and then bash scans the command history to match the commands and displays them on the console. Press Ctrl-r repeatedly to loop through the list of matching commands.

2. Use command alias
If you frequently use a command with the same parameters, you can have bash create an alias for it. This alias will combine the required parameters, so you don't need to memorize them or enter them manually each time. For example, if you use ls-l frequently to get a detailed list of directories, you can use this command:
bash> alias ls= ' Ls-l '

This creates an alias that automatically contains the-l option. Once the alias is created, entering LS at the bash prompt invokes the alias to output ls-l.

You can get a list of available aliases by calling aliases without any arguments, or you can use the Unalias command to remove aliases.

3. Auto-complement with file name
Bash supports automatic file name completion at the command prompt. To use this attribute, enter the first few characters of the file name, and then press the TAB key. Bash scans the current directory and all other directories in the search path to match the file name. If only one match is found, bash will automatically complete the file name for you. If multiple matches are found, you are prompted to select one.

4. Use shortcut keys to efficiently edit the command line
Bash supports many shortcut keys for the navigation and editing of the command line. The shortcut key ctrl-a moves the cursor to the beginning of the command line, and the shortcut ctrl-e moves the cursor to the end of the command line. The shortcut key ctrl-w directly deletes the word before the cursor, and the shortcut key ctrl-k directly deletes all characters before the cursor. You can undo a deletion with ctrl-y. Ctrl-l for clear screen.

5. Get automatic notification of new mail
By setting the $mailpath variable to point to your local mail pool, you can configure bash to automatically notify you of new messages. For example, the command is:
bash> mailpath= '/var/spool/mail/john '
Bash> Export Mailpath
At this point, bash prints a notification to John's console each time a new message enters John's mail pool.

Run tasks in the background
6. Run tasks in the background
Bash allows you to run one or more tasks at a later date and optionally suspend or restart any current tasks. To run a task in the background, add a & mark after its command line. Examples are as follows:
Bash> tail-f/var/log/messages &
[1] 614

Each background task in this way is assigned a task ID and is output in the console. The task can be transferred from the background to the foreground by the command FG Jobnumber, Jobnumber is the task ID you want to transfer to the foreground. Examples are as follows:
Bash> FG 1

Any time you enter jobs at the bash prompt, you can get a list of the tasks you are performing.

7. Quickly jump to a frequently used directory
You may already know that the $path variable lists bash's search path, the directory that will be searched when it cannot find the requested file in the current directory. However, Bash also supports the $cdpath variable, which lists a list of directories that the CD command can access when attempting to change the directory. To use this feature, assign a directory listing to the $cdpath variable, as shown in the following example:
Bash> cdpath= '.: ~:/usr/local/apache/htdocs:/disk1/backups '
Bash> Export Cdpath

Now, as soon as you use the CD command, bash checks all directories in the $cdpath list to match the directory name.

8. Performing calculations
Bash can perform simple arithmetic operations at the command prompt. To use this feature, simply type the arithmetic expression you want to calculate in the parentheses within the command line, as shown below. Bash will attempt to execute the calculation and return the result.
Bash> echo $ ((16/2))
8

9. Custom Shell Prompt
You can customize the Bash shell prompt to display the current user name and hostname, current time, average load, and/or current working directory. To achieve this, change the $PS1 variable as follows:
Bash> ps1= ' \u@\h:\w \@> '
Bash> Export PS1
Root@medusa:/tmp 03:01 pm>

This displays the name, host name, current working directory, and current time of the currently logged-on user at the shell prompt.

10. Get the specified context Help
Bash can give you help with all the built-in commands. To view a list of all built-in commands, enter Help. To get help with a particular command, enter the command, where you want to see the help. Here is an example:
Bash> Help Alias
... Some help text ...

Obviously, you can get detailed help for the bash shell at any time by typing man bash at the command prompt.

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.