How can I shorten my command line (bash) prompt?

by Michael Durrant   Last Updated September 19, 2015 13:01 PM

Currently it is:

michael@Castle2012-Ubuntu-laptop01:~/Dropnot/webs/rails_v3/linker/spec/controllers$

Outside of renaming my machine and directory structure...

How could I make it be something more like:

michael:controllers$


Answers 4


To change it for the current terminal instance only

Just enter PS1='\u:\W\$ ' and press enter.


To change it "permanently"

In your ~/.bashrc, find the following section:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Remove the @\h, and replace the \w with an uppercase \W, so that it becomes:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u:\W\$ '
fi

Save, exit, close terminal and start another to see the result.


Tons more options!

  • See here for a more extensive howto, with many more options
  • See this answer for using up a tiny Python script to set the prompt so that the shortening only occurs when you are deep in a directory structure.
izx
izx
June 02, 2012 19:04 PM

PROMPT_DIRTRIM=3

Will show only last 3 directory name in bash prompt. You can choose 1 to show only current directory.

The effect:

/var/lib/apt/lists# PROMPT_DIRTRIM=3
.../lib/apt/lists# 
santosh Mahto
santosh Mahto
May 02, 2014 06:49 AM

This is my preferred prompt setting:

added in ~/.bashrc

PS1='[\u@\h \W]\$ '    

it looks like this:

[user@hostname dirname]$

(with a space after the $ sign)

unwastable
unwastable
October 10, 2014 03:08 AM

I realize this is super old but since nobody suggested creating an alias I figured I'd post. Using Bash Prompt Escape Sequences I made an alias shorten

In ~/.bash_aliases here you will notice the $Blue var to set the prompt colour which you can omit or change based on preference I also clear the terminal when calling shorten.

alias c='clear'

alias shorten='PS1="$Blue$USER:\W$ "&& c'

To achieve the OP's desired prompt string:

alias shorten='PS1="$USER:\W$ "'

I have colours defined in ~/.bashrccopy and pasted from https://wiki.archlinux.org/index.php/Color_Bash_Prompt. On a side note what's with ansi code colours? I'm confused just looking at it.

Blue='\e[0;34m'         # Blue
Allie Carver
Allie Carver
October 26, 2015 21:03 PM

Related Questions






New user, broken bash prompt parsing?

Updated September 01, 2018 16:02 PM