Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The last few days I've noticed that whenever I shutdown my system (Debian Testing) and fire up a bash session, then press the up arrow key, I get the commands from the first time I cleared the history cache using history -c. I'm not sure how this happened, but even if I clear my history again, then press the up arrow, nice it works, but if I start another shell it doesn't (even less whenever I restart or shutdown or log off).

I've tried everything (even deleting the .bash_history by hand), so I ran out of ideas.

$ echo $SHELL
/bin/bash
$ /bin/bash --version
GNU bash, version 4.2.45(1)-release (i486-pc-linux-gnu)
$ echo $HISTFILE
/home/braiam/.bash_history

When looking for information to add the question, I ran ls -l .bash_history (not to look like a dumb) and found the problem:

$ ls -l .bash_history 
-r--------. 1 braiam braiam 59372 Jul 26 20:18 .bash_history

But running chmod +w .bash_history didn't save the day. Now I wonder how it could end like that...

After deleting the .bash_history file now I have no-history. I checked the .bashrc for clues:

cat .bashrc | grep -i hist
# don't put duplicate lines or lines starting with space in the history.
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

Following ash's answer, in a non-login (gnome-terminal) shell:

braiam@bt:~$ echo abacaba; echo $$
abacaba
15372
braiam@bt:~$ 

Then fired up another non-login shell:

braiam@bt:~$ strace -o e -s 256 -p 15372
Process 15372 attached - interrupt to quit

Went back to the first shell, then type history -a:

braiam@bt:~$ echo abacaba; echo $$
abacaba
15372
braiam@bt:~$ history -a
braiam@bt:~$ 

In the shell with strace, nothing:

braiam@bt:~$ strace -o e -s 256 -p 15372
Process 15372 attached - interrupt to quit

Here I was almost without hope, so I decided to look at strace manual and found that -o is to dump the result to a file, so cat e resturn this:

