If I am in a deep directory, let's say:


then when I open up terminal, it says


and then I write my command. That is very long, and every line I write in the terminal goes to the next line. I want to know if there's a way so that it only displays my current directory. I want it to display:

bob@bob-ubuntu: A3/$

This way it's much clear, and always I can do pwd to see my entire directory. I just don't want the entire directory visible in terminal because it takes too much space.


Since bash 4, to shorten the depth of directory in command-line is done by using PROMPT_DIRTRIM in the .bashrc file. Just remember to reopen your terminal.


See the Bash Manual for more information.



will be trimmed to


  • 13
    This is EXACTLY what I was looking for. Setting something like export PROMPT_DIRTRIM=3 in your .bashrc is the perfect middle ground between \W and \w. See: gnu.org/software/bash/manual/html_node/Bash-Variables.html
    – spkane
    Nov 29 '18 at 19:45
  • 1
    Yeah, this is perfect. Jul 3 '20 at 11:08
  • @spkane, there's no need to export that to child processes. Just set it as a shell variable, as shown. May 26 at 12:49

You need to modify PS1 in your shell startup file (probably .bashrc).

If it's there already, its setting will contain \w, which is what gives your working directory. Change that to \W (upper case). The line in bashrc file looks like below:

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

Log out and in again, or do:

. .bashrc

or (you need to add this prefix '~/' if you are in others directory)

source ~/.bashrc

(or whatever your file is).

If it isn't there, add something like:

PS1='\u@\h: \W:\$'

to .bashrc or whatever. Look up PS1 in the bash manual page to get more ideas.

Be careful; bash can use several more than one initialisation file, e.g. .bashrc and .bash_profile; it may be that PS1 is set in a system-wide one. But you can override that in one of your own files.


Assuming you're using bash, change the prompt string (variable PS1) so that it has \W instead of \w.

e.g. if your PS1 is currently \u@\h:\w\$, set it to \u@\h:\W\$

To make this permanent, you will have to change it in your bash startup files - e.g. ~/.bash_profile or ~/.bashrc.

see man bash and search for PROMPTING for full details and a list of backslash-escaped special characters.

  • I have 4 mentions of PS1 Do I change all 4?
    – K Split X
    Jul 22 '17 at 13:49
  • 4 mentions where? in ~/.bash_profile? it should do no harm to change all instances of \w in PS1 to \W. or you could just set the prompt to whatever you like at the bottom of the script.
    – cas
    Jul 22 '17 at 13:51

This is portable to all sh shells.

Assign to PS1 in one of your shell startup files:

PS1='${PWD##*/} $ '

The prompt will look like

dir $

Where dir is the base name of the current directory.

The $PWD variable contains the current directory path, and ${PWD##*/} will strip the everything up to and including the last / in that path.

The single quotes prevents the shell form evaluating the variable substitution at the time of assignment (the value of $PS1 will be evaluated each time the prompt is displayed).

The PS1 variable should not be exported as it's only used by the current shell.


in this case you will have to edit PS1 ,

insted of \w , you will have a command or a variable that shows shortned path :

original PS1

PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$'

change it to

PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]${PWD##*/}\[\033[00m\]\$'

Note this will put the username insteed of ~ if you are in your home dir !

to avoid that you will need a few commands insteed of ${PWD##*/} e.g.

if [[ "${PWD}" == "${HOME}" ]] ; then printf \~; else echo -n ${PWD##*/}; fi

the new PS1 will look like the following

PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]`if [[ "${PWD}" == "${HOME}" ]] ; then printf \~; else echo -n ${PWD##*/}; fi`\[\033[00m\]\$'

oOps while i am trying to save the world i had noIdea|forgoten the \W



Allows you to truncate a path like:

From: /home/sodium/Projects/Personal/Sierra/Super/Long/Path/HolyAvacado

To: »Projects/Sie…/Sup…/Lon…/Pat…/HolyAvacado/


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.