I am sure that I have seen someone have a part of their prompt aligned to the right in their terminal window and then have the actual cursor start on a second line. I know that I can achieve the second line with a "\n" in the PS1, but I cannot figure out how to align part of it to the right. Was what I saw just whitespace added between the two strings?

6 Answers
6

What you want can fairly easily be done by displaying the first line before displaying the prompt. For example, the following displays a prompt of \w on the left of the first line and a prompt of \u@\h on the right of the first line. It makes use of the $COLUMNS variable which contains the width of the terminal and the $PROMPT_COMMAND parameter which is evaluated before bash displays the prompt.

Note that things get significantly more complicated if you want a colored left prompt, since the non-printing characters mean that string length is not the same as the number of characters displayed.
–
Mu MindSep 1 '12 at 6:49

Based on the information I found here I was able to discover a simpler solution to right align while accommodating variable length content on the right or left including support for colour. Added here for your convenience...

Note on colours: using the \033 escape in favour of alternatives, without \[\] groupings, proves most compatible and therefor recommended.

The trick is to write the right hand side first, then use carriage return (\r) to return to start of line and continue to overwrite the left hand side content on top of that, as follows:

I am using tput cols on Mac OS X to retrieve the terminal/console width from terminfo since my $COLUMNS var is not populated in env but you may substitute the replaceable "*" value in %*s, by providing "${COLUMNS}", or any other value you prefer, instead.

The next example uses $RANDOM to generate different length content includes colours and shows how you might extract functions to refactor the implementation to reusable functions.

Since printf assumes the length of string to be the # of characters we need to compensate for the amount of characters required to render the colours, you will find it always short of the end of screen because of the non printed ANSI characters without compensation. The characters required for colour remains constant and you will find that also printf takes into account the change in length, as returned by $RANDOM for example', which keeps our right alignment in tact.

This is not the case with special bash prompt escape sequences (ie. \u, \w, \h, \t) though, as these will only record a length of 2 because bash will only translate them when the prompt is displayed, after printf has rendered the string. This does not affect the left hand side but best to avoid them on the right.

Of no consequence if the generated content will remain at constant length though. Like with the time \t option which will always render the same amount of characters (8) for 24 time. We only need to factor in the compensation required to accommodate for the difference between 2 characters counted which results to 8 characters when printed, in these cases.

Keep in mind that you may need to triple escape \\\ some escape sequences which otherwise hold meaning to strings. As with the following example the current working directory escape \w holds no meaning otherwise so it works as expected but the time \t, which means a tab character, does not work as expected without triple escaping it first.

Adding on Giles' answer, I wrote something to handle colors better (provided they're properly enclosed in \[ and \]. It's case-by-case and doesn't handle every case, but it lets me set my PS1L in the same syntax as PS1 and uses the (uncolored) date as PS1R.