In ksh93
:
PS1='${PWD#${PWD%?/*/*/*}?/} \$ '
share/doc/libnl-3-dev $ _
PS1='[${HOSTNAME%%.*}:${PWD#${PWD%?/*/*/*}?/}] $USER% '
[host:share/doc/libnl-3-dev] user% _
If you want it to also replace $HOME
with ~
, something nastier is needed:
PS1='$(d=${PWD/#$HOME/"~"};printf %s "${d#${d%?/*/*/*}?/}") $ '
~/w/maemo $ cd sb2-pathmaps
w/maemo/sb2-pathmaps $ _
PS1='$(d=${PWD/#$HOME/"~"};printf %s "[${HOSTNAME%%.*}:${d#${d%?/*/*/*}?/}]") $USER% '
[host:w/maemo/sb2-pathmaps] user% _
All this should also work in bash
, though bash
has its own prompt escapes (eg. \h
for ${HOSTNAME%%.*}
) and path shortening mechanism (with PROMPT_DIRTRIM
).
Also, the nastier variant will be really nasty, because bash
, unlike ksh93
, will fork()
a separate process for each $(...; printf ...)
command substitution, even if it contains only builtins. This also holds true for pdksh
derived shells, like mksh
.
zsh
has prompt escapes quite similar but not identical to tcsh
:
zsh$ PS1='[%m:%3c] %n%# '
[host:share/doc/libnl-3-dev] user% _
Note:
The $HOSTNAME
variable is not set by default in ksh93
; instead of it you could use the uname
builtin (after enabling it with PATH=/opt/ast/bin:$PATH
; the /opt/ast/bin
path doesn't need to exist):
PS1='$(d=${PWD/#$HOME/"~"};h=$(uname -n); printf %s "[${h%%.*}:${d#${d%?/*/*/*}?/}]") $USER% '
Unlike the \h
escape in bash or %m
escape in zsh
or tcsh
this will track the hostname changes.