Is it possible to view another shell's history?
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.
command-history
add a comment |
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.
command-history
add a comment |
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.
command-history
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.
command-history
command-history
asked Jan 31 '17 at 18:06
Michael
22615
22615
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
.
add a comment |
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.
add a comment |
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_ENTRYn"' -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 %dn", $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 "%sn", $line
' | tee gdbscript
run gdb "capture last history entry" command:
sudo gdb -p 16573 -batch -x gdbscript 2>/dev/null | tail -1
kill -STOP $$
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f341534%2fis-it-possible-to-view-another-shells-history%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Jan 31 '17 at 18:37
cg909
2,7511221
2,7511221
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 31 '17 at 18:34
Roman Tkachuk
1414
1414
add a comment |
add a comment |
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_ENTRYn"' -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 %dn", $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 "%sn", $line
' | tee gdbscript
run gdb "capture last history entry" command:
sudo gdb -p 16573 -batch -x gdbscript 2>/dev/null | tail -1
kill -STOP $$
add a comment |
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_ENTRYn"' -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 %dn", $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 "%sn", $line
' | tee gdbscript
run gdb "capture last history entry" command:
sudo gdb -p 16573 -batch -x gdbscript 2>/dev/null | tail -1
kill -STOP $$
add a comment |
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_ENTRYn"' -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 %dn", $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 "%sn", $line
' | tee gdbscript
run gdb "capture last history entry" command:
sudo gdb -p 16573 -batch -x gdbscript 2>/dev/null | tail -1
kill -STOP $$
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_ENTRYn"' -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 %dn", $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 "%sn", $line
' | tee gdbscript
run gdb "capture last history entry" command:
sudo gdb -p 16573 -batch -x gdbscript 2>/dev/null | tail -1
kill -STOP $$
answered Dec 17 at 4:52
sdaau
2,63763148
2,63763148
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f341534%2fis-it-possible-to-view-another-shells-history%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown