6

I was taking a look at my .bash_history, and I found a blank line. I thought that I could have pressed "enter" while I was taking a look in the file.

Is that normal? Or maybe I really pressed enter while looking the file on gedit?

EDIT: Just found today that I have more than one line break. That's strange, I never edit the .bash_history file.

terdon
  • 104,119
Nori-chan
  • 865

3 Answers3

4

This is not a problem, it is not dangerous and is entirely normal. You can get such lines if you hit space a few times and then hit enter. This will be saved in your history (since it is a non-blank line, spaces are characters just invisible ones).

To test this, we can use a command that prints the blank lines in bash's history For example, this grep will match all lines that start with numbers and then have 0 or more whitespace (spaces or tabs or whatever) and nothing else until the end of the line:

history | grep -P '^\s*\d+\s*$'

That should show you a list of empty commands you have run. Now, run ( the first line means hit space a few times, then enter)

$ echo foo
foo
$       
$ history  | tail -n 3
$ history | tail -n 3
80  echo foo
81               ## this is the blank line
82  history | tail -n 3

You will notice that you have a new blank line. In conclusion, don't worry. everything is fine and this is normal.

terdon
  • 104,119
1

As mentioned by terdon, those are likely whitespace only lines.

There are two environment variables options that control whether whitespace lines are stored in the history or not:

  • HISTCONTROL: if it contains ignorespace, then any line that starts with a whitespace is ignored.

  • HISTIGNORE: colon separated list of pattens to ignore. For example, if it contains:

    HISTIGNORE=' *'
    

    then lines that start with a single space will be ignored.

I was not able to make bash store truly empty lines without whitespace.

0

Here is an explanation about how blank lines can occur in the .bash_history file.

In brief:

If you call from the history a command line that you have actually typed earlier in that same current terminal session and then edit it (maybe deleting it) but then interrupt your edit by leaving that (history-)command line using for instance the Down arrow to come back to the prompt of the actual current command line, then when later on you exit the terminal the edits get recorded in the .bash_history file.

The Quark
  • 283