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

    • I bought one of these last year. I really hope I won't have to deal with this crap. My drive is in good condition at the moment.
    • Umm, read my answer again! If you have something to add or contribute, feel free. Otherwise my point was that you apparently dont want faster updates... so you want slower updates by process of elimination. If you have something to contribute, meaningful answers are better.
    • These features described above are good, but far from what developers will like the most. The main feature that developers will care and love the most it's called "Bring Your Own Models". It gives us the ability to connect to LOCAL AI models running on Ollama. The feature it's located on GitHub Copilot tab -> On the model picker where you can select "manage models" instead of paid models and then it will show you the "Bring your own models" window where you can now select Ollama and the endpoint of your local server. So if you have a beefy spec machine you can now use your own model 100% local inside Visual Studio 2026 18.7.0
    • Microsoft Teams is getting a controversial location tracking feature that users may hate by Usama Jawad Image generated with Microsoft Copilot Earlier this year, Microsoft planned to roll out a controversial location tracking feature in Teams, but following customer feedback, it decided to delay its release. The bad news is that the company has decided to launch it later this year, but it's based on roughly the same design that was shared earlier, which means that many users still have good reason to worry. Basically, Microsoft Places and Teams have received workplace check-ins via Wi-Fi. The idea is that if an employee arrives at the office and connects to their enterprise network, their profile status indicator will show them as being present in the office. For example, if you arrive at work, open Teams on your PC, and connect to the "Studio B" company Wi-Fi network, your Teams profile will indicate that you are present in "Studio B", as shown below: Microsoft says that this feature is basically a replacement for physical workplace check-in peripherals, it reduces the need to manually update your status, and it also enables co-workers to know that you're at work so that they can coordinate in-person meetings with you. IT admins can enable this workplace check-in capability at a tenant level, and users have the ability to control whether they want to enable it or not. Of course, all of that sounds great on paper, but naturally, many Teams customers may still have concerns, as they did before. This is because it enables your reporting manager and other members of the organization to track if you are at the office, when you arrive at the office, and where you are right now. This could be problematic for people who work in what they consider to be flexible work environments or hybrid setups, and this kind of location tracking could be considered an invasion of privacy. Microsoft has tried to alleviate some of these concerns by letting users know that they can manually set their location easily, which essentially overrides workplace check-in if they feel uncomfortable with it. However, that doesn't really solve the problem because your organization could enforce a workplace policy that mandates that this feature remains enabled. The Redmond tech giant has also assured users that this capability does not store historical data and is only a real-time indicator of location. Finally, it only generates a signal when you connect to a corporate network, which means that if you are working from home and connect your PC to your personal Wi-Fi, it won't broadcast your location to your employer; you will simply be shown as "Remote". Microsoft has encouraged IT admins to prepare for this change and begin informing users so they know what to expect once it begins rolling out later this year.
  • Recent Achievements

    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • 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
  • Popular Contributors

    1. 1
      +primortal
      516
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      157
    4. 4
      Steven P.
      82
    5. 5
      ATLien_0
      81
  • Tell a friend

    Love Neowin? Tell a friend!