0

I have a preferred bash prompt layout and have tried to apply it to 2 different systems at work. However, it does not perform the same in both environments.

As listed in the question, one is running 3.2.25 and the other 4.1.2. You may have guessed that it works correctly on the machine with the higher version of bash.

My problem is I cannot see what I have introduced that would not be understood by both versions.

Relevant lines from .bashrc follow:

# Colours
RED='\[\e[1;31m\]'
GREEN='\[\e[1;32m\]'
YELLOW='\[\e[1;33m\]'
BLUE='\[\e[1;34m\]'
CYAN='\[\e[1;36m\]'
NORM='\[\e[m\]'

# PS1 pieces
DATE='[\D{%d-%m-%Y} \t]'
LPATH='[\w]'
DB_USER='[genomst]'
DB_COL=BLUE
WHERE='[\u@\h]'

PS1="$YELLOW$DATE$GREEN$LPATH$NORM\n${!DB_COL}$DB_USER$NORM$WHERE\$ "

The unusual behaviour I am getting occurs when I try to search for a previous command I have used, pressing ctrl+r to search.

On the 4.1.2 version of course everything behaves, but on the 3.2.25 version the prompt that returns shows my cursor flashing in the middle:

Steps to repeat are:

1. login as user and receive the following prompt:
[28-09-2017 17:36:20][~]
[genomst][oracle@pwdchora51]$ 
2. Press ctrl+r and search for vim
[28-09-2017 17:36:20][~]
(reverse-i-search)`vim': vim .bashrc
3. Press left arrow key (could be any but this shows just how far I have moved back)
[28-09-2017 17:36:20][~]
[genomst][oracle@pwdchora51]$ vim .bashrc
                    ^

^ indicates where cursor is on my screen

Any light that someone could shed on this issue would be much appreciated 

Please let me know if any further detail is required??

grail
  • 914
  • 6
  • 14
  • Make it `PS1="$YELLOW$DATE$GREEN$LPATH$NORM\n${!DB_COL}$DB_USER$NORM$WHERE\$ $NORM"` (note the `$NORM` at the end to reset the escape sequences to normal.) I find it easier to just use the escape sequences themselves, e.g. `export PS1="\[\e[0;37m\]\D{%R}\[\e[1;34m\] \h:\w> \[\e[0m\]"` – David C. Rankin Oct 06 '17 at 07:39
  • @DavidC.Rankin - even with straight escape sequences the result is the same - PS1='\[\e[1;33m\][\D{%d-%m-%Y} \t]\[\e[1;32m\][\w]\n\[\e[1;34m\]nonex\[\e[m\][\u@\h]\$ ' --- See comment below for type of line that causes an issue – grail Oct 06 '17 at 08:20
  • Have you tried to remove all the color codes and check the behavior? Something like: `PS1="$DATE$LPATH\n${!DB_COL}$DB_USER$WHERE\$ "` – Fabien Bouleau Oct 06 '17 at 09:23
  • @FabienBouleau - I hadn't, but now have, and as expected it works just fine. I was pretty sure it was the colours as they are obviously non-printable characters which you don't want measured. Problem is I cannot find a solution that works in the older version of bash. As previously stated, it works flawlessly in bash v4+ – grail Oct 06 '17 at 11:23

1 Answers1

0

If your goal is to be generic, try using the tput command:

RED="\[$( tput -Txterm setaf 1 )\]"
GREEN="\[$( tput -Txterm setaf 2 )\]"
YELLOW="\[$( tput -Txterm setaf 3 )\]"
BLUE="\[$( tput -Txterm setaf 4 )\]"
MAGENTA="\[$( tput -Txterm setaf 5 )\]"
CYAN="\[$( tput -Txterm setaf 6 )\]"
WHITE="\[$( tput -Txterm setaf 7 )\]"
BOLD="\[$( tput -Txterm bold )\]"
NORM="\[$( tput -Txterm sgr0 )\]"
Fabien Bouleau
  • 464
  • 3
  • 11
  • Thought you were on to something, until I searched for a previous long command line and character being searched for is a decent distance down the line. As an example: rsync -av root@pwdchora16:/u01/oradata/archive/genprd/archive_1_732457891_25248[89]* /u01/oradata/archive/genomst/ --- From this command I searched for the left square bracket and on pressing the left arrow key to select the line my cursor was flashing on the 'e' at the end of the following string 'archive_1_732457891', which is not anywhere near what I searched for. Also, the end of the line is now 'd' in oradata at end – grail Oct 06 '17 at 08:17