5

I am running something in a bash window that I don't want to interrupt or even suspend momentarily. Is it possible to view command history of that particular window's session? I have multiple windows open, so viewing .bash_history won't help much.

4

No, bash doesn't support that. The history is kept in memory and not available for other processes until it is saved to .bash_history in the same session using history -a or history -w. But the moment it's written to the file system, the information from which session the command originated is lost.

The closest you can get is using some lines in .bashrc to let bash append every command directly after execution: https://unix.stackexchange.com/a/1292/147970
Then you can see the commands from all shells in near real-time in .bash_history.

To access the history for a specific session you need to interrupt the foreground process in that session using e.g. Ctrl+Z.

3

You can press Ctrl-Z to put task in background. After that you can work in your shell and see command history. To look tasks in background you can use job command. To back to your task run fg command.

2

Here is how with gdb (you'll need to run it with administrative permissions), via https://stackoverflow.com/questions/7272558/can-we-define-a-new-data-type-in-a-gdb-session :

preparation:

echo 'typedef void * histdata_t;
typedef struct _hist_entry {
  char *line;
  char *timestamp;
  histdata_t data;
} HIST_ENTRY;
typedef struct _hist_state {
  HIST_ENTRY **entries;
  int offset;
  int length;
  int size;
  int flags;
} HISTORY_STATE;
HIST_ENTRY _sampleentry;
HISTORY_STATE _samplestate;
' | tee sample.c
# get sample.o
gcc -g -c sample.c

# get bash pid, maybe via `pgrep bash`, or `pidof bash`, etc
# say in this example, it is 16573

Run test command:

$ sudo gdb -p 16573 -ex "set confirm off" -ex "add-symbol-file sample.o 0" -ex 'printf "ptype HIST_ENTRY\n"' -ex "ptype HIST_ENTRY" -ex 'printf "p *(HISTORY_STATE*)history_get_history_state()\n"' -ex 'p *(HISTORY_STATE*)history_get_history_state()' -ex 'set $myoffs = (*(HISTORY_STATE*)history_get_history_state())->offset' -ex 'printf "myoffs %d\n", $myoffs' -ex 'printf "p *(HIST_ENTRY *)history_get($myoffs)\n"' -ex 'p *(HIST_ENTRY *)history_get($myoffs)'
....
0x00007fb053abb0e9 in __pselect (nfds=1, readfds=0x7ffe81a009b0, writefds=0x0, exceptfds=0x0, 
    timeout=<optimized out>, sigmask=0x7ffe81a00930) at ../sysdeps/unix/sysv/linux/pselect.c:69
69  ../sysdeps/unix/sysv/linux/pselect.c: No such file or directory.
add symbol table from file "sample.o" at
    .text_addr = 0x0
Reading symbols from sample.o...done.
ptype HIST_ENTRY
type = struct _hist_entry {
    char *line;
    char *timestamp;
    histdata_t data;
}
p *(HISTORY_STATE*)history_get_history_state()
$1 = {entries = 0x55ed117f4ab0, offset = 155, length = 155, size = 502, flags = 1}
myoffs 155
p *(HIST_ENTRY *)history_get($myoffs)
$2 = {line = 0x55ed119684d0 "kill -STOP $$", timestamp = 0x55ed119709a0 "#1545016332", data = 0x0}

prepare gdb "capture last history entry" command:

echo '
set verbose off
set complaints 0
set trace-commands off
add-symbol-file sample.o 0
set $myoffs = ((HISTORY_STATE*)history_get_history_state())->offset
set $line = ((HIST_ENTRY *)history_get($myoffs))->line
printf "%s\n", $line
' | tee gdbscript

run gdb "capture last history entry" command:

sudo gdb -p 16573 -batch -x gdbscript 2>/dev/null | tail -1
kill -STOP $$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.