0

Is there a way to get my ubuntu command prompt to be something like:

user@host:home$

but still have the behavior of:

user@host:home$ cd projects

user@host:home/projects$

Right now, my working directory path before the home directory is cluttered with stuff from my school's servers (e.g. user@host:/blah/blah/blah/home$ ). Is there a way I can still append the directory path to the prompt as I navigate around, but just trim the beginning stuff off?

rdav
  • 1

2 Answers2

0

You can create a function and place it in your rc file (perhaps ~/.bashrc) like this:

function simplify_dir {
    local PREFIX
    if [[ $PWD == "$HOME"* ]]; then
        PREFIX=${HOME%/}
        PREFIX=${PREFIX%/*}
        echo "${PWD##"$PREFIX/"}"
    else
        echo "$PWD"
    fi
}

Also add this command to enable expansion in prompt:

shopt -s promptvars

Then set your PS1 prompt to something like this:

PS1='\u@\h:$(simplify_dir)\$ '
konsolebox
  • 60,986
  • 9
  • 83
  • 94
0

Try out this one:

PS1="[\u@\h:\w ] $ " 

Case matters:

  • \w : the current working directory, with $HOME abbreviated with a tilde
  • \W : the basename of the current working directory, with $HOME abbreviated with a tilde
Rasim
  • 1,246
  • 1
  • 11
  • 24