Take the 2-minute tour ×
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.

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 ;) –  Innuendo Nov 11 '11 at 16:24

2 Answers 2

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/
user@host: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 –  Innuendo 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:

birryree@momohime:/usr/bin
$ 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 –  Innuendo Nov 11 '11 at 16:06

Your Answer

 
discard

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.