Hiparco

Recetas y trucos de GNU/Linux e informática en general

Alojado en http://guimi.net

Truncar el pwd

Truncar el pwd que aparece en PS1.
Solo recomendable en usuarios, no en root.
Esto nos permite ver algo así:

 :.+cion/Prueba_de_d$ pwd
/home/guimi/direccion/Prueba_de_d

Para ello editamos el archivo del usuario ~/.bashrc:

 vi ~/.bashrc
# Funcion para truncar el pwd extraida de Debian-Administration.org
#+ <a href="http://www.debian-administration.org/articles/548" class="externo">http://www.debian-administration.org/articles/548</a>
#+ posted by mikhailian
function truncate_pwd
{
 if [ $HOME == $PWD ]
 then
   newPWD="~"
 elif [ $HOME ==  ${PWD:0:${#HOME}} ]
 then
   newPWD="~${PWD:${#HOME}}"
 else
   newPWD=$PWD
 fi

  local pwdmaxlen=15
  if [ ${#newPWD} -gt $pwdmaxlen ]
  then
    local pwdoffset=$(( ${#newPWD} - $pwdmaxlen  ))
    newPWD=".+${newPWD:$pwdoffset:$pwdmaxlen}"
  fi
}

PROMPT_COMMAND=truncate_pwd
PS1="${ttyname}@\[${HOST_COLOUR}\]\h\[${RESET_COLOR}\]:\${newPWD}\\$ "
Category: GNU/Linux, Prompt