Hello, I’m Liang Xu

You can use the history command to view the command history, but this is not the only function of the history command. In particular, the Bash version of the history command provides more functionality than any other Linux Shell history command.

Bash has a long history as an old Shell, and it has an even older predecessor, the Bourne Shell (sh). As a result, Bash’s history command is the most feature-rich of all the Linux Shell history commands. The Bash version of the history command supports not only reverse searching, quick invocation, but rewriting history, and more.

All of the above functions can improve your productivity, so let Ryu take a look at the Bash History command and its common functions:

History is the built-in command

The history command is different from many other commands. You may be used to commands being placed as executables in common system-level locations such as /usr/bin, /usr/local/bin, or ~ /bin. However, the built-in history command is not in the PATH saved by your environment variable PATH.

In fact, the history command is not stored in a physical location:

$ which history

which: no history in [PATH]
Copy the code

History is actually a built-in function of the Shell itself:

$ type history history is a shell builtin $ help history history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]  Display or manipulate the history list. [...]Copy the code

Because history is built into the Shell, the history function is unique to each Shell. Therefore, functions that you can use in Bash may not be available in Tcsh, Fish, or Dash, and likewise, functions that you can use in Tcsh, Fish, or Dash may not be available in Bash.

Check your Bash command history

The most basic and frequent use of the history command isto view the command history of your Shell session:

$ echo "hello"
hello
$ echo "world"
world
$ history
  1  echo "hello"
  2  echo "world"
  3  history
Copy the code

Event prompt

Event prompt (!) It searches history by event. By events, I mean every command recorded in history. In other words, it is a line of command marked with a numeric index for reference.

To rerun a command in history, use the! Simply add (without Spaces) the index number that precedes the command you want to run. For example, suppose the first instruction in history is echo Hello, and you want to rerun it:

$ !1
echo "hello"
hello
Copy the code

You can also use relative positioning by providing negative lines starting from the current position in history. For example, return the third-to-last command in history:

$ echo "alvin" alvin $ echo "hello" hello $ echo "world" world $ ! -3 echo "alvin" alvinCopy the code

If you only want to return the last command, you can use shorthand!! To replace! 1. This saves a whole keystroke time!!

$ echo "alvin"
alvin
$ !!
echo "alvin"
alvin
Copy the code

String search

You can also search for commands in history and run them through a specific string.

If you want to search for commands that start with a particular string, use! Add (without Spaces) the string you want to search for:

$ echo "alvin" alvin $ true $ false $ ! echo echo "alvin" alvinCopy the code

You can also search for commands that contain a particular string in any location. To do that, you just need to use! Just add it to the front and the back, right? Enclosing a specific string, like this:

$ echo "alvin" alvin $ true $ false $ ! ? alvin? echo "alvin" alvinCopy the code

If you know that the string you want to search for is at the end of the command, you can omit the string, right? , like this:

$ echo alvin alvin $ ! ? alvin echo alvin alvinCopy the code

Note that if there is more than one command in the history that contains the target string, it will only execute the last of the qualified commands:

$ echo "hello world" hello world $ echo "hello alvin" hello alvin $ ! ? hello? echo "hello alvin" hello alvinCopy the code

String substitution

You can change the command by searching for a particular string and replacing it with a new string:

$ echo "hello"
hello
$ echo "world"
world
$ ^hello^alvin
echo "alvin"
alvin
Copy the code

But it can only replace the first occurrence of the target string, if the command has two occurrences of the target string, only the first occurrence of the target string will be replaced, like this:

$ echo "hello hello"
hello hello
$ ^hello^alvin
echo "alvin hello"
alvin hello
Copy the code

As with string search, when more than one command in the history contains the target string, only the last one is replaced and executed:

$ echo "hello world"
hello world
$ echo "hello"
hello
$ ^hello^alvin
echo "alvin"
alvin
Copy the code

Make full use of the history command

Bash’s history command actually does a lot more than that, but it’s a good start to getting used to using the history command rather than just looking at history. Use the history command often. You’ll be amazed at how much you can accomplish using the history command without typing a specific command.

If you have any questions about the history command, or if you know of any useful features of the history command, please let me know in the comments.

> < p style = “max-width: 100%; clear: both
#### read all is true love, a praise to walk again? Your “sanlian” is the biggest motivation for liang Xu’s continuous creation!
1\. Follow the ** original ** * public account “** Liangxu Linux**”, the first time to get the latest Linux dry goods!
2. Public account background reply [information] [interview] [resume] to get selected first-line big factory interview, self-improvement, resume and other information.
3\. Follow my blog: [lxlinux.net]([http://www.lxlinux.net]](http://www.lxlinux.net%29/)