启用 shell 的历史记录

启用 shell 的历史记录

当我启动 bash 或任何其他 shell 时,它没有历史记录。你知道我能做什么吗?我尝试使用向上箭头,但如果我使用 OpenBSD 或 Ubuntu xenial 启动新 shell,则它不会产生任何效果。

我尝试了提到的所有配置,我$HISTFILE在我的 中设置.profile,我注销并再次登录,并且我使用两个不同的操作系统。

这正是我的配置以及我如何登录并且默认情况下没有历史记录:

developer@1604:~$ ssh 127.0.1.2 -l root -p 2223
[email protected]'s password: 
Last login: Sat Aug 19 01:34:14 2017
OpenBSD 6.1 (GENERIC) #19: Sat Apr  1 13:42:46 MDT 2017

Welcome to OpenBSD: The proactively secure Unix-like operating system.

Please use the sendbug(1) utility to report bugs in the system.
Before reporting a bug, please try to reproduce it with the latest
version of the code.  With bug reports, please try to ensure that
enough information to reproduce the problem is enclosed, and if a
known fix for it exists, include that as well.

# history                                                               
ksh: fc: no history (yet)
# more .profile                                                                
# $OpenBSD: dot.profile,v 1.9 2010/12/13 12:54:31 millert Exp $
#
# sh/ksh initialization

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin
export PATH
: ${HOME='/root'}
export HOME
umask 022

case "$-" in
*i*)    # interactive shell
        if [ -x /usr/bin/tset ]; then
                if [ X"$XTERM_VERSION" = X"" ]; then
                        eval `/usr/bin/tset -sQ '-munknown:?vt220' $TERM`
                else
                        eval `/usr/bin/tset -IsQ '-munknown:?vt220' $TERM`
                fi
        fi
        ;;
esac
# 

答案1

~./profile不是启用保存当前历史记录(HISTFILE其中没有任何设置)。

ksh对于基础系统中的OpenBSD shell:

编辑您的~/.profile文件并添加以下行:

export ENV="$HOME/.kshrc"

然后编辑~/.kshrc并添加以下行:

set -o emacs
HISTFILE="$HOME/.ksh_history"

这应该足够了。这set -o emacs是为了让箭头键按照您的预期工作(我相信您对此有问题)。

如果您希望避免为交互式 shell 使用单独的文件,HISTFILE="$HOME/.ksh_history"直接设置也足够了。.profile

您还可以指定要保存的历史记录条目的数量,例如,

HISTSIZE=5000

默认值为HISTSIZE500。

这或多或少是我写的我之前的回答也。


当前(2017 年 8 月下旬)有一系列 CVS 致力于 OpenBSDksh实现,以实现各种与历史相关的功能,例如HISTCONTROLignoredupsignorespace(如 中提供的bash)。

答案2

将这些行附加到您的~/.bashrc

set -o history
HISTFILE=$HOME/.bash_history
HISTFILESIZE=500
HISTSIZE=500
shopt -s histappend

然后输入命令source .bashrc,然后随机输入几个命令。查看现在是否有历史记录(history在命令行输入,或使用向上箭头)。

相关内容