cat e
read(0, "\33", 1)                       = 1
read(0, "[", 1)                         = 1
read(0, "A", 1)                         = 1
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
write(2, "history -w", 10)              = 10
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
read(0, "\33", 1)                       = 1
read(0, "[", 1)                         = 1
read(0, "A", 1)                         = 1
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
write(2, "\10a", 2)                     = 2
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
read(0, "\r", 1)                        = 1
write(2, "\n", 1)                       = 1
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigaction(SIGINT, {0x80a0e30, [], 0}, {0x80e9d00, [], 0}, 8) = 0
rt_sigaction(SIGTERM, {SIG_IGN, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_IGN, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGALRM, {0x80a1080, [HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], 0}, {0x80e9d00, [], 0}, 8) = 0
rt_sigaction(SIGTSTP, {SIG_IGN, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGTTOU, {SIG_IGN, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGTTIN, {SIG_IGN, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGWINCH, {0x80a09e0, [], 0}, {0x80e9550, [], SA_RESTART}, 8) = 0
rt_sigaction(SIGINT, {0x80a0e30, [], 0}, {0x80a0e30, [], 0}, 8) = 0
time(NULL)                              = 1378047084
stat64("/home/braiam/.bash_history", {st_mode=S_IFREG|0600, st_size=44, ...}) = 0
open("/home/braiam/.bash_history", O_WRONLY|O_APPEND|O_LARGEFILE) = 3
write(3, "history -w\nhistory -a\n", 22) = 22
close(3)                                = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
rt_sigaction(SIGINT, {0x80a0e30, [], 0}, {0x80a0e30, [], 0}, 8) = 0
time(NULL)                              = 1378047084
rt_sigprocmask(SIG_BLOCK, [CHLD TSTP TTIN TTOU], [], 8) = 0
ioctl(255, TIOCSPGRP, [15372])          = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigaction(SIGINT, {0x80a0e30, [], 0}, {0x80a0e30, [], 0}, 8) = 0
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
ioctl(0, TIOCGWINSZ, {ws_row=24, ws_col=80, ws_xpixel=0, ws_ypixel=0}) = 0
ioctl(0, TIOCSWINSZ, {ws_row=24, ws_col=80, ws_xpixel=0, ws_ypixel=0}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig -icanon -echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, [INT QUIT ALRM TERM TSTP TTIN TTOU], [], 8) = 0
rt_sigaction(SIGINT, {0x80e9d00, [], 0}, {0x80a0e30, [], 0}, 8) = 0
rt_sigaction(SIGTERM, {0x80e9d00, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGTERM, {SIG_IGN, [], 0}, {0x80e9d00, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {0x80e9d00, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_IGN, [], 0}, {0x80e9d00, [], 0}, 8) = 0
rt_sigaction(SIGALRM, {0x80e9d00, [], 0}, {0x80a1080, [HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], 0}, 8) = 0
rt_sigaction(SIGTSTP, {0x80e9d00, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGTSTP, {SIG_IGN, [], 0}, {0x80e9d00, [], 0}, 8) = 0
rt_sigaction(SIGTTOU, {0x80e9d00, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGTTOU, {SIG_IGN, [], 0}, {0x80e9d00, [], 0}, 8) = 0
rt_sigaction(SIGTTIN, {0x80e9d00, [], 0}, {SIG_IGN, [], 0}, 8) = 0
rt_sigaction(SIGTTIN, {SIG_IGN, [], 0}, {0x80e9d00, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigaction(SIGWINCH, {0x80e9550, [], SA_RESTART}, {0x80a09e0, [], 0}, 8) = 0
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
write(2, "\33]0;braiam@bt: ~\7\33[01;31mbraiam@bt\33[00m:\33[01;34m~\33[00m$ ", 56) = 56
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
read(0,  <unfinished ...>

So, according to this, there is not problem. Then, I open another non-login shell, and no joy. The history isn't loaded. A cat .bash_history reveals that the file was indeed filled but the non-login shell doesn't take it.

Now, I tried both strace -o /tmp/e -s 512 bash here (too long for character limit) and strace -o /tmp/e -s 512 gnome-terminal here (both from pastebin.com, sorry). Here with the original settings (minus the -w permission on .bash_history).

I tried login next, Ctrl + Alt + F1, logged in, pressed the up arrow, crossed fingers and it worked! But, that still leave me with the interrogant, why doesn't the non-login shell loads the history?

share|improve this question
1  
Try setting the environment variable HISTFILESIZE=0 in your .bash_profile. –  Drav Sloan Aug 31 '13 at 16:16
    
The behavior of those variables are described in the bash man page if you want to more details :) –  Drav Sloan Aug 31 '13 at 16:23
    
Are the shell sessions being closed normally - either through the exit or logout commands, pressing ctrl-d, or the sighup signal? If not, the shell won't write the updated history. –  ash Aug 31 '13 at 19:04
    
@ash of course. I did logout properly, or closed the gnome terminal. –  Braiam Aug 31 '13 at 19:12
    
You may be interested in this discussion. –  terdon Sep 1 '13 at 14:20
show 4 more comments

4 Answers

One reason this could happen is that the shells are not exitting normally, or you're looking for updates when the shells are still running.

To test this scenario, try history -a after some unique command, like echo abacaba on a running shell and then start a new shell. If the new shell gets the updates from the first, then writing and reading the history file is working. However, if that doesn't work, something else is wrong, and I would personally use strace in an attempt to diagnose.

Another thing to check is that the HOME variable has not changed and is set to the same path as set in the passwd file for the user.

Using strace to diagnose

  • in a shell, echo abacaba; echo $$
  • in another shell, strace -o e -s 256 -p $PID, where $PID = $$ output from the first shell
  • in the first shell, history -a
  • stop the strace and look through it for the processing of history -a; there should be open() and write() calls.

Non-Interactive Shells

It's odd that the non-interactive shells are not getting the shell history, but this could be explained by a number of things. More diagnostics will help.

Strace can again be used to help on that end, if the startup of the non-interactive shells can be modified to insert strace. For example, the following shows a shell startup command modified to use strace:

bash /home/jack/bin/jacks_script
strace -f -o /tmp/e -s 512 bash /home/jack/bin/jacks_script

Of course, if more than one of these runs at a time, /tmp/e will get overwritten.

Using grep open /tmp/e, the full path to the bash_history file should show up if the shell is loaded it. So, this command allows two checks: (1) that a history file is being opened and read, and (2) that the path to the history file is as-expected.

Without strace

Verify the shell has the history option set (set -o history for bash).

One thing that is unclear is just when HISTFILE is checked by the shell. For example, if it's set in the .bash_profile, I'm not sure whether it will take affect for the reading of shell history because it's after the shell starts, but before the first command-prompt is given to the user.

share|improve this answer
    
Ran the commands as you suggested, see my question for update. –  Braiam Sep 1 '13 at 14:45
    
Got it. That helped. It would also help to see how the shells are being started. I'm curious why you refer to some as non-interactive shells; that might lead to an answer. –  ash Sep 1 '13 at 23:38
    
Sorry, I'm messing with myself, instead non-interactive is non-login (gnome-terminal) which remembers/write to the history files. –  Braiam Sep 2 '13 at 0:29
    
gnome-terminal; I've never used it; I'm an xterm user myself. I'll check it out. –  ash Sep 2 '13 at 0:34
    
It worked; not really surprised. Can you run a gnome-terminal by hand, by just running it from a shell (even out of another gnome-terminal)? If so, and the problem persists that way, strace -o /tmp/e -s 512 gnome-terminal will help. –  ash Sep 2 '13 at 0:36
show 9 more comments

You can just unset $HISTFILE (which contains the path to the history file), which won't affect your ability to use history in the current session:

unset HISTFILE
share|improve this answer
    
I want the history, just that it is persistent and the new commands doesn't get recorded. –  Braiam Aug 31 '13 at 17:17
add comment

What I ended doing was setting HISTFILESIZE to 0 in the .bashrc file, as Drav Sloan said in comments.

[...]
HISTFILESIZE=0
[...]
share|improve this answer
    
Sadly, that doesn't fixed the issue. –  Braiam Sep 1 '13 at 15:22
add comment

you tried history -c && history -w ?

history -c clear the cache of history function and history -w write the commands on the cache of history function (nothing) in .bash_history.

Another option is clear the bash history /dev/null > ~/.bash_history and link ~/.bash_history to /dev/null: ln -sf /dev/null ~/.bash_history

if you don't want to save the commands of the current shell session, you must run history -r to reset the history cache to its initial state (before starting session). Then you log out and the commands on history cache (the same as before starting shell session) will be written in .bash_history

share|improve this answer
1  
Umm... I like my history, what I don't like is that it doesn't remembers any of the recent commands that I fire up. –  Braiam Aug 31 '13 at 17:16
    
I don't quite get that. Do you mean the same terminal window or a different one? –  Michael Durrant Aug 31 '13 at 18:05
    
@MichaelDurrant every terminal, none of them remembers what my last commands were. –  Braiam Sep 1 '13 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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