bashの履歴リストのサイズを5000行以上に設定する方法はありますか?


25

HISTSIZE環境変数を5000より大きく設定しても、history組み込みの履歴リストを印刷すると、最後の5000コマンドのみが印刷されます。多くの場合、.bash_history5000行を超える大きな行があり、を押して初期コマンドに対処する必要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

これを、bash 4.1または4.2にHISTSIZE=9999 HISTFILESIZE=999設定し.bashrc、に設定し、6000行で.bash_historyすべてを出力することはできませんhistory。bashのバージョンと入手元、およびの全内容をお知らせください.bashrc
ジル 'SO-悪であるのをやめる'

返信いただきありがとうございます。しかし、.bash_historyは6000行で、その間HISTFILESIZE = 999ですか?私は、GNUのbasshバージョン4.1を使用
マルワン・フウキンチョウ科

shopt -s histappend HISTSIZE = 50000 HISTFILESIZE = 500000
Marwan

申し訳ありませんが、それはタイプミスでしたHISTFILESIZE=9999.bash_history人為的に(私はプロンプトで6000のコマンドを入力したくなかった)テストのために構築されますが、bashは終了時に適切に保存しました。.bashrc質問全体にコピーして貼り付けてください。
ジル「SO-悪であるのをやめる」

を実行するとhistory | wc -l、何行表示されますか?
ティムポスト

回答:


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実際に犯人でした。私は(むしろ愚かに)機能パラメータを見ていた:

inputrcファイルから読み取ることができるhistory-sizeという変数があります。その変数は、履歴リストに保存される履歴エントリの最大数を設定します。ローカルinputrcファイルの値を確認して、5000に等しいことを確認しました。より大きな値に設定すると、問題は解決しました。


上限を設定しない場合、シェルを再起動した後にHISTSIZEを5000より大きい値に設定しても履歴リストのサイズに影響しないのはなぜですか?5000行を超えるサイズの履歴ファイルがある場合は、.bashrcのHISTSIZEを5000より大きい値に設定してから、シェルを再起動してhistory | wc -l。HISTSIZEに関係なく、履歴リストは5000以下であることがわかります。ただし、HISTSIZEを5000未満の値に設定すると、同じ実験を使用して目に見える効果が得られます。
マーワンタナー

3
GNU readlineライブラリのドキュメントを読むと、あなたは正しいことがわかりました。inputrcファイルから読み取ることができるhistory-sizeという変数があります。その変数は、履歴リストに保存される履歴エントリの最大数を設定します。ローカルinputrcファイルの値を確認して、5000に等しいことを確認しました。より大きな値に設定すると、問題は解決しました。洞察に感謝:-)
マーワンタナー

@Marwan Awesome :)これは、history-size(RLの変更ログから)readlineの関数に渡されたもので、最終的にbashによって呼び出されたものだと思いました。一緒に考え出したようです。
ティムポスト

7

HISTSIZEが最初に設定されたときに履歴が切り捨てられるため、〜/ .bashrc または/ etcのシステム全体のbashrcで 5000以前に設定されている場合は、コメントアウトする必要があります。


5

との両方HISTFILESIZEを試してくださいHISTSIZE


HISTFILESIZEを50000に設定しました。問題は、bashを開始するときに.bash_historyの最後の 'HISTSIZE'行を決定し、ctrl-rメカニズムを使用できるHISTSIZEにあります。
マーワンタナー

ログインすると、 echo "$HISTSIZE $HISTFILESIZE" 何が表示されますか?
-ztank1013

これは、生産:「50000 500000」
マルワン・フウキンチョウ科

3

私は同じ(または同様の)問題を抱えていましたが、inputrcは大丈夫でした。私の場合は、働いていた唯一のものはコメントアウトしたHISTSIZE=1000HISTFILESIZE=2000私の在庫の~/.bashrc私はそれらが同じファイルの後のvarsはオーバーライドされていても- !


2

これらの行を変更すると、~/.bashrc修正されました:

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

HISTFILESIZE=2000

その後、ファイルを保存し、bashrcファイルをリロードします

$ . ~/.bashrc

1

HISTSIZEに関してOSの履歴の上限に達している可能性があると思います。Solaris 10(KSHを実行)のfc / historyのmanページから:

[中略]

/ usr / bin / fc

fcユーティリティは、対話型shに以前に入力されたコマンドを一覧表示または編集して再実行します。

コマンド履歴リストは、コマンドを番号で参照します。リストの最初の番号は任意に選択されます。ユーザーがログインし、他のプロセスがリストにアクセスしていない場合を除き、コマンドと番号の関係は変わりません。その場合、システムは番号をリセットして、最も古い保持コマンドを別の番号(通常は1) 。数値がHISTSIZEの値または32767(いずれか大きい方)に達すると、シェルは数値をラップして、より小さい数値(通常は1)で次のコマンドを開始します。ただし、このオプションの数値のラップにもかかわらず、fcはコマンドの時間順序シーケンスを維持します。たとえば、4つのコマンドに順番に32 766、32 767、1(ラップ)の番号が与えられている場合、

[中略]

これは、fcコマンドが履歴ファイル内の最大32767のエントリをアドレス指定できることを意味し、履歴ファイルに保持されるコマンド数の厳しい上限になります。もちろんYMMVですが、この問題についてはOSのドキュメント/マニュアルページを参照してください。私の0.02 ...

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.