mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
commit
a701c9d251
@ -1,159 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to use Bash history commands)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-history-commands)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
How to use Bash history commands
|
||||
======
|
||||
Bash's history command surpasses all other Linux shell history
|
||||
interfaces in the number of features it offers.
|
||||
![Person typing on a 1980's computer][1]
|
||||
|
||||
Bash has a rich history. That is, it's an old shell with an even older ancestor (the Bourne shell), but it also has a great `history` command that surpasses all other shell history interfaces based on its number of features. The [Bash][2] version of `history` allows for reverse searches, quick recall, rewriting history, and more.
|
||||
|
||||
The `history` command isn't like many other commands. You might be used to commands being executable files placed in common system-level locations like `/usr/bin`, `/usr/local/bin`, or `~/bin`. The built-in `history` command isn't in your `PATH` and has no physical location:
|
||||
|
||||
|
||||
```
|
||||
$ which history
|
||||
|
||||
which: no history in [PATH]
|
||||
```
|
||||
|
||||
Instead, `history` is 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.
|
||||
[...]
|
||||
```
|
||||
|
||||
For that reason, the history function in each shell is unique, so what you use in Bash may not work in Tcsh or Fish or Dash, and what you use in those may not work in Bash. In some cases, knowing what Bash can do may inspire users of other shells to create interesting hacks to clone Bash behavior, and it may unlock Bash features that you never knew existed.
|
||||
|
||||
### View your Bash history
|
||||
|
||||
The most basic and frequent use of the `history` command is to view a history of your shell session:
|
||||
|
||||
|
||||
```
|
||||
$ echo "hello"
|
||||
hello
|
||||
$ echo "world"
|
||||
world
|
||||
$ history
|
||||
1 echo "hello"
|
||||
2 echo "world"
|
||||
3 history
|
||||
```
|
||||
|
||||
### Event designators
|
||||
|
||||
Event designators search through your history by event. An _event_ in this context is a command logged in your history, delineated by a newline character. In other words, it's one line, marked by an index number for reference.
|
||||
|
||||
Event designators mostly start with an exclamation point, sometimes also called a _bang_ (`!`).
|
||||
|
||||
To rerun a command from your history, use the exclamation point followed immediately (no spaces) by the index number of the command you want. For instance, assume line 1 contains the command `echo "hello"`, and you want to run it again:
|
||||
|
||||
|
||||
```
|
||||
$ !1
|
||||
echo "hello"
|
||||
hello
|
||||
```
|
||||
|
||||
You can use relative positioning by providing a negative number of lines back from your current position in history. For example, to go back three entries in history:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
foo
|
||||
$ echo "bar"
|
||||
bar
|
||||
$ echo "baz"
|
||||
baz
|
||||
$ !-3
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
If you're just going back one line, you can use the shorthand `!!` instead of `!-1`. That's a savings of one whole keystroke!
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
$ !!
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
### String search
|
||||
|
||||
You can also search for a specific string through the entries, in reverse, for a command to run. To search for a command _starting_ with a specific string, use an exclamation point followed immediately (no space) by the string you want to search for:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
$ true
|
||||
$ false
|
||||
$ !echo
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
You can also search for a command containing a string in any position (not just at the start). To do that, use `!` plus the string you're searching for, as usual, but surround the string with question marks on either end. You may omit the trailing question mark if you know that the string is immediately followed by a newline character (meaning it's the last thing you typed before you pressed **Return**):
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
$ true
|
||||
$ false
|
||||
$ !?foo?
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
### String substitution
|
||||
|
||||
Similar to searching for strings at the start of a line, you can search for a string and replace it with a new string, changing the command:
|
||||
|
||||
|
||||
```
|
||||
$ echo "hello"
|
||||
hello
|
||||
$ echo "world"
|
||||
world
|
||||
$ ^hello^foo
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
### Make history useful
|
||||
|
||||
In Bash, the history command is capable of much more than what's been covered here, but this is a good start for getting used to _using_ your history instead of just treating it as a reference. Use the `history` command often, and see how much you can do without having to type commands. You might surprise yourself!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-history-commands
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/1980s-computer-yearbook.png?itok=eGOYEKK- (Person typing on a 1980's computer)
|
||||
[2]: https://opensource.com/resources/what-bash
|
158
translated/tech/20200615 How to use Bash history commands.md
Normal file
158
translated/tech/20200615 How to use Bash history commands.md
Normal file
@ -0,0 +1,158 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to use Bash history commands)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-history-commands)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
如何使用 Bash history 命令
|
||||
======
|
||||
Bash 的 history 命令超过了所有其他 Linux Shell history 接口提供的功能数量。
|
||||
![Person typing on a 1980's computer][1]
|
||||
|
||||
Bash 有丰富的历史。 也就是说,它是一个久远的 shell(Bourne shell),但是它还有一个很棒的 `history` 命令,它的功能数量超过了所有其他 shell 的 history 接口。 [Bash][2]版本的 `history` 可进行反向搜索、快速调用、重写历史记录等。
|
||||
|
||||
`history` 命令与许多其他命令不同。 你可能习惯于将命令作为可执行文件放在常见的系统级位置,例如 `/usr/bin`、`/usr/local/bin` 或者 `~/bin`。 内置的 `history` 命令不在你的 `PATH` 中并且没有物理位置:
|
||||
|
||||
|
||||
```
|
||||
$ which history
|
||||
|
||||
which: no history in [PATH]
|
||||
```
|
||||
|
||||
相反,`history` 是 shell 本身的内置函数:
|
||||
|
||||
|
||||
```
|
||||
$ 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.
|
||||
[...]
|
||||
```
|
||||
|
||||
因此,每个 shell 中的历史功能都是唯一的,因此你在 Bash 中使用的内容可能无法在 Tcsh 或 Fish 或 Dash 中使用,而在这些 shell 中使用的内容可能也无法在 Bash 中使用。在某些情况下,了解 Bash 可以做什么可能会激发其他 shell 的用户创建有趣的改造来复制 Bash 行为,并且可能会解锁你从未知道的 Bash 功能。
|
||||
|
||||
### 查看你的 Bash 历史
|
||||
|
||||
`history` 命令最基本,最频繁的用法是查看 shell 会话的历史记录:
|
||||
|
||||
|
||||
```
|
||||
$ echo "hello"
|
||||
hello
|
||||
$ echo "world"
|
||||
world
|
||||
$ history
|
||||
1 echo "hello"
|
||||
2 echo "world"
|
||||
3 history
|
||||
```
|
||||
|
||||
### 事件指示器
|
||||
|
||||
事件指示器 (Event designator) 按事件搜索你的历史记录。在这里,_事件_ (enent) 是记录在历史中的命令,以换行符表示。换句话说,它是一行,以索引号来标记。
|
||||
|
||||
事件指示器大多以感叹号开头,有时也称为 _bang_(`!`)。
|
||||
|
||||
要从你的历史记录中重新运行命令,请使用感叹号,之后紧跟(无空格)所需命令的索引号。例如,假设第 1 行包含命令 `echo "hello"`,你想要想再次运行它:
|
||||
|
||||
|
||||
```
|
||||
$ !1
|
||||
echo "hello"
|
||||
hello
|
||||
```
|
||||
|
||||
你可以在 history 中使用基于你当前位置的负数行号进行相对定位。例如,返回到倒数第三个条目:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
foo
|
||||
$ echo "bar"
|
||||
bar
|
||||
$ echo "baz"
|
||||
baz
|
||||
$ !-3
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
如果只想回去一行,那么可以使用速记 `!!` 代替 `!-1`。这节省了按键时间!
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
$ !!
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
### 字符串搜索
|
||||
|
||||
你也可以反过来通过条目搜索特定的字符串来运行命令。要搜索以指定字符串_开始_的命令,请使用感叹号,之后紧跟(无空格)要搜索的字符串:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
$ true
|
||||
$ false
|
||||
$ !echo
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
你还可以搜索在任意位置(不仅是开头)包含该字符串的命令。为此,请像之前一样使用 `!` 加上要搜索的字符串,但在字符串的两端都用问号围绕起来。如果你知道该字符串后紧跟一个换行符,那么可以省略最后的问号(就是在按**回车**之前输入的最后字符):
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
$ true
|
||||
$ false
|
||||
$ !?foo?
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
### 字符串替换
|
||||
|
||||
类似于在行首搜索字符串,你可以搜索字符串并用新字符串替换它,更改命令:
|
||||
|
||||
|
||||
```
|
||||
$ echo "hello"
|
||||
hello
|
||||
$ echo "world"
|
||||
world
|
||||
$ ^hello^foo
|
||||
echo "foo"
|
||||
foo
|
||||
```
|
||||
|
||||
### 让 history 有用
|
||||
|
||||
在 Bash 中,history 命令的功能远远超过此处介绍的内容,但这是让你习惯_使用_ history 而不仅仅是作为参考的良好起点。 经常使用 `history` 命令,并试试无需输入命令即可执行的操作。 你可能会感到惊讶!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-history-commands
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/1980s-computer-yearbook.png?itok=eGOYEKK- (Person typing on a 1980's computer)
|
||||
[2]: https://opensource.com/resources/what-bash
|
Loading…
Reference in New Issue
Block a user