mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
commit
06f3c36ea6
@ -1,156 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Make Bash history more useful with these tips)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-history-control)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
Make Bash history more useful with these tips
|
||||
======
|
||||
Tell Bash what you want it to remember—or even rewrite history by
|
||||
deleting entries you don't want or need.
|
||||
![A person programming][1]
|
||||
|
||||
A Linux terminal running [Bash][2] has a built-in history that you can use to track what you've been doing lately. To view a history of your Bash session, use the built-in command `history`:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
foo
|
||||
$ echo "bar"
|
||||
bar
|
||||
$ history
|
||||
1 echo "foo"
|
||||
2 echo "bar"
|
||||
3 history
|
||||
```
|
||||
|
||||
The `history` command isn't an executable file on your filesystem, like most commands, but a function of Bash. You can verify this by using the `type` command:
|
||||
|
||||
|
||||
```
|
||||
$ type history
|
||||
history is a shell builtin
|
||||
```
|
||||
|
||||
### History control
|
||||
|
||||
The upper limit of lines in your shell history is defined by the `HISTSIZE` variable. You can set this variable in your `.bashrc` file. The following sets your history to 3,000 lines, after which the oldest line is removed to make room for the newest command, placed at the bottom of the list:
|
||||
|
||||
|
||||
```
|
||||
`export HISTSIZE=3000`
|
||||
```
|
||||
|
||||
There are other history-related variables, too. The `HISTCONTROL` variable controls what history is stored. You can force Bash to exclude commands starting with empty space by placing this in your `.bashrc` file:
|
||||
|
||||
|
||||
```
|
||||
`export HISTCONTROL=$HISTCONTROL:ignorespace`
|
||||
```
|
||||
|
||||
Now, if you type a command starting with a space, that command won't get recorded in history:
|
||||
|
||||
|
||||
```
|
||||
$ echo "hello"
|
||||
$ mysql -u bogus -h badpassword123 mydatabase
|
||||
$ echo "world"
|
||||
$ history
|
||||
1 echo "hello"
|
||||
2 echo "world"
|
||||
3 history
|
||||
```
|
||||
|
||||
You can avoid duplicate entries, too:
|
||||
|
||||
|
||||
```
|
||||
`export HISTCONTROL=$HISTCONTROL:ignoredups`
|
||||
```
|
||||
|
||||
Now, if you type two commands, one after another, only one appears in history:
|
||||
|
||||
|
||||
```
|
||||
$ ls
|
||||
$ ls
|
||||
$ ls
|
||||
$ history
|
||||
1 ls
|
||||
2 history
|
||||
```
|
||||
|
||||
If you like both of these ignores, you can just use `ignoreboth`:
|
||||
|
||||
|
||||
```
|
||||
`export HISTCONTROL=$HISTCONTROL:ignoreboth`
|
||||
```
|
||||
|
||||
### Remove a command from history
|
||||
|
||||
Sometimes you make a mistake and type something sensitive into your shell, or maybe you just want to clean up your history so that it more accurately represents the steps you took to get something working correctly. If you want to delete a command from your history in Bash, use the `-d` option with the line number of the item you want to remove:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
foo
|
||||
$ echo "bar"
|
||||
bar
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
536 echo "bar"
|
||||
537 history | tail
|
||||
$ history -d 536
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
536 history | tail
|
||||
537 history -d 536
|
||||
538 history | tail
|
||||
```
|
||||
|
||||
To stop adding `history` entries, you can place a `space` before the command, as long as you have `ignorespace` in your `HISTCONTROL` environment variable:
|
||||
|
||||
|
||||
```
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
536 echo "bar"
|
||||
$ history -d 536
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
```
|
||||
|
||||
You can clear your entire session history with the `-c` option:
|
||||
|
||||
|
||||
```
|
||||
$ history -c
|
||||
$ history
|
||||
$
|
||||
```
|
||||
|
||||
### History lessons
|
||||
|
||||
Manipulating history is usually less dangerous than it sounds, especially when you're curating it with a purpose in mind. For instance, if you're documenting a complex problem, it's often best to use your session history to record your commands because, by slotting them into your history, you're running them and thereby testing the process. Very often, documenting without doing leads to overlooking small steps or writing minor details wrong.
|
||||
|
||||
Use your history sessions as needed, and exercise your power over history wisely. Happy history hacking!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-history-control
|
||||
|
||||
作者:[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/computer_keyboard_laptop_development_code_woman.png?itok=vbYz6jjb (A person programming)
|
||||
[2]: https://opensource.com/resources/what-bash
|
@ -0,0 +1,155 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Make Bash history more useful with these tips)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-history-control)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
这些技巧让 Bash history 更加有用
|
||||
======
|
||||
告诉 Bash 你想要它记住什么,甚至删除不需要的记录重写历史。
|
||||
![A person programming][1]
|
||||
|
||||
运行 [Bash][2] 的 Linux 终端有内置的历史记录,你可以用来跟踪最近的操作。要查看你的 Bash 会话的历史记录,请使用内置命令 `history`:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
foo
|
||||
$ echo "bar"
|
||||
bar
|
||||
$ history
|
||||
1 echo "foo"
|
||||
2 echo "bar"
|
||||
3 history
|
||||
```
|
||||
|
||||
与大多数命令不一样,`history` 命令不是文件系统上的可执行文件,而是 Bash 的功能。你可以使用 `type` 命令来验证:
|
||||
|
||||
|
||||
```
|
||||
$ type history
|
||||
history is a shell builtin
|
||||
```
|
||||
|
||||
### 历史控制
|
||||
|
||||
shell 历史记录的行数上限由 `HISTSIZE` 变量定义。你可以在 .bashrc 文件中设置此变量。以下将你的历史记录设置为 3,000 行,之后将最早的行删除以为最新命令腾出空间,该命令位于列表的底部:
|
||||
|
||||
|
||||
```
|
||||
`export HISTSIZE=3000`
|
||||
```
|
||||
|
||||
还有其他与历史相关的变量。 `HISTCONTROL` 变量控制哪些历史被记录。你可以在 `.bashrc` 中写入下面的行来强制 Bash 排除以空格开头的命令:
|
||||
|
||||
|
||||
```
|
||||
`export HISTCONTROL=$HISTCONTROL:ignorespace`
|
||||
```
|
||||
|
||||
现在,如果你输入以空格开头的命令,那么它将不会记录在历史记录中:
|
||||
|
||||
|
||||
```
|
||||
$ echo "hello"
|
||||
$ mysql -u bogus -h badpassword123 mydatabase
|
||||
$ echo "world"
|
||||
$ history
|
||||
1 echo "hello"
|
||||
2 echo "world"
|
||||
3 history
|
||||
```
|
||||
|
||||
你也可以避免重复的条目:
|
||||
|
||||
|
||||
```
|
||||
`export HISTCONTROL=$HISTCONTROL:ignoredups`
|
||||
```
|
||||
|
||||
现在,如果你一个接着一个输入两个命令,历史记录中只会显示一个:
|
||||
|
||||
|
||||
```
|
||||
$ ls
|
||||
$ ls
|
||||
$ ls
|
||||
$ history
|
||||
1 ls
|
||||
2 history
|
||||
```
|
||||
|
||||
如果你喜欢这两个忽略,那么可以使用 `ignoreboth`:
|
||||
|
||||
|
||||
```
|
||||
`export HISTCONTROL=$HISTCONTROL:ignoreboth`
|
||||
```
|
||||
|
||||
### 从历史记录中删除命令
|
||||
|
||||
有时你会犯一个错误,在 shell 中输入了一些敏感内容,或者你只是想清理历史记录,以便它更准确地表示让某件事正常工作所采取的步骤。如果要从 Bash 的历史记录中删除命令,请在要删除的项目的行号上使用 `-d` 选项:
|
||||
|
||||
|
||||
```
|
||||
$ echo "foo"
|
||||
foo
|
||||
$ echo "bar"
|
||||
bar
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
536 echo "bar"
|
||||
537 history | tail
|
||||
$ history -d 536
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
536 history | tail
|
||||
537 history -d 536
|
||||
538 history | tail
|
||||
```
|
||||
|
||||
要停止添加 `history` 条目,只要在 `HISTCONTROL` 环境变量中有 `ignorespace`,就可以在命令前添加`空格`:
|
||||
|
||||
|
||||
```
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
536 echo "bar"
|
||||
$ history -d 536
|
||||
$ history | tail
|
||||
535 echo "foo"
|
||||
```
|
||||
|
||||
你可以使用 -c 选项清除所有会话历史记录:
|
||||
|
||||
|
||||
```
|
||||
$ history -c
|
||||
$ history
|
||||
$
|
||||
```
|
||||
|
||||
### history 命令教训
|
||||
|
||||
操纵历史通常没有听起来那么危险,尤其是当你有目标地管理它时。例如,如果你要记录一个复杂的问题,通常最好使用会话历史来记录命令,因为通过将命令插入历史记录,你能运行它们并从而测试过程。很多时候,不执行历史命令会导致忽略小的步骤或写错小细节。。
|
||||
|
||||
按需使用历史会话,并明智地控制历史记录。享受历史修改吧!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-history-control
|
||||
|
||||
作者:[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/computer_keyboard_laptop_development_code_woman.png?itok=vbYz6jjb (A person programming)
|
||||
[2]: https://opensource.com/resources/what-bash
|
Loading…
Reference in New Issue
Block a user