没有行号的Bash历史记录

cwd*_*cwd 116 linux bash

bash history命令非常酷.我理解为什么它显示行号,但有没有办法可以调用历史命令并抑制行号?

这里的要点是使用history命令,所以请不要回复 cat ~/.bash_history

电流输出:

  529 man history
  530 ls
  531 ll
  532 clear
  533 cd ~
  534 history
Run Code Online (Sandbox Code Playgroud)

历史图形来源.

期望的输出:

man history
ls
ll
clear
cd ~
history
Run Code Online (Sandbox Code Playgroud)

历史图形来源.

感谢大家为您提供的出色解决方案.保罗是最简单的,并且对我有用,因为我的bash历史大小设定为2000.

我还想分享今天早上发现的一篇很酷的文章.它有几个很好的选项,我现在正在使用,比如保留重复的条目超出bash历史记录,并确保多个bash会话不会覆盖历史文件:http://blog.macromates.com/2008/working-with -history合的bash /

Pau*_*l R 183

试试这个:

$ history | cut -c 8-
Run Code Online (Sandbox Code Playgroud)

  • `man cut`.它正在删除`history`命令的每行输出的前7个字符.它应该只有问题,如果数量超过99,999,这是我从未见过的(我使用shell*很多*).但如果你担心的话:`历史| sed's/^*[0-9]**//'` (27认同)
  • @cwd:如果您的历史记录中有100,000条命令,则应该退出键盘并回家.如果你已经回家了,就到外面去吧.8-)}(是的,我知道历史可以跨会话保留.) (5认同)

Zso*_*kai 20

awk 可以帮助:

history|awk '{$1="";print substr($0,2)}'
Run Code Online (Sandbox Code Playgroud)

如果您有悠久的历史,这个答案可能会失败.


cze*_*rny 16

history -w /dev/stdout
Run Code Online (Sandbox Code Playgroud)

从输出history --help

-w 将当前历史写入历史文件

它将当前历史记录写入指定文件 -/dev/stdout在这种情况下。

  • 这实际上是实践中最简单的记住!干净简单 - 而且您无需了解 regex/sed/awk :-) (3认同)

Att*_*igh 12

我很清楚这个问题是针对bash的,很多人宁愿不转换为zsh(cue downvotes ...)

但是,如果您愿意切换到zsh,那么zsh本机支持(以及历史格式化的其他选项)

zsh> fc -ln 0
Run Code Online (Sandbox Code Playgroud)

(参见https://serverfault.com/questions/114988/removing-history-or-line-numbers-from-zsh-history-file)

  • 实际上`fc`也是一个'bash`内置.唯一的区别是第一行是"1",所以它是`fc -ln 1` (6认同)

iMi*_*Mil 11

我就这一个较晚,但更短的方法是添加在你的下面~/.bashrc~/.profile文件:

HISTTIMEFORMAT="$(echo -e '\r\e[K')"

来自bash manpage:

       HISTTIMEFORMAT
              If this variable is set and not null, its value is used as a
              format string for strftime(3) to print the time stamp associated
              with each history entry displayed by the history builtin.  If
              this variable is set, time stamps are written to the history
              file so they may be preserved across shell sessions.  This uses
              the history comment character to distinguish timestamps from
              other history lines.

使用此功能,智能黑客包括使变量"打印"回车符(\r)并清除行(ANSI代码K)而不是实际时间戳.

  • 还有一个单行选项:`HISTTIMEFORMAT = $'\ r \n [K'历史` (4认同)

Man*_*y D 5

或者,您可以使用sed:

history | sed 's/^[ ]*[0-9]\+[ ]*//'
Run Code Online (Sandbox Code Playgroud)

使用别名,您可以将其设置为标准(将其粘贴在bash_profile中):

alias history="history | sed 's/^[ ]*[0-9]\+[ ]*//'"
Run Code Online (Sandbox Code Playgroud)


小智 5

尽管使用-c选项cut对大多数实际目的都有效,但我认为将历史记录到 awk 将是更好的解决方案。例如:

history | awk '{ $1=""; print }'
Run Code Online (Sandbox Code Playgroud)

或者

history | awk '{ $1=""; print $0 }'
Run Code Online (Sandbox Code Playgroud)

这两种解决方案都做同样的事情。历史的输出被馈送到 awk。然后,Awk 将第一列清空,该列对应于历史命令输出中的数字。这里 awk 更方便,因为您不必关心输出数字部分中的字符数。

print $0等价于print,因为默认是打印出现在行上的所有内容。打字print $0更明确,但您选择哪一个取决于您。如果您使用 awk 来打印文件print $0print当与 awk 一起使用时and simple的行为会更加明显(cat键入会比 awk 更快,但这是为了说明一点)。

[Ex] 使用 awk 显示 $0 文件的内容

$ awk '{print $0}' /tmp/hello-world.txt
Hello World!
Run Code Online (Sandbox Code Playgroud)

[Ex] 使用 awk 显示文件内容,不显式 $0

$ awk '{print}' /tmp/hello-world.txt
Hello World!
Run Code Online (Sandbox Code Playgroud)

[Ex] 当历史行跨越多行时使用 awk

$ history
   11  clear
   12  echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style."

$ history | awk ' $1=""; {print}'
 clear
 echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style."
Run Code Online (Sandbox Code Playgroud)