How to Shorten Bash Prompt in Linux?

If you are working with a deep directory structure, then you might have noticed a lengthier bash prompt. This tutorial will explain various tricks that lets you to shorten bash prompt. For example, if you are working within a directory /var/www/website/content/assets/styles/css/, then based on your settings, you might see a bash prompt as shown below:


If you are annoyed with such lengthier bash prompts, then there are few tricks to shorten it.

shorten bash prompt

Shorten bash prompt in Linux

Method 1: Editing bashrc file.

Step 1: Open .bashrc file

# vim ~/.bashrc

Step 2: Lookout for the code similar to the one shown below

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

Step 3: In the above code, lookout for the line that sets PS1 (both within if & else condition) and remove ‘@\h‘ and replace ‘\w‘ with ‘\W‘ (uppercase W)

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

Step 4: Save and exit from the file.

Now, you may source ~/.bashrc to see the immediate change in the bash prompt or logout & login again.

Method 2: Setting PROMPT_DIRTRIM variable.

If you are using bash version 4 & above, then you can simply set PROMPT_DIRTRIM variable.

bash --version
 GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
[root@portal-dashboard:/var/www/website/content/assets/styles/css/]# PROMPT_DIRTRIM=3

and hit enter.

You will see the shortened prompt as shown below:


The value ‘3’ for PROMPT_DIRTRIM means, three directories levels to be shown. You can choose to set your desired directory level (the value should be greater than zero).

To make this change permanent, you need to add the below line in ~/.bashrc


Method 3: Trim out all the parent directories and show only current working directory

Set PS1 variable as below:

[root@portal-dashboard:/var/www/website/content/assets/styles/css/]# PS1="[\W]\\$ "

You should see shortened prompt as shown below:

[root@portal-dashboard css]#

To keep it permanent, set the variable in ~/.bashrc file.

Method 4: Trim username, hostname & other directories

If you want to get rid of username, hostname & other directories except the current working directory, then set PS1 variable as shown below:

[root@portal-dashboard:/var/www/website/content/assets/styles/css/]# PS1="[\W]\\$ "

You should see a cleaner prompt as below:


To keep it permanent, set the variable in ~/.bashrc file.

Related Content
vps germany
Disclaimer: The content published in this article is the views of the author only. Techglimpse does not gurantee accuracy, completness or validity. If you believe the content on this post violates your copyright, please send us a mail for removal. Read more.