FedoraForum.org - Fedora Support Forums and Community
Results 1 to 7 of 7
  1. #1
    bytesniper Guest

    colored bash prompts

    This is nothing big but i figured i would share because i find this incredibly useful and i would think that anyone who spends any amount of time at the command line would appreciate this. Its a minor /etc/bashrc modification (idea borrowed from gentoo) that colors the bash prompt so that normal users are green and root is colored red. with minor modification the coloring can be changed to anything (example: members of certain groups are various colors or certain users are a specific color, etc )

    in /etc/bashrc (in fedora 7) on line 37 you will see this:
    Code:
       [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
    that sets your local bash prompt. PS1 is the bash prompt environment variable, you can view what yours is set to with set | grep PS1

    comment that line out by placing a "#" (no quotes) at the beginning of it. and on the next line paste (or enter) the following:
    Code:
    if [ $EUID == 0 ]; then
      [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;31m\]\u@\h\[\033[01;34m\] \W \$\[\033[00m\] "
     else
      [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] "
    fi
    and then save. close, and open up another terminal window to see how it worked.

    joseph

  2. #2
    Join Date
    Aug 2007
    Location
    London Postbox (the red one)
    Age
    52
    Posts
    3,868
    Pretty cool, ill have to keep a copy of your post, id never of thought of doing that,

  3. #3
    scottro's Avatar
    scottro is offline Retired Community Manager -- Banned from Texas by popular demand.
    Join Date
    Sep 2007
    Location
    NYC
    Posts
    8,127
    I have my own little page on it that goes into a bit more detail. (It covers zsh and ksh).

    Yours is better for the beginner though, I tend to be too wordy.

  4. #4
    Join Date
    Apr 2018
    Location
    Athens
    Posts
    1

    Thumbs up Re: colored bash prompts

    Quote Originally Posted by bytesniper
    This is nothing big but i figured i would share because i find this incredibly useful and i would think that anyone who spends any amount of time at the command line would appreciate this. Its a minor /etc/bashrc modification (idea borrowed from gentoo) that colors the bash prompt so that normal users are green and root is colored red. with minor modification the coloring can be changed to anything (example: members of certain groups are various colors or certain users are a specific color, etc )

    in /etc/bashrc (in fedora 7) on line 37 you will see this:
    Code:
       [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
    that sets your local bash prompt. PS1 is the bash prompt environment variable, you can view what yours is set to with set | grep PS1

    comment that line out by placing a "#" (no quotes) at the beginning of it. and on the next line paste (or enter) the following:
    Code:
    if [ $EUID == 0 ]; then
      [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;31m\]\u@\h\[\033[01;34m\] \W \$\[\033[00m\] "
     else
      [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] "
    fi
    and then save. close, and open up another terminal window to see how it worked.

    joseph
    Joseph I was looking for something like this one and it works great, thank you very much!
    I also made a very small addition to your code, so I added a " \ " before $, to get the "$" sign for simple users and "#" sign for root.
    Take care.
    Code:
    if [ $EUID == 0 ]; then
      [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;31m\]\u@\h\[\033[01;34m\] \W \\$\[\033[00m\] "
     else
      [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \\$\[\033[00m\] "
    fi

  5. #5
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,042

    Re: colored bash prompts

    /etc/bash.rc does not match your /etc/bash.rc

    The new way is to setup a SetPS1.sh as shown

    vi /etc/profile.d/SetPS1.sh
    if [ $EUID == 0 ]; then
    PROMPT_COMMAND='[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;31m\]\u@\h\[\033[01;34m\] \W \\$\[\033[00m\] " '
    else
    PROMPT_COMMAND='[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \\$\[\033[00m\] " '
    fi:
    Last edited by lsatenstein; 22nd April 2018 at 07:01 PM. Reason: PROMPT_COMMAND
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  6. #6
    Join Date
    Jun 2013
    Location
    USA
    Posts
    373

    Re: colored bash prompts

    (In the Archlinux forums this is where an admin would pop in with an admonishment about necrobumping posts from over a decade ago, the advice therein very possibly outdated)

    ;-)
    Mind the gap.

  7. #7
    Join Date
    Dec 2013
    Location
    United Kingdom
    Posts
    5,966

    Re: colored bash prompts

    not just in the Arch forum, the same applies here. if you have any colour scheme for bash questions or want to create a guide applicable to current command sets and terminology please do so in a new thread. this thread is closed and moved to the EOL section.

Similar Threads

  1. Colored command-line prompts
    By roundboy in forum Installation, Upgrades and Live Media
    Replies: 3
    Last Post: 14th June 2009, 04:21 PM
  2. Colored packages?
    By jaapvstr in forum Using Fedora
    Replies: 0
    Last Post: 5th December 2006, 02:09 PM
  3. Colored snow
    By ddan in forum Installation, Upgrades and Live Media
    Replies: 1
    Last Post: 20th October 2006, 01:10 PM
  4. colored bash
    By thesource in forum Using Fedora
    Replies: 2
    Last Post: 26th May 2005, 04:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •