如何使用 Bash 在屏幕会话中获取命令历史记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14914570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:20:34  来源:igfitidea点击:

How do I get the command history in a screen session using Bash?

bashcommand-linegnu-screen

提问by hexacyanide

If I start a screen session with screen -dmS name, how would I access the command history of that screen session with a script?

如果我使用 启动屏幕会话screen -dmS name,我将如何使用脚本访问该屏幕会话的命令历史记录?

Using the , the last executed command appears, even in screen.

使用,最后执行的命令会出现,即使在屏幕中也是如此。

采纳答案by chepner

screendoesn't maintain a history of the commands you type. Your shell may or may not keep a history. Since you appear to use bash, you can use the historycommand.

screen不保留您键入的命令的历史记录。您的外壳可能会也可能不会保留历史记录。由于您似乎使用bash,您可以使用该history命令。

screendoes appear to have a crude approximation of a history search (it merely searches the scrollback buffer for a command line. See the screenman page under the "history" command (bound to C-a {by default).

screen似乎有一个历史搜索的粗略近似(它只是搜索命令行的回滚缓冲区。请参阅screen“历史”命令下的手册页(C-a {默认绑定)。

回答by Nathan S. Watson-Haigh

I use the default bash shell on my system and so might not work with other shells.

我在我的系统上使用默认的 bash shell,因此可能无法与其他 shell 一起使用。

this is what I have in my ~/.screenrcfile so that each new screen window gets its own command history:

这是我的~/.screenrc文件中的内容,以便每个新屏幕窗口都有自己的命令历史记录:

Default Screen Windows With Own Command History

具有自己的命令历史记录的默认屏幕窗口

To open a set of default screen windows, each with their own command history file, you could add the following to the ~/.screenrcfile:

要打开一组默认屏幕窗口,每个窗口都有自己的命令历史记录文件,您可以将以下内容添加到~/.screenrc文件中:

screen -t "window 0" 0 bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'
screen -t "window 1" 1 bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'
screen -t "window 2" bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'

Ensure New Windows Get Their Own Command History

确保新 Windows 获得自己的命令历史记录

The default screen settings mean that you create a new window using Ctrl+a cor Ctrl+a Ctrl+c. However, with just the above in your ~/.screenrcfile, these will use the default ~/.bash_historyfile. To fix this, we will overwrite the key bindings for creating new windows. Add this to your ~/.screenrcfile:

默认屏幕设置意味着您使用Ctrl+a c或创建一个新窗口Ctrl+a Ctrl+c。但是,在您的~/.screenrc文件中只有上述内容时,这些将使用默认~/.bash_history文件。为了解决这个问题,我们将覆盖创建新窗口的键绑定。将此添加到您的~/.screenrc文件中:

bind c screen bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'
bind ^C screen bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'

Now whenever you create a new screen window, it's actually launching a bash shell, setting the HISTFILEenvironmental variable to something that includes the current screen window's number ($WINDOW).

现在每当你创建一个新的屏幕窗口时,它实际上是在启动一个 bash shell,将HISTFILE环境变量设置为包含当前屏幕窗口编号 ( $WINDOW) 的内容。

Command history files will be shared between screen sessions with the same window numbers.

命令历史文件将在具有相同窗口编号的屏幕会话之间共享。

Write Commands to $HISTFILEon Execution

$HISTFILE在执行时写入命令

As is normal bash behavior, the history is only written to the $HISTFILEfile by upon exiting the shell/screen window. However, if you want commands to be written to the history files after the command is executed, and thus available immediately to other screen sessions with the same window number, you could add something like this to your ~/.bashrcfile:

与正常的 bash 行为一样,历史记录仅$HISTFILE在退出 shell/屏幕窗口时写入文件。但是,如果您希望在命令执行后将命令写入历史文件,从而立即可用于具有相同窗口编号的其他屏幕会话,您可以在您的~/.bashrc文件中添加如下内容:

export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"

回答by technosaurus

When you exita terminal (or shell) the shell writes its history to $HISTFILE, so to get its history in another terminal you can type exitin the terminal you want the history of and it will get written.

当您exit使用终端(或 shell)时,shell 会将其历史记录写入 $HISTFILE,因此要在另一个终端中获取其历史记录,您可以输入exit您想要其历史记录的终端,它将被写入。

cat $HISTFILE
#or tac, less, $EDITOR, ... depending on how you want to "access" it

回答by RaveTheTadpole

@technosaurus is right. $HISTFILE is written when bash exits, so you could exit one bash session, and start a new one, and the history should have been preserved through the file.

@technosaurus 是对的。$HISTFILE 是在 bash 退出时写入的,因此您可以退出一个 bash 会话,并开始一个新的会话,并且历史记录应该已通过文件保留。

But I think there is a better way to solve your problem. The bash manual includes a description of the history built-in command. It allows you to save this history with history -w [filename]and read the history with history -r [filename]. If you don't provide a filename, it will use $HISTFILE.

但我认为有更好的方法可以解决您的问题。bash 手册包括对history 内置命令的描述。它允许您使用 保存此历史记录history -w [filename]并使用 阅读历史记录history -r [filename]。如果您不提供文件名,它将使用 $HISTFILE。

So I would propose that you save the history inside your screen session to a specific file (or to your default $HISTFILE if you want). Then read the history file in the other bash session you want to access the history from. This way you don't have to exit the original bash session.

因此,我建议您将屏幕会话中的历史记录保存到特定文件(如果需要,也可以保存到默认的 $HISTFILE)。然后在要从中访问历史记录的另一个 bash 会话中读取历史记录文件。这样您就不必退出原始 bash 会话。

回答by mehrdad

use this: screen -L with capital L it will store a copy of terminal input and output to a file named screenlog.0 or if you use -S to name it, the log file gets the screen name

使用这个: screen -L 大写 L 它将把终端输入和输出的副本存储到一个名为 screenlog.0 的文件中,或者如果你使用 -S 来命名它,日志文件将获得屏幕名称

回答by Regisz

I put the next lines into my .bashrc:

我将下一行放入我的 .bashrc 中:

case "$TERM" in
   screen)
       declare SCREEN_NAME=$(echo $STY | sed -nr 's/[^.]*\.(.*)//p')
       if [[ $SCREEN_NAME ]]; then
           HISTFILE="${HISTFILE}.${SCREEN_NAME}.${WINDOW}"
           declare -p HISTFILE
       fi
       unset SCREEN_NAME
       ;;
   *)
       ;;
esac

My default .bashrc has this 'case' basically with 'xterm*|rxvt*)' value, so I only added my 'screen' part into it. If you have not this 'case', you can use the next instead of it:

我的默认 .bashrc 有这个 'case' 基本上带有 'xterm*|rxvt*)' 值,所以我只添加了我的 'screen' 部分。如果你没有这个“案例”,你可以使用下一个代替它:

if [[ $TERM == screen ]]; then
   declare SCREEN_NAME=$(echo $STY | sed -nr 's/[^.]*\.(.*)//p')
   if [[ $SCREEN_NAME ]]; then
       HISTFILE="${HISTFILE}.${SCREEN_NAME}.${WINDOW}"
       declare -p HISTFILE
   fi
   unset SCREEN_NAME
fi

And after I have an own bash_history for all window of my all screen.

在我对所有屏幕的所有窗口都有自己的 bash_history之后。

Note: this not work in chroot!

注意:这在 chroot 中不起作用!

回答by Yap Kai Lun Leon

historywill show all the history command.

history将显示所有历史命令。