LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 04-15-2017, 06:39 PM   #1
Gerard Lally
Senior Member
 
Registered: Sep 2009
Location: Leinster, IE
Distribution: Slackware, NetBSD
Posts: 2,177

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
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
.bashrc
Code:
source ~/prompt_align
PROMPT_COMMAND=print_pre_prompt
export PS1='[\!][\u]\$ '
Attached Thumbnails
Click image for larger version

Name:	bash-prompt.png
Views:	70
Size:	48.9 KB
ID:	24787  

Last edited by Gerard Lally; 04-16-2017 at 03:31 PM.
 
Old 04-15-2017, 10:17 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
I'm a little confused by your use of the term "job number". From the bash manual page:
Quote:
HISTCMD
The history number, or index in the history list, of the current command.
...
If that is what you want try this:

In prompt_align change the last new line to a carriage return and don't print HISTCMD:
Code:
printf "\n%s%$(($COLUMNS-${#PS1L}))s\n%s%$(($COLUMNS-${#HISTCMD}-2))s\r" "$PS1L" "${SHELL##*/} | $TERM | $(tty) " "[$PS1R]"
Then add a line feed to your prompt:
Code:
export PS1='[\!]\n[\u]\$ '
I tried the above and it seemed to work.

HTH
 
1 members found this post helpful.
Old 04-16-2017, 06:59 AM   #3
Gerard Lally
Senior Member
 
Registered: Sep 2009
Location: Leinster, IE
Distribution: Slackware, NetBSD
Posts: 2,177

Original Poster
Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Quote:
Originally Posted by norobro View Post
I'm a little confused by your use of the term "job number". From the bash manual page:
If that is what you want try this:

In prompt_align change the last new line to a carriage return and don't print HISTCMD:
Code:
printf "\n%s%$(($COLUMNS-${#PS1L}))s\n%s%$(($COLUMNS-${#HISTCMD}-2))s\r" "$PS1L" "${SHELL##*/} | $TERM | $(tty) " "[$PS1R]"
Then add a line feed to your prompt:
Code:
export PS1='[\!]\n[\u]\$ '
I tried the above and it seemed to work.

HTH
That does indeed put the history command number above the user name but it also gets rid of the right-aligned path.
 
Old 04-16-2017, 08:24 AM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Unless I'm missing something it looks like the following produces what you want on my box.

prompt_align:
Code:
$ cat prompt_align 
print_pre_prompt () 
{ 
PS1L="${HOSTNAME}"
PS1R=$PWD
if [[ $PS1R/ = "$HOME"/* ]]
then
PS1R=\~${PS1R#$HOME}
fi
printf "\n%s%$(($COLUMNS-${#PS1L}))s\n%$(($COLUMNS))s\r" "$PS1L" "${SHELL##*/} | $TERM | $(tty) " "[$PS1R]"
#printf "\n%s%$(($COLUMNS-${#PS1L}))s\n" "$PS1L" "${SHELL##*/} | $TERM | $(tty) " 
#printf "%*s\r%s\n\%s\$ " "$(tput cols)" "$PWD" "$HISTCMD"
}
PROMPT_COMMAND=print_pre_prompt
In .bashrc
Code:
source ~/temp/prompt_align
PROMPT_COMMAND=print_pre_prompt
export PS1='[\!]\n[\u]\$ '
Attached Thumbnails
Click image for larger version

Name:	prompt.jpg
Views:	34
Size:	24.4 KB
ID:	24789  
 
1 members found this post helpful.
Old 04-16-2017, 08:45 AM   #5
Gerard Lally
Senior Member
 
Registered: Sep 2009
Location: Leinster, IE
Distribution: Slackware, NetBSD
Posts: 2,177

Original Poster
Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Quote:
Originally Posted by norobro View Post
Unless I'm missing something it looks like the following produces what you want on my box.

printf "\n%s%$(($COLUMNS-${#PS1L}))s\n%$(($COLUMNS))s\r" "$PS1L" "${SHELL##*/} | $TERM | $(tty) " "[$PS1R]"
}
PROMPT_COMMAND=print_pre_prompt[/code]
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?
Attached Thumbnails
Click image for larger version

Name:	bash-prompt.png
Views:	34
Size:	99.5 KB
ID:	24791  

Last edited by Gerard Lally; 04-16-2017 at 08:46 AM.
 
Old 04-16-2017, 09:07 AM   #6
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by Gerard Lally View Post
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
 
1 members found this post helpful.
Old 04-16-2017, 09:13 AM   #7
Gerard Lally
Senior Member
 
Registered: Sep 2009
Location: Leinster, IE
Distribution: Slackware, NetBSD
Posts: 2,177

Original Poster
Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Quote:
Originally Posted by norobro View Post
Sorry, I typed that in instead of c/p'ing.

You're probably to young to remember manual typewriters and teletypes but that is where I learned the term carriage return.
50 next month so not too young at all! Thanks for the explanation. It makes sense of course.

Last edited by Gerard Lally; 04-16-2017 at 09:14 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Make Bash prompt look like Windows C:\ prompt -- *PROBLEM* arkadios Linux - General 5 07-24-2012 11:29 AM
Ominous text [ PROMPT_COMMAND='pwd>&8;kill -STOP $$'] in my bash history file. zapo Linux - Security 2 01-12-2011 01:28 AM
shell prompt works, cron job fails btuley Linux - General 1 11-13-2008 11:40 AM
lpr job number -- where does lpr get the job number? conetic Linux - Software 1 02-28-2008 03:38 PM
Can you start a job without getting the job number spit back out? BrianK Linux - General 5 12-13-2007 08:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration