formatting - Displaying what `history` line is current in bash prompt

05
2013-12
  • warren

    What formatting character needs to be added to a bash prompt to indicate the most recent history item run (or the current command number if 1 could be added to the last history entry)?

    My prompt string is this:

    \[\033[33m\][\u@\[\033[1;31m\]\h]\]\033[0m {\W}\n\033[1;34m\]\w\]\033[0m >
    

    Gives me the following display:

    [user@host] {~}
    ~ >
    

    User is in yellow, and the host is in red. The entry in brackets is the current directory, and the entry before the greater-than sign is the full pwd.

    Can I append to the first line the current command number so I would have something like the following:

    [user@host] {~} (nnn)
    ~ >
    

    where (nnn) is the current (or just processed) command number, as shown when running history?

  • Answers
  • Doug Harris

    From the PROMPTING section of the bash man page:

    \!     the history number of this command
    \#     the command number of this command
    

    and further down:

    The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file ..., while the command number is the position in the sequence of commands executed during the current shell session.

    If you want the last command number, you could wrap the number in an arithmetic evaluation like this:

    export PS1='last cmd num: $((\# -1)), this num: \# '
    

  • Related Question

    Bash history loss
  • Jefromi

    I like to keep a lot of history, so I have histappend set in my .bashrc. Most of the time everything works fine, with history built up from many shells appending. However, every once and a while, I'll start a new shell and find that I've lost the entire history - and it often only contains some of the commands from the last shell to exit (i.e. it's not just overwriting instead of appending). Because of this, I'm suspicious it's happening at shell exit, rather than from some other process killing the .bash_history file. Supporting this conclusion, I have history command numbers in my prompt, and I've never seen them jump down.

    Anyone ever run into a similar problem? Or even just have suggestions how to track down the problem?


  • Related Answers
  • innaM

    No idea why this happens, but maybe you can circumvent the problem by forcing bash to write to its history file each time it displays a prompt:

    PROMPT_COMMAND="history -a; history -n"
    

    This will write (-a) and then re-read (-n) the history file each time bash prompts for the next command. Additional benefit: you'll get command X in shell 1 in the history of shell 2.

  • Jefromi

    Sorry to answer my own question, but none of the other answers really address the problem.

    I've finally figured out that this only happens when closing gnome-terminal itself (i.e. file > exit, the 'x' button, alt+F4), and even then generally only when closing several terminals in quick succession. It never happens when using ctrl-D to close the shell, letting the terminal follow.

    If I can pin it down well enough, I'll file a gnome-terminal bug report. In the meantime, perhaps this will help some other people who get here from google!

  • Axxmasterr

    I have seen this happen before but it was a problem with disk errors that were happening in increasing frequency. I would run a scan on the drive. If it turns out the drive is fine, I would check to see if this file is not surpassing an arbitrary shell history limit.

    Something that might be able to keep that from happening would be to keep pruning the file back to 80 lines or however many commands you want the history to be.

  • David Mackintosh

    My experience was that shells updated the history file at exit time. So a shell's initial "history" depended on the most recently exited shell's view of the history.

    The result of this is that you can get commands coming and going from the history, depending on how other shells started and stopped.