Search code examples
formatzsh

The reason of starting a line with colon ":" in zsh_history file


I'm using the zsh history plugin, which adds the timestamp of the command I entered. The raw file of the .zsh_history text file looks like below:

: 1582469132:0;jupyterlab
: 1582469132:0;jupyter notebook
: 1582469132:0;jupyterlab
: 1582469132:0;jupyter lab
: 1582469132:0;jupyter notebook
: 1582469132:0;ls

I just don't understand the reason for using this format that why every line item starts with a colon :?

I'm pretty sure this plugin is not the only one using this format. I've seen SWIFT messages using this format and some other file which I couldn't remember the name.


Solution

  • I'm not positive, but I believe the leading colon allows you to execute the history file as a script.

    : is a do-nothing command: it ignores its arguments then completes with an exit status of 0. In this case, the argument is the string <timestamp>:0; the semicolon is a command terminator.

    You can try it out at the prompt:

    % : 1235:0;echo hello
    hello
    

    Thus, executing this file as a script would have the same affect as executing

    jupyterlab
    jupyter notebook
    jupyterlab
    jupyter lab
    jupyter notebook
    ls
    

    It's unlikely you would want to execute an arbitrary history file as a script, but a file containing a specially crafted history could be useful.