有没有一种方法可以将bash中的历史记录列表的大小设置为5000行以上?


25

无论我将HISTSIZE环境变量设置为大于5000 多少,使用history内置功能打印历史记录列表时,它只会打印最后的5000条命令。我之所以需要它,是因为我通常有一个.bash_history超过5000行的大行,有时需要通过按来解决早期命令Ctrl-R,但是如果该命令早于5000行,那么我将无法使用该机制进行访问。我知道我可以grep在上使用.bash_history,但是我认为该Ctrl-R机制会更快(更方便)。我使用gnu bash 4.1版。

那就是我的.bashrc文件的全部内容:

    #!/bin/bash
    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples

    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return

    # don't put duplicate lines in the history. See bash(1) for more options
    # ... or force ignoredups and ignorespace
    #HISTCONTROL=ignoredups:ignorespace:erasedups

    # append to the history file, don't overwrite it
    shopt -s histappend

    # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
    HISTSIZE=50000
    HISTFILESIZE=500000

    # check the window size after each command and, if necessary,
    # update the values of LINES and COLUMNS.
    shopt -s checkwinsize

    # make less more friendly for non-text input files, see lesspipe(1)
    [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

    # set variable identifying the chroot you work in (used in the prompt below)
    if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
        debian_chroot=$(cat /etc/debian_chroot)
    fi

    # set a fancy prompt (non-color, unless we know we "want" color)
    case "$TERM" in
        xterm-color) color_prompt=yes;;
    esac

    # uncomment for a colored prompt, if the terminal has the capability; turned
    # off by default to not distract the user: the focus in a terminal window
    # should be on the output of commands, not on the prompt
    #force_color_prompt=yes

    if [ -n "$force_color_prompt" ]; then
        if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes

        else
        color_prompt=

        fi
    fi

    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\         [\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    else
        PS1='${debian_chroot:+($debian_chroot)}\@-\u@\h:\w\$ '
    fi
    unset color_prompt force_color_prompt

    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac

    # enable color support of ls and also add handy aliases
    if [ -x /usr/bin/dircolors ]; then
        test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval    "$(dircolors -b)"
        alias ls='ls --color=auto'
        #alias dir='dir --color=auto'
        #alias vdir='vdir --color=auto'

        alias grep='grep --color=auto'
        alias fgrep='fgrep --color=auto'
        alias egrep='egrep --color=auto'
    fi

    # some more ls aliases
    alias ll='ls -alF'
    alias la='ls -A'
    alias l='ls -CF'

    # Alias definitions.
    # You may want to put all your additions into a separate file like
    # ~/.bash_aliases, instead of adding them here directly.
    # See /usr/share/doc/bash-doc/examples in the bash-doc package.

    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi

    # enable programmable completion features (you don't need to enable
    # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
    # sources /etc/bash.bashrc).
    if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
        . /etc/bash_completion
    fi

我无法使用HISTSIZE=9999 HISTFILESIZE=999设置为的bash 4.1或4.2来重现此内容.bashrc,而6000行则.bash_history全部显示在的输出中history。告诉我们您的bash版本,从何处获得以及bash的全部内容.bashrc
吉尔(Gilles)“所以,别再邪恶了”,

感谢您的答复,但是您的.bash_history如何为6000行,同时HISTFILESIZE = 999呢?我使用的是GNU bassh版本4.1
Marwan Tanager

shopt -s histappend HISTSIZE = 50000 HISTFILESIZE = 500000
Marwan Tanager

抱歉,这是一个错字:我有HISTFILESIZE=9999。该.bash_history被人为构建的测试(我不想在提示符下键入命令6000),但bash所做保存好退出。请完整复制并粘贴.bashrc到问题中。
吉尔(Gilles)“所以,别再邪恶了”,

如果执行history | wc -l,则显示多少行?
Tim Post

Answers:


16

这是加载历史记录的实际代码(从bashhist.c260行开始):

/* Load the history list from the history file. */
void

load_history ()
{
  char *hf;

  /* Truncate history file for interactive shells which desire it.
     Note that the history file is automatically truncated to the
     size of HISTSIZE if the user does not explicitly set the size
     differently. */
  set_if_not ("HISTSIZE", "500");
  sv_histsize ("HISTSIZE");

  set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
  sv_histsize ("HISTFILESIZE");

  /* Read the history in HISTFILE into the history list. */
  hf = get_string_value ("HISTFILE");

  if (hf && *hf && file_exists (hf))
    {
      read_history (hf);
      using_history ();
      history_lines_in_file = where_history ();
    }
}

如果设置了HISTSIZE和的值HISTFILESIZE,则将使用它们。

Readline是实际处理输入/行编辑和历史记录的库,确实提供了限制历史记录缓冲区可以增长多少的功能。但是,Bash对此并没有设定严格的上限,在该上限中,任何更大的值都将被忽略,至少我可以找到。

编辑

评论来看,readline确实是罪魁祸首。我在看(相当愚蠢)功能参数:

有一个名为history-size的变量,可以从inputrc文件中读取。该变量设置历史列表中保存的最大历史条目数。我在本地inputrc文件中检查了它的值,发现它等于5000。将其设置为更大的值可以解决此问题。


如果它没有设置上限,那么为什么在重新启动Shell之后将HISTSIZE设置为大于5000的任何值对历史记录列表的大小没有影响?如果历史文件的大小大于5000行,请尝试将.bashrc中的HISTSIZE设置为大于5000的值,然后重新启动Shell并执行history | wc -l。您将看到历史记录列表小于或等于5000,而不管HISTSIZE。但是,将HISTSIZE设置为小于5000的任何值将使用相同的实验产生可见的效果。
Marwan Tanager

3
阅读GNU readline库文档,事实证明您是对的。有一个名为history-size的变量,可以从inputrc文件中读取。该变量设置历史列表中保存的最大历史条目数。我在本地inputrc文件中检查了它的值,发现它等于5000。将其设置为更大的值可以解决此问题。感谢您的见解:-)
Marwan Tanager

