Colorize your Csh or Bourne shell. Bourne shells include sh(1), ksh variants (Korn shells), and zsh(1). csh(1) is a different kind of shell which uses a different syntax for commands.


Environment
Environment values can be added to your shell configuration file(s). These variables for Bourne shells can be set in $HOME/.profile. sh, zsh and variant ksh Posix Bourne shells can read from these files. Bash can also use these files, but it has variations in its more specific customization files.

Csh uses many of the same basic variables as Bourne shells, but the syntax incorporating them is different. This shell uses .cshrc for customization.

set can be used to see basic variables from bourne shells, and csh. For csh, setenv can be used to see its additional variables. export is used from sh, ksh variants, and zsh. These commands are typed without arguments to view their variables, which can be adjusted. These same commands and additional arguments can be used to set these variables. Bourne shells vary in how their variables are set.

echo with $ and the specific variable written in capital letters can also be used to see that variable from your shell.

To enable some configurations for sh, the following can be run from the commandline.
Code:
ENV=$HOME/.shrc; export ENV
This can also be added to the configuration file. Then, restart the shell. Other Bourne shells reference a different configuration file.


Changing colors of ls output
The environment value LSCOLORS refers to the color output for ls(1) in the terminal. This value be added to your shell configuration file(s).

ls(1):
Code:
LSCOLORS           The value of this variable describes what color to
                         use for which attribute when colors are enabled with
                         CLICOLOR or COLORTERM.  This string is a
                         concatenation of pairs of the format fb, where f is
                         the foreground color and b is the background color.
Code:
The color designators are as follows:
                               a     black
                               b     red
                               c     green
                               d     brown
                               e     blue
                               f     magenta
                               g     cyan
                               h     light grey
                               A     bold black, usually shows up as dark grey
                               B     bold red
                               C     bold green
...
Code:
The order of the attributes are as follows:

                               1.   directory
                               2.   symbolic link
                               3.   socket
                               4.   pipe
                               5.   executable
                               6.   block special
                               7.   character special
                               8.   executable with setuid bit set
                               9.   executable with setgid bit set
                               10.  directory writable to others, with sticky
                                    bit
                               11.  directory writable to others, without
                                    sticky bit
Code:
                   The default is "exfxcxdxbxegedabagacad", i.e., blue
                         foreground and default background for regular
                         directories, black foreground and red background for
                         setuid executables, etc.

CLICOLOR sets this from the shell's configuration file, so ls -G or its alias aren't needed. COLORTERM and TERM are mentioned as similar in function to CLICOLOR.

Different shells set these variables in different ways. Csh uses setenv, and Zsh uses export for setting these. Here's examples for Csh and Zsh:
.cshrc
Code:
setenv LSCOLORS "gx"
setenv CLICOLOR
.zshrc
Code:
export LSCOLORS=ex
export CLICOLOR

LSCOLORS is for basic ANSI colors. LS_COLORS is an alternative variable with a different syntax that comes with more colors, but it's available with certain shells: this variable can be found in the manpages of shells that use it. An example of this variable:
Code:
LS_COLORS='di=32:ln=35:so=33:pi=31:ex=34:bd=34;40:cd=35;40:su=30;42:sg=37;42:tw=30;46:ow=31;46'


Setting prompt colors
To set up your prompt in csh, use: set prompt= with the variables within double quotes. Variables within .cshrc are prefixed with %.

