[GENERAL] Bash Prompt Tips


Recommended Posts

Another simple how-to written by myself ;-)

Here are some bash commands to make your prompt look cool ;-)

\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patchlevel (e.g., 2.00.0)
\w the current working directory
\W the basename of the current working direc­ tory
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could be used to embed a terminal con­ trol sequence into the prompt
\] end a sequence of non-printing characters

This is what my ~/.bashrc file looks like, simple yet effective.

PS1="(\d \u@\h : \W)\n (\T) $ "

And this is the output of it

(Sun Mar 14 adam@eatbabies : adam)
(10:01 PM) $

All that needs to be done is in your favorite text editor, place those commands in a file called .bashrc in your HOME directory and you're all set. ;-)

Could someone post the bash color codes and an example on how to add them? Thanksssssss ;-)

Link to comment
Share on other sites

you can use setfont to change the color, but I don't think it carries over between sessions. You can set it to call in a startup script. It would be system wide. I have never had a reason to fool around with my bash prompt....but I shall try to figure out how to set colors in the .bashrc :)

Link to comment
Share on other sites

The basic idea behind BASH prompt colors is to take the standard options, and interspace them with escape sequences that control the color:

Lets imagine I want a prompt that looks like this:

[evnshow:mycomputer /home/evnshow ] $

I would define it in my .bash_profile:

#NORMAL BASH PROMPT
export PS1="[\u:h \W] \$ "

#COLORED BASH PROMPT
export PS1="<SOME COLOR SEQUENCE>[\u:\h \W] \$ "

The question is "what the hell is <SOME COLOR SEQUENCE> supposed to look like?" And the answer is:

\[033[BRIGHTORNOT;COLORNUMBERm\]

BRIGHTORNOT is a number: either 1 or 0 that determins whether the color is "bright". 0 is yes, 1 is no (or it's the other way around: I think terminal.app has a bug so I'm not 100% sure)

COLORNUMBER is replaced with a number from 30 - 39 they are:

30: Black/Dark grey

31: Red

32: Green

33: Yellow

34: Blue

35: Magenta

36: Fuscia

37: White/light grey

38: "Default" foreground color

So with that in mind, lets say you wanted to make a nice Green prompt:

export PS1="\[\033[1;32m\] ? ? [\u:\h \W ] \$ "

#The first part: \[\033[ is the escape sequences that says "Hey I'm formatting the prompt"
#The next part: 1;32 is "I want normal green text from this point on"
#Then \] closes off the escape sequence.
#
# THe ending is exactly the same as a normal bash prompt for username:hostname /WORKINGDIR $

of course this will turn everything you type green, but maybe we want bright blue text instead:

export PS1="\[\033[1;32m\] ? [\u:\h \W] \$ ? ?\[\033[0;34m\]"

# this makes the text green, draws the prompt, and then sets all text back to
# the bright blue color with \[\033\0;34m\]

some people will use \e[colorcode] to setup bash prompt colors but I've had some wierd quirks with line wrapping and scrolling that just didn't make any sense to me.

For those interested, here is the relevant section from my .bash_profile:

TERM=xterm-color

PS1="\[\033[1;37m\](\[\033[0m\]\u\[\033[1m\]@\[\033[0m\]\h\[\033[1m\]:\[\033[0m\]\W\[\033[1;37m\])\[\033[0m\]\[\033[1m\]\\$\[\033[0m\] "

export TERM
export PS1

I have my terminal set to use a nice blue as the foreground color, so it looks like the attachment. Without that, it will be grey and white which is still nice, but not as cool.

edit: attachment

Link to comment
Share on other sites

Come on, this has only been about as long as a ZX81 :)

Check out CPM/86 options.

OK, sarcastic but good find and I hope you have fun.

/Dougie.

Link to comment
Share on other sites

  • 8 months later...

I've started doing more with the shell, rather than just the occasional mount for my usb drive, and found the following useful:

http://linuxreviews.org/beginner/Bash-Beginners-Guide/

and more on scripting

http://linuxreviews.org/beginner/Bash-Scri...oduction-HOWTO/

Figure others new and learning the shell would appreciate the links...

Link to comment
Share on other sites

I've started doing more with the shell, rather than just the occasional mount for my usb drive, and found the following useful:

http://linuxreviews.org/beginner/Bash-Beginners-Guide/

and more on scripting

http://linuxreviews.org/beginner/Bash-Scri...oduction-HOWTO/

Figure others new and learning the shell would appreciate the links...

585062617[/snapback]

tldp has a nice guide for beginners.

Link to comment
Share on other sites

  • 4 weeks later...

Just thought I'd add a little tip in here:

You can make different parts of your bash prompt have different colours depending on the return value of the last command you ran. I have mine set up so that the last '$' character (or '#' if I'm root) is blue normally, but if the previous command exits with an error, it turns red. This is useful if you sometimes tend not to pay too much attention to wether a command worked properly or not - I'm often guilty of just assuming things worked without checking, and this gives me an easy visual indicator.

PS1='\[\033[01;31m\]\h \[\033[01;34m\]\W\[\e[01;$(($??31:34))m\] \$ \[\e[0m\]'

The important part is the "$(($??31:34))" section - this makes the \$ turn blue or red depending on the return value of the last command. You could change the 31 and 34 for other numbers to vary between other colours, and you could place these anywhere in your prompt to vary any parts you choose.

Hope thats of interest to someone...

Link to comment
Share on other sites

Awesome tidbit Rezza, my bashrc is now:

\[\033[01;32m\]\u@\h \[\033[01;34m\]\W\[\e[01;$(($??31:34))m\] \$ \[\033[00m\]

Which gives:

g=green, etc
gggggggg b b/r
nick@root ~ $

with the $ either blue or red, depending on the last command, thanks to rezza

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.