Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example I typed such commands:

user@my-machine:~$ cd ABC
user@my-machine:~/ABC$ cd long-name
user@my-machine:~/ABC/long-name$ cd another-long-name
user@my-machine:~/ABC/long-name/another-long-name$ ls

So, I've chosen directory I need, and I need to work in it ( lots of commands ). But half of my console line is the prompt line. It's not very comfortable. Can I shorten it for a while? For example I'll make it user@my-machine:~foo$ and when I finish working in this directory - I'll turn this shortness off.

Is there a way to do this?

share|improve this question

migrated from stackoverflow.com Nov 12 '11 at 13:35

This question came from our site for professional and enthusiast programmers.

In the future, this is a very superuser.com post, not stackoverflow question. – Rob Nov 11 '11 at 16:19
Ok, I understand, next time I won't make this mistake ;) – Larry Cinnabar Nov 11 '11 at 16:24
up vote 5 down vote accepted

The prompt format is in the PS1 environment variable; see the bash manpage, prompting section for format specifiers.

10:50:39 user@host:1996 ~$ echo $PS1
\t \u@\h:\! \W$

The \W only prints the top directory like you (?) want. (\w prints the whole thing like you have now).

10:50:45 user@host:1997 ~$ PS1='\u@\h:\W$ '
user@host:~$ cd test
user@host:test$ cd testdir/

You'll probably want to save the old PS1 in a variable (say, $ export OLDPS1=$PS1) first so you can restore it when you're done ($ PS1=$OLDPS1).

share|improve this answer
Yeap: ` export OLDPS1=$PS1` and PS1='\u@\h:\W$ is what I needed – Larry Cinnabar Nov 11 '11 at 16:07

I just put the prompt on a second line, and the user/directory information is put on the first.

Example of my bash prompt:

$ command-here

If you're using bourne-shell or bash, the environment variable PS1 determines the formatting of the prompt.

I have this in my ~/.bash_profile (you might use .bashrc, I have my .bashrc source my .bash_profile).

# ~/.bash_profile
PS1="\u@\h:\w\n$ " # \u means user name
                   # \h means hostname
                   # \w is working directory
                   # \n is newline
                   # $ is my prompt

And my .bashrc

# ~/.bashrc
source ~/.bash_profile

For fun, my zsh prompt looks like this:

birryree@momohime:~/.oh-my-zsh master* 
λ > 

The master* means I'm on the master branch of a git repository (~/.oh-my-zsh is a git-controlled directory), and the directory is unclean.

You can get extremely fancy with your bash prompts, I used to do colors and everything before I switched to zsh.

This section has all the escapes:

share|improve this answer
thanks for explanation – Larry Cinnabar Nov 11 '11 at 16:06

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.