Making your command line more helpful and fun!

You can do a lot more with your command line font colors than turn them on and off. How about picking your own colors? How about customizing your prompt so that it balks when you make a mistake?

The default terminal settings on most linux systems is to use colors to serve as hints about file types and permissions. For example, you'll see directory names in blue, executables in green, and file archives in red. And, if you don't want to see the colors -- like when they're just too distracting, you can turn them off easily. The command line colors are set when the ls command is aliased to an ls command that uses the --color option like this:

alias ls='ls --color=auto'

To turn off font colors for some period of time, you can just turn off the alias.

$ unalias ls

To turn off font colors all of the time, you can put the unalias command in your ~/.bashrc file or remove the alias command that sets it up in the first place, though that might be in your ~/.bashrc file or some system file.

You can also make individual changes to your font color palette. Say you don't like seeing blue as the font color that displays the names of directories. Maybe your background color is a close blue. You can change the color used for just directories with a command like this:

LS_COLORS=$LS_COLORS:'di=35:' ; export LS_COLORS

So, where did that come from? The di in that command signifies directories. It and other particular file types that you can specify are listed here:

di = directory
fi = file
ln = symbolic link
pi = fifo file
so = socket
bd = block (buffered) special file
cd = character (unbuffered) special file
or = symbolic link pointing to a nonexistent file
mi = non-existent file pointed that a symbolic link
     points out (showing up in the long listing)
ex = an executable file
*.rpm = files with the ending .rpm

The 35 in the command indicates the color -- 35 means purple. Some of the options in the list below can be combined. For example, you could use a purple font with a green background if you use di=35;42.

Here is a list of your options, though I have to admit to some of these color combinations combined with flashing would probably make me dizzy.

0  = default colour
1  = bold
4  = underlined
5  = flashing text
7  = reverse field
31 = red
32 = green
33 = orange
34 = blue
35 = purple
36 = cyan
37 = grey
40 = black background
41 = red background
42 = green background
43 = orange background
44 = blue background
45 = purple background
46 = cyan background
47 = grey background
90 = dark grey
91 = light red
92 = light green
93 = yellow
94 = light blue
95 = light purple
96 = turquoise
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background

Another cute trick that I picked up online is making the color of your prompt change depending on whether the command you just entered completed successfully or not. This trick uses some of what we've seen above (e.g., selecting the right font colors), but adds a function to your ~/.bashrc file that formats your prompt after evaluating the exit status of your last command.

Add a function like this to your ~/.bashrc file and your prompt will go from green (normal) to red (something is wrong) when you type a command that fails for some reason.

function exstat {
    EXSTAT="$?"
    RED="\[\033[1;31m\]"
    GREEN="\[\e[32;1m\]"
    OFF="\[\033[m\]"

    if [ "${EXSTAT}" -eq 0 ]
    then
         PS1="${GREEN}>${OFF} "
    else
         PS1="${RED}>${OFF} "
    fi
}

Notice that the RED and GREEN settings correspond to colors listed above. If the exist status is 0 (success), your prompt will be green. If your exit status is anything else (error), your prompt will be red. You can customize the colors by changing the the RED and GREEN settings and then applying them in the lines that follow.

The prompts in this post's image were done using nearly the same settings, but with the additional words "ok" and "OOPS!". Here are the lines for those settings -- maybe just right for the next Unix beginner you add to your staff.

    if [ "${EXSTAT}" -eq 0 ]
    then
        PS1="${GREEN}ok>${OFF} "
    else
        PS1="${RED}OOPS!>${OFF} "
    fi

Don't forget that you can also customize your prompt in other ways. You might, for example, want to include the system's name in the prompt so that you remember what system you are logged into. Use a \h for that -- PS1="${GREEN}\h>${OFF} " or add your username with \u to help you remember what account you're currently using -- PS1="${GREEN}\u@\h>${OFF} ".

shs@boson>

Messing around with your prompt and screen colors can help remind you what you're doing an where -- particular if you, like me, often find yourself working on several systems at a time and jumping from window to window.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags playprogrammers

More about

Show Comments
[]