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
https://www.neowin.net/forum/topic/148706-general-bash-prompt-tips/
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 :)

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

  • 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...

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.

  • 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...

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

    • No registered users viewing this page.
  • Posts

    • My son is in Monaco right now, and I was checking his location in Apple's Find My app. I noticed that Prince Albert's Palace was blurred out on the satellite imagery in both Find My and Apple Maps. I checked Google Maps, and the palace wasn't blurred there. Does Apple have some kind of process where property owners can request that their homes be blurred on Apple Maps?  
    • No, it was THAT ugly and I’d rather forget it completely existed.
    • There is a lot of reasons not to use Edge but faster fixes and security updates is not one of them.
    • Can't reproduce. I installed Edge, went to neowin.net > accepted the cookie consent > used menu to go to forums, everything loads and I can browse around the forums. If you can't interact with the dialog on the forums for some reason, go to the main site and accept the cookie consent there? It is true that the site will not function properly until the cookie consent is accepted or rejected,. it's a legal requirement and I also know that certain VPN/ad blockers block it, which is a user related issue and not a neowin.net problem.   This is not our cookie consent dialog. Gotta love browser hijacking... /s Edit: this may be what Californians see, I will confirm with our consent provider.
    • Google Chrome 149.0.7827.115 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | Direct Link | 131.0 MB Download: Google Chrome Offline Installer 32-bit | Direct Link | 119.0 MB Download page: Google Chrome Portable Download: Chrome ARM64 | Direct Link View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      491
    2. 2
      PsYcHoKiLLa
      170
    3. 3
      +Edouard
      164
    4. 4
      Steven P.
      85
    5. 5
      ATLien_0
      76
  • Tell a friend

    Love Neowin? Tell a friend!