Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 14 down vote accepted

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.

print_pre_prompt () 
    if [[ $PS1L/ = "$HOME"/* ]]; then PS1L=\~${PS1L#$HOME}; fi
    printf "%s%$(($COLUMNS-${#PS1L}))s" "$PS1L" "$PS1R"
share|improve this answer
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 Mind Sep 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:

prompt() {
    PS1=$(printf "%*s\r%s\n\$ " "$(tput cols)" 'right' 'left')

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.

function prompt_right() {
  echo -e "\033[0;36m$(echo ${RANDOM})\033[0m"

function prompt_left() {
  echo -e "\033[0;35m${RANDOM}\033[0m"

function prompt() {
    PS1=$(printf "%*s\r%s\n\$ " "$(($(tput cols)+${compensate}))" "$(prompt_right)" "$(prompt_left)")

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.

function prompt_right() {
  echo -e "\033[0;36m\\\t\033[0m"

function prompt_left() {
  echo -e "\033[0;35m\w\033[0m"

function prompt() {
    PS1=$(printf "%*s\r%s\n\$ " "$(($(tput cols)+${compensate}))" "$(prompt_right)" "$(prompt_left)")


share|improve this answer

Using printf with $COLUMNS worked really well, something like:

printf "%${COLUMNS}s\n" "hello"

It right justified it perfectly for me.

share|improve this answer

I just thought I would throw mine in here. It's almost exactly the same as the GRML zsh prompt (except zsh updates it's prompt a little better on new lines and back spaces - which is impossible to replicate in bash ... well very difficult at this point in time, at least).

I spent a good three days on this (only tested on a laptop running arch), so here's a screenshot and then the stuff that goes in my ~/.bashrc :)

screenshot of bash prompt in action

warning - it's a little crazy

important aside - every ^[ (such as ^[[34m) is really the escape character (char)27. The only way I know how to insert this is to enter ctrl+([v) (i.e. hit both [ and v while ctrl is held down.

# grml battery?

# battery dir
if [ -d /sys/class/power_supply/BAT0 ]; then

# ps1 return and battery
    # should be at beg of line (otherwise more complex stuff needed)

    # battery
    if [[ "$GRML_DISPLAY_BATTERY" == "1" ]]; then
        if [ -d /sys/class/power_supply/$_PS1_bat_dir ]; then
            # linux
            STATUS="$( cat /sys/class/power_supply/$_PS1_bat_dir/status )";
            if [ "$STATUS" = "Discharging" ]; then
                bat=$( printf ' v%d%%' "$( cat /sys/class/power_supply/$_PS1_bat_dir/capacity )" );
            elif [ "$STATUS" = "Charging" ]; then
                bat=$( printf ' ^%d%%' "$( cat /sys/class/power_supply/$_PS1_bat_dir/capacity )" );
            elif [ "$STATUS" = "Full" ] || [ "$STATUS" = "Unknown" ] && [ "$(cat /sys/class/power_supply/$_PS1_bat_dir/capacity)" -gt "98" ]; then
                bat=$( printf ' =%d%%' "$( cat /sys/class/power_supply/$_PS1_bat_dir/capacity )" );
                bat=$( printf ' ?%d%%' "$( cat /sys/class/power_supply/$_PS1_bat_dir/capacity )" );

    if [[ "$RET" -ne "0" ]]; then
        printf '\001%*s%s\r%s\002%s ' "$(tput cols)" ":( $bat " "^[[0;31;1m" "$RET"
        printf '\001%*s%s\r\002' "$(tput cols)" "$bat "

_HAS_GIT=$( type 'git' &> /dev/null );

# ps1 git branch
    if ! $_HAS_GIT; then
        return 1;
    if [ ! "$( git rev-parse --is-inside-git-dir 2> /dev/null )" ]; then
        return 2;
    branch="$( git symbolic-ref --short -q HEAD 2> /dev/null )"

    if [ "$branch" ]; then
        printf ' \001%s\002(\001%s\002git\001%s\002)\001%s\002-\001%s\002[\001%s\002%s\001%s\002]\001%s\002' "^[[0;35m" "^[[39m" "^[[35m" "^[[39m" "^[[35m" "^[[32m" "${branch}" "^[[35m" "^[[39m"

# grml PS1 string
PS1="\n\[\e[F\e[0m\]\$(_PS1_ret)\[\e[34;1m\]${debian_chroot:+($debian_chroot)}\u\[\e[0m\]@\h \[\e[01m\]\w\$(_PS1_git) \[\e[0m\]% "

I'm still working on making the colors configurable, but I am happy with the colors as they are now.

Currently working on a fix for the crazy ^[ character and easy color switching :)

share|improve this answer

You can use printf to do right alignment:

$ printf "%10s\n" "hello"

$ PS1='$(printf "%10s" "$somevar")\w\$ '
share|improve this answer

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.

function title {
    case "$TERM" in
        echo -en "\033]2;$1\007"

print_pre_prompt() {
    PS1L_clean="$(sed -r 's:\\\[([^\\]|\\[^]])*\\\]::g' <<<$PS1L_exp)"
    PS1L_exp=$(eval echo '"'$PS1L_exp'"')
    PS1L_clean=$(eval echo -e $PS1L_clean)
    title $PS1L_clean
    printf "%b%$(($COLUMNS-${#PS1L_clean}))b\n" "$PS1L_exp" "$PS1R"

Here it is on github: dbarnett/dotfiles/ I use it in my .bashrc like this:

source $HOME/dotfiles/
PS1='\[\033[01;34m\]\w\[\033[00m\]\$ '

Note: I also added a newline after PS1R, which makes no visual difference, but seems to keep the prompt from getting garbled if you scroll back through certain commands in your command history.

I'm sure someone else can improve on this, and maybe generalize some of the special-case-iness.

share|improve this answer

Here is a solution based on PROMPT_COMMAND and tput:

function __prompt_command() {
  local EXIT="$?"             # This needs to be first
  history -a
  local COL=$(expr `tput cols` - 8)
    PS1="💻 \[$(tput setaf 196)\][\[$(tput setaf 21)\]\W\[$(tput setaf 196)\]]\[$(tput setaf 190)\]"
    local DATE=$(date "+%H:%M:%S")
  if [ $EXIT != 0 ]; then
    PS1+="\[$(tput setaf 196)\]\$"      # Add red if exit code non 0
    tput sc;tput cuu1; tput cuf $COL;echo "$(tput setaf 196)$DATE"; tput rc
  PS1+="\[$(tput setaf 118)\]\$"
    tput sc;tput cuu1; tput cuf $COL;echo "$(tput setaf 118)$DATE"; tput rc
  PS1+="\[$(tput setaf 255)\] "

The magic is performed by:

tput sc;tput cuu1; tput cuf $COL;echo "$(tput setaf 196)$DATE"; tput rc

Which is broken down by:

tput sc                       # saved the cursor position
tput cuu1                     # up one line
tput cuf $COL                 # move $COL characters left
echo "$(tput setaf 196)$DATE" # set the colour and print the date
tput rc                       # restore the cursor position

In PS1, tput is escaped with \[ \] so that it is not counted in displayed length.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .