You should know the 15 Linux Bash Extensions example __linux

Source: Internet
Author: User
Tags modifier modifiers


Bash has a long history, and the way you know how to use it will make you very creative with the Linux command line.

This article explains 15 examples of using bash:

The practice designator----refers to the special order of history. Begin.

The word designator----refers to a specific historical entry. This even indicator and word designator are separated by spaces

Modifier----The result of modifying an event or Word descriptor


This article is part of my ongoing Bash Tutorial series


As you know, view the history and use the history command. Usually soldiers all the commands that were executed earlier are in the table below.

$ history
1 tar cvf etc.tar/etc/
2 cp/etc/passwd/backup
3 ps-ef | grep http
4 service sshd restart
  
   5/usr/local/apache2/bin/apachectl restart
  

Bash's historical event modifier

1. Perform specific commands from the history list. N (n is line number)

If you have previously executed a command, you do not have to re-enter this command, you can execute it directly with the command line number shown in the History list.

For example, execute the command #4, and do it in the following ways

$! 4
Service sshd restart

The command to execute the command just executed.

$!-2

Execute the command you just executed

$ !!

$!-1

You can also press <ctrl>-p (if you are in the default Emacs mode) to enter the previous command


If you are using the vi style of editing, you can use the ' Set-o vi ', with <ESC >-K to enter the previous command.

2. Perform keyboard input commands. String and.. String

You can also use the keyboard to perform commands that have been executed in the history list.


The next example will search for the previous command to start with ' PS ' and execute it.

$!ps
Ps-ef | grep http

The next example searches for the previous command containing the "Apache" keyword and executes it. In this case, it looks up the previous "/usr/local/apache2/bin/apachectl restart"

and execute it.

$ !? Apache
/usr/local/apache2/bin/apachectl Restart

3. Replace the character from the previous command from the use of ^STR1^STR2

In the next example, we first execute the LS command to validate a file. In the future we will realize that we want to see the contents of the file. And then we're not.

Print the entire command, instead of using cat to replace LS, as shown below

$ ls/etc/cron.daily/logrotate

$ ^ls^cat^
cat/etc/cron.daily/logrotate
Note: To learn more about history usage, you can refer to 15 for examples of history command lines. This explains how to show time,

How to use a variety of historical-related environmental variables including Histtimeformat, Histsize,histfile,histcontrol, and Histignore


Bash History Word designators

The word designator is useful when you print a new command, in addition to using the parameters in the execution of the previous command. Here are some examples


4. Get the parameters for the previous command: ^

The next example, ". cp:^ "used as an argument for" Ls-al ". “。 cp:^ "Locates commands in the previous command that begin with" CP "and obtains its first argument

$ cp/etc/passwd/backup

$ ls-l!cp:^
ls-l/etc/passwd


The following example gets the first argument of the previous command

$ ls-l!!:^

5. Get the last parameter of a command: $

In the next example, ". cp:$ "as a parameter of the Ls-al, which locates the previous command in the history list with" CP "and obtains its last argument.


$ cp/etc/passwd/backup

$ ls-l!cp:$
ls-l/backup

The next example gets the last argument of the previous command

$ls-L!!:$


6. Get n parameters to use: N

In the next example, ". Tar:2 "used as an argument for the Ls-l command, which is positioned to fall into a parameter before the command


$ tar cvfz/backup/home-dir-backup.tar.gz/home

$ ls-l!tar:2
ls-l/backup/home-dir-backup.tar.gz

7. The parameters used for the command are: *

In the next example, ". cp:* is used as a ls-l parameter that locates commands in the previous command that start with the CP and obtains all of its parameters.


$ cp/etc/passwd/backup

$ ls-l!cp:*
ls-l/etc/passwd/backup


8. Refer to the most recently searched word. %

As we have stated above, ". Apche "Searches for commands that contain Apache keywords in previous commands and executes them.

$/usr/local/apache2/bin/apachectl Restart

$!? Apache
/usr/local/apache2/bin/apachectl Restart

!% will point before. The entire phrase or word that was searched.


For example, if you have previously searched ". Apache, then "!%" will match "/usr/local/apache2/bin/apachectl". Note that the "/" here is considered to be the

Part of the sentence.


So, in this case, execute the following, you can stop Apache.

$!% Stop
/usr/local/apache2/bin/apachectl stop

9. Get a certain range of parameters using X-y

In the next example, ". Tar:3-5 "As a ls-l parameter,". Tar:3-5 "Locate the command in the previous command that starts with" tar "and get the arguments between its 第3-5


$ tar cvf Home-dir.tar John Jason Ramesh Rita

$ ls-l!tar:3-5 ls-l
John Jason Ramesh

The following example gets all the arguments (starting from 2)

$ ls-l!tar:2-$

Please note the following:

。。 : * Get all the parameters of the previous command.

。。 : 2* obtains all parameters starting from the second.

。。 : 2-$ is the same as above. Gets all the arguments starting with the second.

。。 : 2-Get all the arguments, starting with the second (except the last argument)


Bash History Modifiers

modifiers, after a word indicator, are interpreted as follows:


10. The file name after removing the path uses: H

In the next example, ". : $:h "Get the last parameter of the previous command, clear and remove the path Ming, in this case, it removes the filename, it gets the path name

$ ls-l/very/long/path/name/file-name.txt

$ ls-l!!:$:h
ls-l/very/long/path/name

11. Remove all boot paths from the word use: t

This is obviously the opposite of the previous example

In the next example, "!!:$:t" gets the last parameter of the previous command, removing all the boot path names and only the filename.


$ ls-l/very/long/path/name/file-name.txt

$ ls-l!!:$:t
ls-l file-name.txt


12. Remove file extensions by: R

In the next example, "!!:$:r" gets the last parameter of the previous command and removes the. suffix (file name extension), in which case it removes the. txt


$ ls-l/very/long/path/name/file-name.txt

$ ls-l!!:$:r
ls-l/very/long/path/name/file-name

13. Sed is like substitution in bash history: s/str1/str2/

Instead of using ^original^replacement^ we discussed earlier, we can also use sed like substitution in the history of bash. This could be easy to remember.

。。 Invoke the previous command, ": s/original-string/replacement-string/" is a sed-like syntax replacement string


$!!:s/ls-l/cat/
You can also use the G tag as a global replacement, as shown below. This is very useful when you want to replace the big words in the global.


In the next example, I used password two times instead of passwd.


$ cp/etc/password/backup/password.bak

Solve him, with the global history of sed like substitution.


$!!:gs/password/passwd/
Cp/etc/passwd/backup/passwd.bak

14. Repeat Replace with: &

If you have successfully executed a bash history replacement, as shown above, you can repeat the same replacement using: &


I entered the wrong "password" instead of "passwd" in another


$ tar cvf Password.tar/etc/password
Now, replace the re-enter command, or "gs/password/passwd", I just use: & to reuse the last substitution. Use ":g&" to use the last

Global substitution.


$!!:g&
tar cvf passwd.tar/etc/passwd

15. Print a command without performing a use:p

This is useful when you make complex command substitutions or if you want to see the last command before executing.


In the next example, ". tar:3-:p ", and there is no real execution command.


Since we have given:; He's just making some substitutions to show the new commands once you've verified that bash history expands and if you want this command you're going to

execution, removal: p executes him again.


$ tar cvf Home-dir.tar John Jason Ramesh Rita

$ tar cvfz new-file.tar!tar:3-:p
tar cvfz new-file.tar John Jason R Amesh


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.