Show only current directory name (not full path) on bash prompt
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.
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/*\//}#'.
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/'
}