Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Anurag Rana
Anurag Rana

Posted on • Originally published atMedium on

     

How to shorten the bash shell prompt

So sometimes it happens that you are working on command terminal and you navigate inside a directory then again in the inner directory and so on until half of the screen width is filled with prompt string. Example below.

> _rana@Brahma: dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8$ls_
Enter fullscreen modeExit fullscreen mode

Now I want to display just the last directory in the terminal prompt string so that I am left with enough width space to execute lengthy commands.

For this, We need to change the prompt string from the.bashrc file. Below are the steps -

  1. Go to your home directory.
  2. Open.bashrc file.
  3. Search for string ‘PS1’. To search quick open the file in vi and type /PS1 and hit enter.
  4. now you will find below code. (or similar to it) [I am working on Ubuntu 16.04]
> _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_
Enter fullscreen modeExit fullscreen mode
  1. Replace the ‘w’ (highlighted in bold) with capital W. So your new code will be.
> _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_
Enter fullscreen modeExit fullscreen mode
  1. Restart the terminal and navigate inside any directory. You will see just the last directory in teminal prompt.
 _rana@Brahma: dir8$_
Enter fullscreen modeExit fullscreen mode

Keep Learning.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Backend Developer. Working on Python and Java. Writes about Python and Django on https://www.pythoncircle.com
  • Joined

More fromAnurag Rana

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp