[SOLVED] Bash prompt - getting history command number in PROMPT_COMMAND
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash prompt - getting history command number in PROMPT_COMMAND
In PS1 I'm able to get the history command number using one of bash's internal variables: \!
In the attached screenshot you can see this number is updated when \! is used in PS1 (next to user name).
I prefer to have this number on the line above the prompt, which is produced by PROMPT_COMMAND. How is that achieved? I can't use \! there, and if I use $HISTCMD instead, the job number remains stuck at 1, as you can see in the screenshot.
Below are the prompt_align and .bashrc scripts.
prompt_align
Code:
print_pre_prompt ()
{
PS1L=" ${HOSTNAME}"
PS1R=$PWD
if [[ $PS1R/ = "$HOME"/* ]]
then
PS1R=\~${PS1R#$HOME}
fi
printf "\n%s%$(($COLUMNS-${#PS1L}))s\n%s%$(($COLUMNS-${#HISTCMD}-2))s\n" "$PS1L" "${SHELL##*/} | $TERM | $(tty) " "[$HISTCMD]" "[$PS1R]"
}
PROMPT_COMMAND=print_pre_prompt
Perfect! Thank you. In your earlier post you had a %s before the second COLUMNS arithmetic expansion. Removing that in your second post solved the problem. See attached.
Why does \r work when \n doesn't?
Last edited by Gerard Lally; 04-16-2017 at 08:46 AM.
In your earlier post you had a %s before the second COLUMNS arithmetic expansion.
Sorry, I typed that in instead of c/p'ing.
Quote:
Why does \r work when \n doesn't?
You're probably to young to remember manual typewriters and teletypes but that is where I learned the term carriage return.
A carriage return ("\r"), in this case, moves the cursor to the first column on the same line. So HISTCMD "[\!]" is printed on the same line as the PWD and then on a new line "[\u]\$ " is printed. A line feed ("\n") would move the cursor to the first column on the next line. https://en.wikipedia.org/wiki/Carriage_return
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.