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

前端 未结 7 1728
悲&欢浪女
悲&欢浪女 2021-02-01 19:08

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

Using the , the

相关标签:
7条回答
  • 2021-02-01 19:32

    I put the next lines into my .bashrc:

    case "$TERM" in
       screen)
           declare SCREEN_NAME=$(echo $STY | sed -nr 's/[^.]*\.(.*)/\1/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:

    if [[ $TERM == screen ]]; then
       declare SCREEN_NAME=$(echo $STY | sed -nr 's/[^.]*\.(.*)/\1/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.

    Note: this not work in chroot!

    0 讨论(0)
  • 2021-02-01 19:36

    @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.

    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.

    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.

    0 讨论(0)
  • 2021-02-01 19:38

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

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

    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 ~/.screenrc file:

    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

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

    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 HISTFILE environmental variable to something that includes the current screen window's number ($WINDOW).

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

    Write Commands to $HISTFILE on Execution

    As is normal bash behavior, the history is only written to the $HISTFILE file 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 ~/.bashrc file:

    export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"
    
    0 讨论(0)
  • 2021-02-01 19:38

    history will show all the history command.

    0 讨论(0)
  • 2021-02-01 19:44

    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

    0 讨论(0)
  • 2021-02-01 19:52

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

    cat $HISTFILE
    #or tac, less, $EDITOR, ... depending on how you want to "access" it
    
    0 讨论(0)
提交回复
热议问题