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

  markjensen said:
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...

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

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

    • No registered users viewing this page.
  • Posts

    • Lmao. Cries about not playing those games not installed and yet don't ever want to touch them.
    • If I want to merge folder trees that have a similar structure, Beyond Compare is always my first choice. It's not free but it's awesome. If I want to just scan a whole drive/folder and find duplicates that are taking up space, I like Czkawka.
    • Claude Code gets throttled as Anthropic rolls out fresh usage caps by David Uzondu Claude Code, the AI-in-terminal utility developed by Anthropic and launched back in February, is getting updated usage limits following weeks of user complaints about being abruptly cut off. Many developers on the "$200/month Max plan" found their access blocked after just a few requests, with no explanation from the company. In a recent thread posted to X, the AI lab explained that it has seen "unprecedented demand since launch," pointing to some of its heaviest users who were running the tool continuously in the background 24/7, with one person reportedly consuming tens of thousands of dollars in model usage on a single $200 subscription. Anthropic also claimed that some users were violating its usage policy by sharing and reselling accounts, which impacts system capacity for everyone. These factors all led the company to announce new weekly limits that will be added on top of the existing five-hour caps, effective August 28. Max plan subscribers will have the option to buy additional usage at standard API rates if they hit their cap. Here's what the new weekly limits look like: Pro Plan ($20/month): An estimated 40 to 80 hours of usage with the Sonnet 4 model. Max Plan ($100/month): An estimated 140 to 280 hours with Sonnet 4 and 15 to 35 hours with the top-tier Opus 4 model. Max Plan ($200/month): An estimated 240 to 480 hours with Sonnet 4 and 24 to 40 hours with Opus 4. Per TechCrunch, the company provided these hour-based estimates, noting that the actual numbers may vary based on the size of a project's codebase. What's interesting is how this new structure compares to the old marketing. Anthropic previously advertised its $200 Max plan as offering 20 times more usage than the Pro plan. Based on these new hourly estimates, that multiple is now closer to six. It is possible the 20x figure still applies when measured in tokens or raw compute, but, according to TechCrunch, the company has not clarified that point.
    • I don't give a rat's f### what Trumpette, the Putin puppet likes!
  • Recent Achievements

    • First Post
      Gladiattore earned a badge
      First Post
    • Reacting Well
      Gladiattore earned a badge
      Reacting Well
    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      637
    2. 2
      ATLien_0
      260
    3. 3
      Xenon
      164
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      107
  • Tell a friend

    Love Neowin? Tell a friend!