The colors go within the bracket parenthesis {}, prefixed with %. \33 is the character escape, so the next variables can be used. Within that is an ANSI variable for colors. [x;xxm%

Code:
set prompt = "%{\033[1;36m%}%~ %{\033[0m%}%# "
In this example, [36;m% and [0m% are ANSI code for colors. %~ is used to show the current directory.

In my setup, the directories of the ls output and in the prompt have nearly the same color. I adjusted the color, so it can contrast to a dark background.
csh.jpg


Code:
set prompt = "%{\033[1;33m%}%N:%{\033[1;36m%}%~ %{\033[0m%}%# "
adds the user name with colors to indicate that. This is useful for when indicating root on the console. Jail name can also be displayed on the prompt in a similar way.
csh-root.jpg



zsh uses PS1= for setting the prompt. PS2= is for the prompt, when the shell is waiting on an interactive response. Some have used, PROMPT= for setting the prompt in zsh. For color variables, zsh uses the word names, that are within %F{ } and end enclosed by %f.


More
Examples of csh and zshrc configurations can be viewed and shared on: Thread share-your-tcshrc-file.21040 and Thread share-your-zshrc-file.62653. For a related thread on Csh, see: Thread howto-color-files-by-extension-in-tcsh.29669. For a guide on Zsh: https://zsh.sourceforge.io/Guide/zshguide02.html.

.shrc and .kshrc were a bit more challenging to set up. There are scarce examples or instructions for these on the forums. There are also not as many examples of these on the Internet as other shells. More on colorizing Bourne shells can be added below.

More on colorizing Bourne shells, Csh or information that's closely related to the subject is welcome to be added to this thread.

This is unrelated to colorizing, but autoload -U history-search-end can be added to .zshrc for auto completion.


For Bash, see: Thread colorize-your-bash-like-linux.45006.
 
You could use something like like this to give some colors to man (this is for zsh):

Bash:
LESS_TERMCAP_mb=$(printf '\e[1;31m');        export LESS_TERMCAP_mb
LESS_TERMCAP_md=$(printf '\e[1;31m');        export LESS_TERMCAP_md
LESS_TERMCAP_me=$(printf '\e[0m');           export LESS_TERMCAP_me
LESS_TERMCAP_so=$(printf '\e[1;33;40m');     export LESS_TERMCAP_so
LESS_TERMCAP_se=$(printf '\e[0m');           export LESS_TERMCAP_se
LESS_TERMCAP_us=$(printf '\e[0;4;35m');      export LESS_TERMCAP_us
LESS_TERMCAP_ue=$(printf '\e[0m');           export LESS_TERMCAP_ue

Which in this case would render a man like this:

screen_1654864397.png
 
Thanks for this. Now I know what env things I need to unset and aliases to clear.
My opinion:
I hate the colorization stuff being turned on by default. Hurts my eyes and actually hides the data from me.
And if you are scripting the output, you go from a text file to a binary file with all the escape sequences which is a pain when you bring it up in an editor.

But I recognize that some people love it.
 
I hate the colorization stuff being turned on by default. Hurts my eyes and actually hides the data from me.
Agree. Most of the things I see on the web make a clown college out of everything while claiming it helps find things in code. But, in this case, the OP has sensible, mild settings that don't hit you in the face like elephant dung at Ringling Brothers.
 
I hate the colorization stuff being turned on by default. Hurts my eyes and actually hides the data from me.
It's meant to do the opposite, which works fine if done in a sane way...
And if you are scripting the output, you go from a text file to a binary file with all the escape sequences which is a pain when you bring it up in an editor.
A program that outputs escape sequences by default when stdout is NOT a tty is broken.

Still it isn't binary either. Esc is a control character defined in ASCII and it's the only one used.
 
I agree that the OP settings are reasonable, but some colors like the yellows and some greens hit the eye right in the peak of visible spectrum and seem brighter than they actually are. So it's often the juxtaposition of the colors that cause issues (color photography type of stuff).

I'm starting to believe that a bunch of folks here grew up on black&white VT100, graduated to green|orange on black and their (our) eyes just can't deal with "modern sensibilities".

Over time, contrast and brightness become primary drivers of useability. Higher contrast, with lower brightness is often easier to look at all day.

So please OP just take this as a bit of grumbling from someone barely younger than dirt.
 
Intended for sharing Bourne and Korn shell configurations: Thread share-your-shrc-kshrc-or-mkshrc-file-segments-bourne-or-korn-shell.85467. I had mksh in mind when referring to ksh on this thread. mksh is a little more user friendly and a little more advanced than ksh. Mksh is easier to set up than Ksh. Also a lot or all from .kshrc applies to .mkshrc.

Thread learning-materials-for-shell-shell-scripting.66660 is a thread asking about learning materials for sh and ksh.


Can another thread be used for discussing colorization of terminals/shells.
 
Last edited:
Yes. Please move discussions to the other parts of the forums. I'm fine with actual information being added to a how-to but let's try to keep how-tos nice and clean :)
 
Note that running FreeBSD 13.1, one can configure ls to underline attributes instead of using bold color.
Set a capital character (e.g. X) as background color to underline the corresponding attribute:
export LSCOLORS=EXfxcxdxbxegedabagacad
 
I have:
Code:
export COLORFGBG='15;0'
export COLORTERM=truecolor
export TERM=xterm-256color
export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:'
 
Colorizing SH as of FreeBSD 14.0
For sh(1) available as of FreeBSD 14.0. /usr/local/etc/profile can be used for system wide Bourne shell settings, that aren't overriden by local files. I might use an if-then statement, for when settings are similar with small differences for different users here. The typical place for customization and for overriding system wide settings is ~/.shrc and/or ~/.profile. I avoid using /etc/profile for custom settings, as that can be overriden by updates.

To colorize ls output:
Code:
export CLICOLOR="YES" # any value can be put in quotes,
                      # and this is also required for customizing
export LSCOLORS="gx"  # these are my custom values
The syntax slightly varies from the settings of other Bourne shells.

To colorize terminal line, including prompt:
This version has added escape sequences, which are used on top of "\e" as the escape sequence for ANSI codes. The code must be within "\[" and "\]", and there must be a "\e" for the start of the code, so the ANSI color codes go between \[\e and \].

In my example:
Code:
export PS1="\[\e[1;33m\]\u:\[\e[1;36m\]\w \[\e[0m\]\$ "
In this example, "1;" is optional to make the font bolder for the text it covers. The last part brings the color and font back to default for the end of the prompt, and where you type.

These examples are similar to the output displays in above examples. These can also be tested on the command line, until you find terminal colors that suit you.

For details on the recent version of sh Bourne shell:

I couldn't get many of these functions working in previous versions, and in previous versions, a reboot may have been needed for customizations to take effect.

Now, sh is almost suitable as a replacement for csh in FreeBSD. However, while these settings are fully working for terminal (VT) consoles, hacking is needed for them to work for terminal emulators. If ENV is set to /etc/profile, it will pick up ENV from multiple shell configuration files, which are listed from there. Use export -p, env or echo $ENV to see your ENV settings. For the desktop as a short term hack, ENV can be set through .xinitrc or xsession, without an & at the end. For instance, export ENV=/etc/profile (this calls /usr/local/etc/profile) that goes before any terminal or other command line program is started through this script. This may need attention, because there has to be a better way for this to be set permanently for desktop terminals.

Now, there's a better answer for Thread the-bourne-shell-prompt-color.85914, that's if you're using FreeBSD 14 (or newer).


An unrelated caveat is to copy lines from /root/.shrc needed for history autocomplete and other functions.

For a reference on basics of Bourne Shell, since information on Bourne itself without being about other shells is rare: https://www.ibm.com/docs/en/aix/7.3?topic=shells-bourne-shell
 
You could use something like like this to give some colors to man (this is for zsh):
I tried to do this for man under sh(1). From looking it up, this depends on that manpages use a text viewer set by PAGER, which the default is less.
man(1):
Code:
-P pager
             Use specified pager.  Defaults to “less -sR” if color support is
             enabled, or “less -s”.  Overrides the MANPAGER environment
             variable, which in turn overrides the PAGER environment variable.
Code:
 MANCOLOR    If set, enables color support.

 MANPAGER    Program used to display files.

                 If unset, and color support is enabled, “less -sR” is used.

                 If unset, and color support is disabled, then PAGER is used.
                 If that has no value either, “less -s” is used.
less(1) uses TERMCAP:
Code:
 LESS_TERMCAP_xx
              Where "xx" is any two characters, overrides the definition of
              the termcap "xx" capability for the terminal.
This can be seen in the example in a relevant post above, of manpage coloring using zsh along with the screenshot. This is for parts which are the color red.
LESS_TERMCAP_md=$(printf '\e[1;31m'); export LESS_TERMCAP_md

What I have in my sh configuration file:
sh:
export LESS_TERMCAP_md=$(tput bold; tput setaf 6)
I rather use this without tput, and try the default newer settings for sh. I couldn't get it to work, by using the ANSI escapes, but it was close. This will help someone else get closer to colorizing their manpages, and understanding it a bit more.
2023-12-03-235104_1263x628_scrot.png

The only setting in that example was for cyan blue.
 
I have different prompt colours for user (green) and root (red) to not confuse if I'm logged in as root.
Additionally I use another colorset in my /root/.vimrc to also distinguish if I'm root here.
 
Back
Top