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

    • When will the Photos app be updated to remember the window size and position when reopened? They addressed this issue in a 2024 version of the app (though I can't recall the build number). Unfortunately, after that specific version, the problem persists! Please prioritise this fix in your K2 schedule. Additionally, the Snipping Tool has lost the ability to capture the Windows Taskbar starting from the 2024 version!
    • Same, never saw it on Android or iOS. Guess only some people got it *shrugs*
    • Anthropic pulls Fable 5 and Mythos 5 after US export control order by Pradeep Viswanathan In April this year, Anthropic launched the Claude Mythos Preview frontier model with state-of-the-art cyber and coding capabilities for a select set of companies around the world. After preparing appropriate guardrails, early this week, Anthropic launched Claude Fable 5 and Mythos 5, its most capable AI models. Claude Fable 5 is for general users and comes with strict safeguards, while Mythos 5 is designed with fewer safeguards for cybersecurity and biology use cases. Today, Anthropic abruptly suspended access to its Fable 5 and Mythos 5 AI models for all customers after receiving an export control directive from the US government. The company received the directive from the government today at 5:21 p.m. ET, and the received letter did not provide any details regarding the national security concern. Anthropic understands that the government became aware of a method to bypass, or “jailbreak,” Fable 5, which might be the reason behind the directive. The order was issued under national security authorities and requires the company to suspend all access to Fable 5 and Mythos 5 by any foreign national, whether they are inside or outside the United States. The restriction also applies to foreign national employees working at Anthropic. As a result, the company has disabled both models for all customers to ensure compliance. Access to previous Anthropic models like Opus and Sonnet is not affected by this government order. The company highlighted that it had developed strong safeguards to reduce the possibility that Fable is misused for tasks related to cybersecurity. In fact, many developers are complaining that the safeguards are going overboard. Additionally, the company worked with the US government, the UK AISI, multiple private third-party organizations, and internal teams to red-team Fable’s safeguards for thousands of hours. Finally, Anthropic noted that no testers have yet been able to find a universal jailbreak on Fable 5. As expected, Anthropic disagrees that a narrow potential jailbreak should lead to the recall of a commercial model used by hundreds of millions of people. It warned that applying this standard across the AI industry could effectively halt new frontier model deployments. Anthropic concluded by mentioning that it is working to restore access to Fable 5 and Mythos 5 as soon as possible and plans to share more details within the next 24 hours.
    • Brave Browser 1.91.172 is out.
  • Recent Achievements

    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
    • Rookie
      Rimplesnort went up a rank
      Rookie
    • One Year In
      Markus94287 earned a badge
      One Year In
    • One Month Later
      Markus94287 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      176
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      ATLien_0
      92
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!