Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I currently have my prompt as

durrantm@Castle2012-Ubuntu-laptop01:~/Dropnot/webs/rails_apps/linker (master)

which I really like as it shows my username, machine, directory and git branch!

How can I shorten the machine part though?

i.e. how could In have it be:

durrantm@Castle...:~/Dropnot/webs/rails_apps/linker (master)

I tried various combination of h:0:6 but had no luck getting a correct syntax that would work.

My .bashrc uses this:

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

PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\n\$ '
share|improve this question
Why not shorten the hostname itself? from Castle2012-Ubuntu-laptop01 to be castle2012, for example. –  aularon Feb 8 '14 at 15:39
That's too easy ;) Yes that worked well. Thanks! –  Michael Durrant Feb 8 '14 at 15:57

2 Answers 2

up vote 2 down vote accepted

Improving terdon's answer, you don't even need to invoke those two processes (hostname and cut). Just use bash's string manipulation:

PS1="\[\033[01;32m\]\u@${HOSTNAME:0:10}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\n\$ "
share|improve this answer
If the - is to be the delimiter, the much better solution is ${HOSTNAME%%-*}. –  Patrick Jul 3 '14 at 22:35

I have to agree with @aularon that it would make more sense to shorten your hostname, that's a ridiculously long name. However, you can also get what you want by parsing it:

## For example, take the first 10 chars of hostname
name=$(hostname | cut -c -10);

## Now use $name in your PS1 instead of \h
PS1="\[\033[01;32m\]\u@$name\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\n\$ "

That should produce a prompt like:

durrantm@Castle2012:~/Dropnot/webs/rails_apps/linker (master)
share|improve this answer
+1 I'm going with the hostname change but this info is useful and may help others. –  Michael Durrant Feb 8 '14 at 15:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.