Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
Linux console Colors And Other Trick's
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 38

Thread: Linux console Colors And Other Trick's

  1. #1
    Senior Member comtux's Avatar
    Join Date
    Sep 2004
    Location
    Wilkes Barre Pa
    Posts
    342

    Linux console Colors And Other Trick's

    Linux Console Colors & Other Tricks
    By Jay Link


    If you've just begun programming on the Linux console, you may find yourself less than enthused about the available color choices (or lack thereof). Indeed, the default -- dreary gray on black -- brings to mind Henry Ford's famous statement regarding color schemes for his Model T: "You can have any color you want, as long as it's black."


    You may have noticed that the "ls" command is capable of producing a rainbow of colors; executables are typically green, compressed files are red, and graphics (.GIF, .JPG, etc.) are purple.


    But are these special hues limited to file listings? If not, how can you apply the day-glo treatment to your own code?


    The answer is surprisingly simple: all you need are some console escape sequences.


    Try typing the following at your command prompt:

    echo -e "\033[35;1m Shocking \033[0m"

    What you should have is the word "Shocking" appearing in bright purple. (Sorry, electric pink is not an option).


    This is made possible by \033 , the standard console "escape" code, which is equivalent to ^[ , or 0x1B in hex. When this character is received, the Linux console treats the subsequent characters as a series of commands. These commands can do any number of neat tricks, including changing text colors.


    Here's the actual syntax:

    \033 [ <command> m

    (In practice, you can't have any spaces between the characters; I've just inserted them here for clarity).


    Anything following the trailing "m" is considered to be text. It doesn't matter if you leave a space behind the "m" or not.


    So this is how you turn your text to a deep forest green:

    echo -e "\033[32mRun, forest green, run."

    Note that the "-e" argument to "echo" turns on the processing of backslash-escaped characters; without this, you'd just see the full string of text in gray, command characters and all. Finally, the command "0" turns off any colors or otherwise funkified text:

    \033[0m

    Without the "0" command, your output will continue to be processed, like so:

    echo -e "\033[32mThis is green."

    echo "And so is this."

    echo "And this."

    echo -e "\033[0mNow we're back to normal."


    Running a command that uses console colors (e.g., ls) will also reset the console to the standard gray on black.



    Programming Console Colors

    Of course, escape sequences aren't limited to shell scripts and functions. Let's see how the same result can be achieved with C and Perl:

    C:
    printf("\033[34mThis is blue.\033[0m\n");

    Perl:
    print "\033[34mThis is blue.\033[0m\n";

    Available Colors

    Now, how do you know which codes do what? The first eight basic EGA colors are defined as follows:

    30 black foreground
    31 red foreground
    32 green foreground
    33 brown foreground
    34 blue foreground
    35 magenta (purple) foreground
    36 cyan (light blue) foreground
    37 gray foreground


    So, if I wanted the word "ocean" to appear in light blue, I could type the following:

    echo -e "The \033[36mocean\033[0m is deep."




    Combining Commands

    Multiple console codes can be issued simultaneously by using a semicolon (";"). One useful command is "1", which sets text to bold. The actual effect is a lighter shade of the chosen color. So, to get a light magenta (purple) as shown in the first example, you would do this:

    echo -e "\033[35;1mCombining console codes\033[0m"

    This bolding feature allows you to access the other half of the standard 16 EGA colors. Most notably, brown turns into yellow, and gray turns into bright white. The other six colors are just brighter versions of their base counterparts.


    Backgrounds

    Text backgrounds can also be set with console codes, allowing you to have white on top of red (for example). Here is the full list of available background options:

    40 black background
    41 red background
    42 green background
    43 brown background
    44 blue background
    45 magenta background
    46 cyan background
    47 white background


    What do you think this does?

    echo -e "\033[45;37mGrey on purple.\033[0m"

    Finally, here are some other noteworthy command codes:

    0 reset all attributes to their defaults
    1 set bold
    5 set blink
    7 set reverse video
    22 set normal intensity
    25 blink off
    27 reverse video off


    Conclusion

    And now you have the answer to boring, plain ol' console text. A splash of color can liven up almost any display, creating better aesthetics as well as improving the overall feel.

    Unfortunately, these techniques are limited to the console, as they don't display over telnet (unless the remote interface is also a Linux console).


    Note that the codes given here are known as ECMA-48 compliant. That is, they work on systems other than Linux. (In case you're interested, ECMA is the European Computer Manufacturers Association, a standards body similar to the ISO). Any system with a VT-102 capable console can use the color codes demonstrated above.



    Related Resources

    1. ECMA This is the page that covers Standard ECMA-48, "Control Functions for Coded Character Sets".

    2. "man console_codes" The console_codes man page contains substantial information on not only ECMA-48 compliant codes, but the Linux-specific ones as well.


    3. The Linux Documentation Project The LDP is a vast storehouse of Linux-related knowledge.



    About Author

    Jay Link is twentysomething and lives in Springfield, Illinois. Aside from Linux, his interests include mountain climbing and flying. He administrates InterLink BBS (an unintentionally not-for-profit Internet provider) in his fleeting spare moments, as well as working various odd jobs to pay the rent.

    http://www.developer.com/open/article.php/631241

    Uranus posted < http://www.ibiblio.org/pub/Linux/doc...mpt-HOWTO.html >
    Take a look here alos lot's of good info.

  2. #2
    Mentor jro's Avatar
    Join Date
    May 2004
    Location
    Pennsylvania, USA
    Posts
    1,206

    Re: Linux console Colors And Other Trick's

    Good article CT. Here is what I do in my .bashrc:

    Code:
    # Define some colors first&#58;
    red='\e&#91;0;31m'
    RED='\e&#91;1;31m'
    green='\e&#91;0;32m'
    GREEN='\e&#91;1;32m'
    blue='\e&#91;0;34m'
    BLUE='\e&#91;1;34m'
    cyan='\e&#91;0;36m'
    CYAN='\e&#91;1;36m'
    NC='\e&#91;0m' # No Color
    Then I can access these color just using their assigned vars. Here is my login welcome text.

    Code:
    echo -e "$&#123;CYAN&#125;Welcome to $&#123;HOSTNAME&#125; $&#123;USERNAME&#125;.$&#123;cyan&#125;"
    echo -e "$&#123;cyan&#125;Today is&#58;&#58;$&#123;RED&#125;$TODAY_IS"
    echo -e "$&#123;cyan&#125;Uptime&#58;&#58;&#58;&#58;$&#123;RED&#125;$MY_UPTIME$&#123;NC&#125;"
    And since I like colors in my prompt, and I like a color indicator if I am root. Here is how I do that.

    Code:
    # Check if I'm root, make the username red of so
    UC=$NC #default user color
    if &#91; $UID -eq 0 &#93;; then
         UC=$RED
    fi
    export PS1="\n$&#123;UC&#125;\u$&#123;NC&#125;@\h&#58;&#91;$&#123;CYAN&#125;\w$&#123;NC&#125;&#93;\n&#91;\@&#93;&#58;"
    Keeps the console interesting for me. I don't have to completely read everything either, the color coding tells me what I need to know faster than I can read it.
    jro -
    To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

    Linux counter#:213782
    GnuPG ID: 406238E7

  3. #3
    Senior Member comtux's Avatar
    Join Date
    Sep 2004
    Location
    Wilkes Barre Pa
    Posts
    342
    That's cool jro thank's for contributeing.
    Wenn Sie Spaß meines Englisch mich Willensfuckingtötung Sie bilden.

  4. #4
    Associate
    Join Date
    Jun 2004
    Location
    The Matrix
    Posts
    34

    Re: Linux console Colors And Other Trick's

    The Bash-Prompt howto on www.tldp.org is also really cool - you get to play even with positioning and "gradients" and stuff like that. You might also like the output of a command in your prompt - like $(date -H +%h:%m) (not sure about the exact syntax here, as I'm not on my lappy right now )
    Sam

  5. #5
    Senior Member comtux's Avatar
    Join Date
    Sep 2004
    Location
    Wilkes Barre Pa
    Posts
    342
    Thanks Uranus for the info i posted a direct link to Bash-
    Prompt in my starting post above.
    Wenn Sie Spaß meines Englisch mich Willensfuckingtötung Sie bilden.

  6. #6
    Member
    Join Date
    Dec 2004
    Location
    Baltimore, MD
    Posts
    167

    Re: Linux console Colors And Other Trick's

    Great tutorial(/info). I've been looking at some other things posted here (ie. "how to build a custom package management system"). These things should be placed in the Knowledge Base. Isn't that what it's for?

  7. #7
    Guest
    Dude, you should submitting these as knowledge base articles....

  8. #8
    Senior Member
    Join Date
    Apr 2004
    Location
    Atlanta GA
    Posts
    373

    Re: Linux console Colors And Other Trick's

    Here is a snap of my ever so complicated bash prompt:

    From left to right it tells me my current user name current hostname number of Backgrounded processes in this session amount of available free space and current date and time on line one

    The second line tells me my current directory (which changes colors to red if the directory is not writable to the current user) and tells me my Current remaining battery.

  9. #9
    Senior Member comtux's Avatar
    Join Date
    Sep 2004
    Location
    Wilkes Barre Pa
    Posts
    342
    I work on ideas here because i am constantly reformating and testing new builds you can be assured that i forget nothing the custom package management system is dam near done i am just looking for a good way to build a custom database engine for it .
    Everything in the package management post is nill compared to what i have finshed but with out the DB it is pointless.

    This post really does kinda go with everything else but untill that porject is compleat it won't find it's way to the knowledge base articles...

    I am looking up alot more info on ncurses & away to build your own custom dialogs.

    Thanks ZennouRyuu that's one of the most creative bash promps i have ever seen.

  10. #10
    Advisor
    Join Date
    Apr 2004
    Location
    orlando
    Posts
    608
    Quote Originally Posted by comtux";p="4687
    I work on ideas here because i am constantly reformating and testing new builds you can be assured that i forget nothing the custom package management system is dam near done i am just looking for a good way to build a custom database engine for it .
    Everything in the package management post is nill compared to what i have finshed but with out the DB it is pointless.

    This post really does kinda go with everything else but untill that porject is compleat it won't find it's way to the knowledge base articles...

    I am looking up alot more info on ncurses & away to build your own custom dialogs.

    Thanks ZennouRyuu that's one of the most creative bash promps i have ever seen.
    are you writing your package manager in sh or another lang? If your writing it in something that has access to the dbm API i would suggest using it for your databases. Standing on the shoulders of giants....

Similar Threads

  1. New Colors
    By peter in forum Announcements and Suggestions
    Replies: 2
    Last Post: 10-02-2006, 02:58 AM
  2. Boring Colors
    By peter in forum Announcements and Suggestions
    Replies: 7
    Last Post: 02-14-2006, 05:26 PM
  3. Mac ssh colors & mouse
    By flashingcurser in forum Linux Distros
    Replies: 1
    Last Post: 03-29-2005, 03:27 AM
  4. Awsome trick with Mozilla
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 08-10-2002, 08:50 AM
  5. BASH colors
    By airhead in forum Linux - General Topics
    Replies: 4
    Last Post: 05-11-2002, 09:59 AM

Bookmarks

Posting Permissions

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