Colorize your bash like linux.

Something simple and nice that I want to share with you.
Create .bashrc on home directory and copy this:
Code:
if [[ $- != *i* ]] ; then
  # Shell is non-interactive.  Be done now!
  return
fi

# Bash won't get SIGWINCH if another process is in the foreground.
# Enable checkwinsize so that bash will check the terminal size when
# it regains control.  #65623
# http://cnswww.cns.cwru.edu/~chet/bash/FAQ (E11)
shopt -s checkwinsize

# Enable history appending instead of overwriting.  #139609
shopt -s histappend

# Change the window title of X terminals 
case ${TERM} in
  xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
      PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'
      ;;
  screen)
      PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\"'
      ;;
esac

use_color=false

# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS.  Try to use the external file
# first to take advantage of user additions.  Use internal bash
# globbing instead of external grep binary.
safe_term=${TERM//[^[:alnum:]]/?}   # sanitize TERM
match_lhs=""
[[ -f ~/.dir_colors   ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
[[ -z ${match_lhs}    ]] \
  && type -P dircolors >/dev/null \
  && match_lhs=$(dircolors --print-database)
[[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true

if ${use_color} ; then
  # Enable colors for ls, etc.  Prefer ~/.dir_colors #64489
  if type -P dircolors >/dev/null ; then
      if [[ -f ~/.dir_colors ]] ; then
          eval $(dircolors -b ~/.dir_colors)
      elif [[ -f /etc/DIR_COLORS ]] ; then
          eval $(dircolors -b /etc/DIR_COLORS)
      fi
  fi

  if [[ ${EUID} == 0 ]] ; then
      PS1='\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
  else
      PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
  fi

  CLICOLOR="YES";    export CLICOLOR
  LSCOLORS="ExGxFxdxCxDxDxhbadExEx";    export LSCOLORS
  #alias ls='ls --color=auto'
  #alias grep='grep --colour=auto'
else
  if [[ ${EUID} == 0 ]] ; then
      # show root@ when we don't have colors
      PS1='\u@\h \W \$ '
  else
      PS1='\u@\h \w \$ '
  fi
fi

# Try to keep environment pollution down, EPA loves us.
unset use_color safe_term match_lhs

Create a .dir_colors on home directory and copy this:
Code:
# Below, there should be one TERM entry for each termtype that is colorizable
TERM ansi
TERM color-xterm
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM cons25
TERM console
TERM cygwin
TERM dtterm
TERM Eterm
TERM gnome
TERM konsole
TERM kterm
TERM linux
TERM linux-c
TERM mach-color
TERM putty
TERM rxvt
TERM rxvt-cygwin
TERM rxvt-cygwin-native
TERM rxvt-unicode
TERM screen
TERM screen-bce
TERM screen-w
TERM screen.linux
TERM vt100
TERM xterm
TERM xterm-256color
TERM xterm-color
TERM xterm-debian

# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00           # global default, although everything should be something.
FILE 00             # normal file
DIR 01;34           # directory
LINK 01;36          # symbolic link.  (If you set this to 'target' instead of a
                    # numerical value, the color will match the file pointed to)
FIFO 40;33          # pipe
SOCK 01;35          # socket
DOOR 01;35          # door
BLK 40;33;01        # block device driver
CHR 40;33;01        # character device driver
ORPHAN 01;05;37;41  # orphaned syminks
MISSING 01;05;37;41 # ... and the files they point to

# This is for files with execute permission:
EXEC 01;32

# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')

.cmd 01;32 # executables (bright green)
.exe 01;32
.com 01;32
.btm 01;32
.bat 01;32
.sh  01;32
.csh 01;32

.tar 01;31 # archives / compressed (bright red)
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z   01;31
.Z   01;31
.gz  01;31
.bz2 01;31
.bz  01;31
.tbz2 01;31
.tz  01;31
.deb 01;31
.rpm 01;31
.rar 01;31        # app-arch/rar
.ace 01;31        # app-arch/unace
.zoo 01;31        # app-arch/zoo
.cpio 01;31       # app-arch/cpio
.7z  01;31        # app-arch/p7zip
.rz  01;31        # app-arch/rzip

.jpg 01;35 # image formats
.jpeg 01;35
.gif 01;35
.bmp 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.tiff 01;35
.png 01;35
.mng 01;35
.xcf 01;35
.pcx 01;35
.mpg 01;35
.mpeg 01;35
.m2v 01;35  # MPEG-2 Video only
.avi 01;35
.mkv 01;35  # Matroska (http://matroska.org/)
.ogm 01;35  # Ogg Media File
.mp4 01;35  # "Offical" container for MPEG-4
.m4v 01;35  # MPEG-4 Video only
.mp4v 01;35 # MPEG-4 Video only
.mov 01;35  # Quicktime (http://developer.apple.com/qa/qtw/qtw99.html)
.qt 01;35   # Quicktime (http://developer.apple.com/qa/qtw/qtw99.html)
.wmv 01;35  # Windows Media Video
.asf 01;35  # Advanced Systems Format (contains Windows Media Video)
.rm 01;35   # Real Media
.rmvb 01;35 # Real Media Variable Bitrate
.flc 01;35  # AutoDesk Animator
.fli 01;35  # AutoDesk Animator
.gl 01;35
.dl 01;35

.pdf 00;32 # Document files
.ps 00;32
.txt 00;32
.patch 00;32
.diff 00;32
.log 00;32
.tex 00;32
.doc 00;32

.mp3 00;36 # Audio files
.wav 00;36
.mid 00;36
.midi 00;36
.au 00;36
.ogg 00;36
.flac 00;36
.aac 00;36

The result is this:
Screenshot_021914_09_55_47.png
 
Thanks for the tutorial and for striking my curiosity.
My shell isn't the same as yours but you did get me to look more into the bash man page.
 
For completeness sake, you can colorize tcsh(1) too. Just set the variable CLICOLOR and ls(1) will show a colourized output. You can change the colors with the LS_COLORS variable.
 
Just I pick the code from internet and just worked. Well I tried a lot of codes, I read some tutorials and tried to write a script by own but was not very complete. I just made it to colorize a small part of things. This was just the best I found and I thought to share it with you.
Tcsh is the default shell of FreeBSD? I don't remember. I always change to bash because of tab witch makes things faster.
Why FreeBSD has not his own colorized shell? Bash or the default shell. It should be. FreeBSD is most of the time a terminal even if you use GUI. So my opinion is that a colorized shell looks better on eyes and helps you even easier to understand witch is a folder or not. Yes on bash cd "TAB" give the folders but now I can see them with ls :)
 
sk8harddiefast said:
Tcsh is the default shell of FreeBSD?
It's one of the few shells that are in the base. Root's shell is, by default, csh(1).

I always change to bash because of tab witch makes things faster.
Both csh(1) and tcsh(1) have tab-completion too. Even sh(1) has some rudimentary tab-completion these days.

Why FreeBSD has not his own colorized shell?
Not everybody likes it. And it's easy to turn on if you want.
 
It shoudn't be colorized by defauld but if you want it colorized you should write something like
Code:
bash/tcsh/sh/csh_colorized="YES"
in /etc/rc.conf.
This is the best way I think because to turn it on is not easy all the time. For me that I don't know bash scripting, I must read tutorials and even then I will not have the sucess I want. The only I can do is to hope if some already made script, will work as worked now. And I tell that because I read tutorials and I tried to write my own .bashrc without success at all. The best I do was to colorize ember@Unix and that's it :(
 
From what you posted I'm happy that I use tcsh, as it seems to be more easily cofigured than bash.

To enable tab-complete just add
Code:
set filec
but according to tcsh man page
filec In tcsh, completion is always used and this variable is ignored
by default. If edit is unset, then the traditional csh comple‐
tion is used. If set in csh, filename completion is used.

If you want to enable colors in ls read ls() man page and search for CLICOLOR and LSCOLORS.
If you want to enable colors in tcsh read tcsh() man page and search for LS_COLORS and color shell variable.

I don't think /etc/rc.conf is the right place to configure shell.
 
This was just an exaple to explain that could be an option for a preconfigured colorized shell with a switch to enable it if you want.
 
sk8harddiefast said:
This was just an exaple to explain that could be an option for a preconfigured colorized shell with a switch to enable it if you want.
You can add your script to /etc/profile and it will be executed each time a login shell is started. Every user will then have the colors.
 
No. That I mean is that this could be an option as part of FreeBSD. Just an opinion... Is not hard to accomplished and could be nice as extra option for those who want this (Like me for example and I guess that I am not the only one). Well now I have the code so I can save the .bashrc and .dir_colors on my NAS to have whenever I want a colorized shell. Just I wanted to see this as an option on the base of FreeBSD.
 
For completeness sake, you can colorize tcsh(1) too. Just set the variable CLICOLOR and ls(1) will show a colourized output. You can change the colors with the LS_COLORS variable.
Also it is possible to 'colorize' the output of builtin ls-F command (TAB key) in tcsh(1)
(to enable this feature (autolist with TAB) you also need to add set autolist or set autolist = ambiguous in ~/.tcshrc)
Code:
# choices are listed only when completion fails
set autolist = ambiguous

# enable colors for the builtin ls-F (TAB)
set color = ls-F
and optionally
Code:
# "ls -a" when press TAB
set listflags = a

So when I press TAB
WBUFhf2.png


______________________________________________________
By the way, here is nice colorized bash(1) prompt.
Code:
PS1="[\A\[\033[0;32m\] \W \[\033[0m\]]\\$ "
Ado1S6J.png


(similar prompt for tcsh is set prompt = "%{\033[1;37m%}%T %{\033[1;32m%}%C%{\033[1;37m%} %#%{\033[0m%} ")


To change cursor color in bash, add
Code:
echo -ne '\e]12;#FF0000\a'
to ~/.bashrc. Where "#FF0000" is a Hex Color Code of your cursor.
 
Last edited by a moderator:
I've never understood this fascination with coloring text like this. It's a distracting blob of color that blurs into one big mess. Some claim it helps differentiate between elements, like variables and function calls, but when everything is colored, you might as well make it all white.

Reading such things is distracting because every color change results in an interruption. I've never used coloring in code but, in web development, it's one of those design considerations for linked text. How do you indicate a piece of text is a link without interrupting the viewers reading? Any change in that text is a bump in the road.

Now, code is not a story but the results are the same when I've asked UI designers.
 
Tastes differ.
For me, color in my terminal is helpful a lot, because I can see where are symlinks,
directories etc with no need to read their names :D.
Also colored prompt is helpful a lot IMHO, because you'll never be confused when
you'll try to recognize which account you are using, root or user.
vBlDCoo.png

And no need to specify current active account when you're using colored prompt, so you got an extra space.
OgoerX0.png

Also colored prompt is useful when you're trying to scroll back your terminal output and to find some commands, that were executed.

Also it is "eye candy" :)
30VSm80.png

VS
0I9ZiQ4.png

Colored output is much more readable for me.

But yes, colored prompt shouldn't be by default,
because FreeBSD is not Ubuntu :D or even PC-BSD.
Just create your own ~/.tcshrc or ~/.bashrc and use it.
 
Last edited by a moderator:
While, for my needs at least, I don't see the point of default coloration--and, of the Linux distributions I play with, if you do a minimal installation, they don't colorize the prompt either--I can certainly see the use of say, making root's prompt red, or having various servers that people log into having their own color. For example, if I have 6 terminals open on my screen, and the remote servers have a blue prompt, I can tell at a glance that these are the ones that shouldn't get poweroff commands.

Prompts are a personal sort of thing, which is why I like FreeBSD and several Linux ways of giving you a very plain rc file and allowing you to customize if you wish. Heh, I see I have an ancient page on prompts, from when I was more into it.
http://www.scottro.net/qnd/qnd-prompt.html

I think Debian has a colored prompt by default, and I'm sure many others do. But for me, the Debian bashrc, as an example, is too long and overly complex for what should be a relatively simple thing.
 
Hello,

This code only colorized PS and directory listed.
dir_colors isn't supported on FreeBSD. So, .dir_colors file and related code snippets don't work.

I'd to colorized .log, .png, .bz2 etc. with ls command How can I do it?

Thanks in advance.
 
Hello,

This code only colorized PS and directory listed.
dir_colors isn't supported on FreeBSD. So, .dir_colors file and related code snippets don't work.

I'd to colorized .log, .png, .bz2 etc. with ls command How can I do it?

Thanks in advance.
This isn't possible with base ls(1). While it supports coloring via LSCOLORS [1] it cannot color files differently based on file extensions.

GNU utilities support what you want so just install sysutils/coreutils.The utilities will all have a g prefix so it might be useful to also use alias ls="gls --color" (or similar) in your shell config.

[1] This is different to LS_COLORS that the GNU utilities use and gdircolors(1) exports.
 
This isn't possible with base ls(1). While it supports coloring via LSCOLORS [1] it cannot color files differently based on file extensions.

GNU utilities support what you want so just install sysutils/coreutils.The utilities will all have a g prefix so it might be useful to also use alias ls="gls --color" (or similar) in your shell config.

[1] This is different to LS_COLORS that the GNU utilities use and gdircolors(1) exports.
Is there a way to pipe the output of gls --color into "more" and not get all the color codes expanded into ESC characters?
 
Is there a way to pipe the output of gls --color into "more" and not get all the color codes expanded into ESC characters?
Use more -R (setting an alias will avoid extra typing).

more(1):
Code:
-R or --RAW-CONTROL-CHARS
          Like  -r,     but  only ANSI    "color"    escape sequences are output in
          "raw" form.  Unlike -r, the screen appearance is maintained cor-
          rectly  in  most    cases.     ANSI "color" escape sequences are se-
          quences of the form:

           ESC [ ... m

          where the    "..." is zero or more color  specification  characters
          ...
 
Back
Top