这些技巧让 Bash 命令历史更加有用 | Linux 中国
https://linux.cn/article-12381-1.html
作者:Seth Kenlon
译者:geekpi
$ echo"foo"
foo
$ echo"bar"
bar
$ history
1echo"foo"
2echo"bar"
3 history
$ 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
1echo"hello"
2echo"world"
3 history
你也可以避免重复的条目:
export HISTCONTROL=$HISTCONTROL:ignoredups
现在,如果你一个接着一个输入两个命令,历史记录中只会显示一个:
$ ls
$ ls
$ ls
$ history
1ls
2 history
如果你喜欢这两个忽略功能,那么可以使用
ignoreboth
:export HISTCONTROL=$HISTCONTROL:ignoreboth
从历史记录中删除命令
有时你会犯一个错误,在 shell 中输入了一些敏感内容,或者你只是想清理历史记录,以便它更准确地表示让某件事正常工作所采取的步骤。如果要从 Bash 的历史记录中删除命令,请在要删除的项目的行号上使用
-d
选项:$ echo"foo"
foo
$ echo"bar"
bar
$ history |tail
535echo"foo"
536echo"bar"
537 history |tail
$ history -d 536
$ history |tail
535echo"foo"
536 history |tail
537 history -d 536
538 history |tail
$ history |tail
535echo"foo"
536echo"bar"
$ history -d 536
$ history |tail
535echo"foo"
你可以使用
-c
选项清除所有会话历史记录:$ history -c
$ history
$
history 命令的经验教训
操纵历史通常没有听起来那么危险,尤其是当你有目标地管理它时。例如,如果你要记录一个复杂的问题,通常最好使用会话历史来记录命令,因为通过将命令插入历史记录,你能运行它们并从而测试过程。很多时候,不执行历史命令会导致忽略小的步骤或写错小细节。
按需使用历史会话,并明智地控制历史记录。享受历史修改吧!
关键词
命令
文件
变量
有用
中国
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。