@Marwan Awesome :)我以为这history-size是从RL changelog传递到readline中的函数的东西,最终被bash调用。好像我们一起想通了。
Tim Post


5

同时尝试HISTFILESIZEHISTSIZE


我将HISTFILESIZE设置为50000。问题在于HISTSIZE,它确定启动bash时要加载到内存中的.bash_history中的最后“ HISTSIZE”行,并且可以在其中使用ctrl-r机制。
Marwan Tanager

登录后,如果键入 echo "$HISTSIZE $HISTFILESIZE" ,您会看到什么?
ztank1013 2011年

它产生:“ 50000 500000”
Marwan Tanager

3

我有相同(或相似)的问题,但inputrc很好。就我而言,唯一可行的方法就是注释掉HISTSIZE=1000并保留HISTFILESIZE=2000我的库存~/.bashrc,即使稍后我在同一文件中覆盖这些变量!


2

更改这些行~/.bashrc会为我修复:

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=5000  

HISTFILESIZE=2000

之后,保存文件并重新加载bashrc文件

$ . ~/.bashrc

1

我认为您可能会在HISTSIZE的操作系统上达到历史最高记录。从Solaris 10(运行KSH)中的fc / history的手册页中:

[片段]

/ usr / bin / fc

fc实用程序列出或编辑并重新执行先前输入到交互式sh中的命令。

命令历史记录列表按编号引用命令。列表中的第一个数字是任意选择的。除非用户登录并且没有其他进程正在访问列表,否则数字与其命令的关系不会改变,这时系统可能会重置编号以从另一个数字(通常为1)开始保留最旧的命令。当数字达到HISTSIZE或32767(以较大者为准)中的值时,shell可能会包装数字,并以较小的数字(通常为1)开始下一个命令。但是,尽管有此可选的数字换行,fc仍将保持命令的时间顺序。例如,如果依次给四个命令指定了数字32 766、32 767、1(包装),

[片段]

这意味着fc命令最多可以处理历史文件中的32767个条目,这使其成为历史文件中保留的命令数量的硬上限。当然是YMMV,但是我认为您可以在此问题上查阅OS文档/手册页。我的0.02 ...

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.