Show only current directory name (not full path) on bash prompt

151,080

Solution 1

Change the \w (lowercase) to \W (uppercase):

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
                                                                                       ^^
           this one waaaaaay over here ------------------------------------------------+    

Have a look at the Bash Prompt HOWTO for lots of fun details. example:

user@host:/usr/local/bin$ echo $PS1
${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ 

user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\W\[\033[00m\]\$ '

user@host:bin$

The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.

If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:

$ unset PROMPT_COMMAND

Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.

Solution 2

Simple bash replace command is

${VAR/pattern_to_find/pattern_to_replace}

For showing the last directory you can just do ${PWD/*\//}, i.e. find any thing before and including the last '/' and replace it with nothing.

On my ubuntu machine I use:

export PS1='$(whoami):${PWD/*\//}#'. 

Solution 3

My solution is to show the top three and bottom 2 directories when there are more than 5

So my prompt (which has other info too) looks like:

08:38:42 durrantm U2017 /home/durrantm/Dropbox/_/rails/everquote

when my pwd is actually

/home/durrantm/Dropbox/93_2016/work/code/ruby__rails/rails/everquote

My PS1 prompt is setup as follows:

HOST='\[\033[02;36m\]\h'; HOST=' '$HOST
TIME='\[\033[01;31m\]\t \[\033[01;32m\]'
LOCATION=' \[\033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"`'
BRANCH=' \[\033[00;33m\]$(git_branch)\[\033[00m\]\n\$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH

git_branch is a function which shows current git branch, I keep it in my dotfiles, it is:

git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
  }

Solution 4

My solution for this is slightly different in regards to how do I export it, so thought I'd share it here:

Create another prompt string variable; PS5 and export the following string in your .profile / .bash_profile file:

\u: Display the current username .

\W: Print the base of current working directory.

# Display username and current directory only.
export PS5='export PS1="\u:\W$";';

Now whenever you need to use the shorthand-ed PS, just run: eval $PS5


Or even better yet, create an alias in your .bash_aliases file: (thanks to @muru)

alias PS5='export PS1="\u:\W$";';

source again, and now you can just type PS5 to switch.

Solution 5

I believe this option is much easier, by simply doing:

echo $PWD | rev | cut -d '/' -f 1 | rev

So assign this to the PS1 variable in your .bashrc file:

PS1='$(PWD | rev | cut -d '/' -f 1 | rev)'
Share:
151,080

Related videos on Youtube

agentofuser
Author by

agentofuser

I'm a software engineer with 15+ years of experience. I have written front-end and back-end code used by millions of people while at Qype GmbH (acquired by Yelp). I'm currently specializing in React, IPFS, and functional programming. I have designed, implemented, and marketed products from the ground up, lead teams of engineers, and founded my own startups, one them funded in the Start-Up Chile program. I have worked remotely for many years, both on open source projects (3x participant in the Google Summer of Code program) and commercial ones. I'm a good communicator and team member. I'm self-directed and disciplined with my time. I studied Computer Engineering at Brazil's (often ranked as) #1 higher education and research institution (both in general and specifically in Computer Science): University of Campinas. I've lived for almost 2 years in the United States. I can read, write, and speak English fluently. Feel free to get in touch at [email protected]. More contact information at https://agentofuser.com.

Updated on September 17, 2022

Comments

  • agentofuser
    agentofuser over 1 year

    The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every command wraps into the next line. How do I make it show only the last part of the path?

    This is what I have in my .bashrc:

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    
    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
        ;;
    *)
        ;;
    esac
    
  • agentofuser
    agentofuser over 14 years
    Hm... I'm afraid it's already \w, but it seems like that case statement overrides it when i'm on an xterm, and the problem seems to be with the PWD in the PROMPT_COMMAND line. Do you know what I should put there?
  • quack quixote
    quack quixote over 14 years
    \w (lower case) sets it to full path, \W (uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run.
  • agentofuser
    agentofuser over 14 years
    Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :)
  • Combine
    Combine almost 6 years
    Great answer. Make sure you reboot in order this to take effect. Thanks!
  • Roy F. Fear
    Roy F. Fear almost 6 years
    I like this answer better than the accepted one because it's generic to any situation instead of just the special parsing logic of $PS1. Fewer more powerful tools are easier to remember and compose. :)
  • muru
    muru over 5 years
    This looks like a frankenalias. Why not just use an alias or a function?
  • Carlos F
    Carlos F over 4 years
    This is awesome! Thanks a lot for this answer.