# Putting colour in your shell



## xy16644 (Jul 30, 2009)

In the past when I have tinkered with Linux I noticed that the shell has colour codings for various items like directories etc.

Is it possible to do the same in FreeBSD? I find having the colour coding can help me find things in a directory with many items alot faster.

Also, is it possible for FreeBSD to show what directory I am in without having to continually type pwd? In Linux it use to show this where FreeBSD puts the hostname.


----------



## dennylin93 (Jul 30, 2009)

Yes, it is possible. Simply use `$ ls -G`. For convenience, I use

```
alias ls        ls -FG
```
in ~/.cshrc. It is also possible to modify the colours as well, but I just use the defaults.

As for the path, set a shell prompt that shows the user, host, and path. My ~/.cshrc:

```
set prompt = "%B[%n@%m %~]%# "
```


%B Starts/stops boldfacing mode
%n is the username
%m is the host
%~ is the path
%# is the prompt ($ for normal users and # for root)

More options can be found here: tcsh(1).


----------



## xy16644 (Jul 30, 2009)

I put:

```
set prompt = "[%B%n@%m %~]%# "
```

in my ~/.cshrc file and logged out and back in again but I didn't seem to make a difference. Is there a service I have to restart for this change to kick in?


----------



## dennylin93 (Jul 30, 2009)

No, there shouldn't be. Try `$ source ~/.cshrc`.

Are you using csh or tcsh? Different shells would require different settings. `$ echo $SHELL` should tell you which shell you're using.


----------



## xy16644 (Jul 30, 2009)

If I login to Webmin and check my account it says:

/bin/sh


----------



## MG (Jul 30, 2009)

Example for sh:

Put in your ~/.profile: (/etc/profile for everybody)


```
export LSCOLORS=hxgxbxBxHxfxDxHxHxhxhx
```

I thougt it was in the handbook, but can't find it no more.
Here's another BSD-related page about it:
http://plug-and-pray.blogspot.com/2008/02/lscolors.html

color example for sh script:


```
#!/bin/sh
colred ()
{
  printf '\033[1;31;40m'
}

colnormal ()
{
  printf '\033[0m'
}

# begin

printf "This is "
colred
printf "red"
colnormal
printf ", right?\n"
```


----------



## SirDice (Jul 30, 2009)

If you use (t)csh there's no need to alias ls -G just:

`$ setenv CLICOLOR`

You can change you default shell with `$ chsh`. Try not to change root's default shell.


----------



## dennylin93 (Jul 30, 2009)

MG said:
			
		

> I thougt it was in the handbook, but can't find it no more.



It's in the man pages: ls(1). See the environment section for related options.


----------



## aragon (Jul 30, 2009)

I use the builtin ls(1) with colour support as shown above, but you can also install misc/gnuls if you want the same behaviour as linux's ls.


----------



## xy16644 (Jul 30, 2009)

I tried:


```
alias ls        ls -FGl
```

which worked but when I logged out and back in again I was back to the defaults?


----------



## aragon (Jul 30, 2009)

You need to add it to your shell's startup file.  For csh, ~/.cshrc.  For sh, ~/.profile.  For bash, ~/.bash_profile.

See your shell's man page for more info.

My .bash_profile for example:


```
export LSCOLORS=Hxfxcxdxbxegedabagacad
alias ls='ls -FG'
```


----------



## xy16644 (Jul 30, 2009)

I am busy installed the bash3 port as I keep hearing about it ;-)


----------



## xy16644 (Jul 30, 2009)

I just installed bash, very nice!

I set the ~/.bash_profile file as follows:


```
DIR=Ex
SYM_LINK=Gx
SOCKET=Fx
PIPE=dx
EXE=Cx
BLOCK_SP=Dx
CHAR_SP=Dx
EXE_SUID=hb
EXE_GUID=ad
DIR_STICKY=Ex
DIR_WO_STICKY=Ex

export LSCOLORS="$DIR$SYM_LINK$SOCKET$PIPE$EXE\
$BLOCK_SP$CHAR_SP$EXE_SUID$EXE_GUID\
$DIR_STICKY$DIR_WO_STICKY"

export CLICOLOR="YES"
```

Much easier on the eyes and it shows the directory I am in so I dont have to contunally type pwd.

I tried setting 


```
alias ls='ls -FGl'
```

in roots .bash_profile but it doesn't seem to work? Works fine when I login as a normal user but not when logged in as root, whys this? The colour coding looks fine, just not the ls alias...


----------



## Carpetsmoker (Jul 30, 2009)

I don't know about bash, but for tcsh you should drop the equals sign (=), i.e.
alias ls "ls -FGl"


----------



## tangram (Jul 31, 2009)

xy16644 said:
			
		

> I tried setting
> 
> 
> ```
> ...



Is root using bash?


----------



## dennylin93 (Jul 31, 2009)

It's best to leave root's shell as the default (csh), so just  put

```
alias -FGl
```
in /root/.cshrc.


----------



## xy16644 (Jul 31, 2009)

Is there a reason for leaving roots shell as csh?


----------



## SirDice (Jul 31, 2009)

xy16644 said:
			
		

> Is there a reason for leaving roots shell as csh?



AFAIK there's no real reason, changing root's shell is just considered to be bad practice.
If you want a root account with a different shell consider enabling the toor account and change the shell for that.


----------



## olegrpg (Jul 31, 2009)

This is my .cshrc shell tcsh


```
if ($?prompt) then
        # An interactive shell -- set some stuff up
        set filec
        set history = 100
        set savehist = 100
        set autolist = TAB
        set mail = (/var/mail/$USER)
        if ( $?tcsh ) then
                bindkey "^W" backward-delete-word
                bindkey -k up history-search-backward
                bindkey -k down history-search-forward
                if ($uid) then
                        set prompt = "%{\033[32m%}%B<%n@%{\033[33m%}%m%{\033[33m%}>%b%{\033[0m%}%/ # "
                else
                        set prompt = "%{\033[31m%}%B<root@%{\033[33m%}%m%{\033[33m%}>%b%{\033[0m%}%/ # "
                endif
        endif
endif
```


----------



## olegrpg (Jul 31, 2009)

And i have user name-green color and root-red color


----------



## SirDice (Jul 31, 2009)

I don't have any colors in my prompt but I did add something to make xterm (or a putty session) change the title:


```
switch($TERM)
	case "xterm*":
		setenv TITLE "%{\033]0;%n@%m:%~\007%}"
		breaksw
	default:
		setenv TITLE ""
		breaksw
endsw

<snip>

	if ( $?tcsh ) then
        <snip>
                set prompt = "${TITLE}%n@%m:%~%#"
        endif
endif
```


----------



## MG (Jul 31, 2009)

xy16644 said:
			
		

> Is there a reason for leaving roots shell as csh?



I like the tab-completion and Ctrl-R history editing of bash. So bash is my root shell.

If your /usr/local is another filesystem, be sure bash is in your root filesystem in case your /usr/local refuses to get mounted for any reason.


----------



## tangram (Jul 31, 2009)

Add the following to your .cshrc to enable tab completion and correction:


```
if ($?prompt) then
        # An interactive shell -- set some stuff up
        set autolist
        set autocorrect =       ambiguous
        set complete    =       enhance
        set correct     =       cmd
endif
```

To use Ctrl-R to search backwards the history add the following to .cshrc:

```
if ( $?tcsh ) then
                       bindkey "^R" i-search-back
endif
```

Essencially there isn't anything that bash does that t/csh can't do.


----------



## lme@ (Aug 1, 2009)

I like my tcsh prompt like this:

```
Sat, 01. Aug 2009,14:10:00
<FreeBSD 8.0-BETA2> [maggie:~]
lars@pts/5 >
```

The code for this is:

```
set prompt = "\n%d, %D. %w %Y, %P\n<`uname -sr`> [%m:%~]\n%B%U%n@%l%u %#%b "
```


----------



## Drunky (Aug 6, 2009)

using only one colored function 


```
> alias
grep    (grep --color=auto)
```


----------



## z0ran (Aug 9, 2009)

lme@ said:
			
		

> I like my tcsh prompt like this:
> 
> ```
> Sat, 01. Aug 2009,14:10:00
> ...



looks awesome, i have same now, thanks, but i was wondering what is "@pts/0" means, thanks again!


----------



## DutchDaemon (Aug 9, 2009)

pts(4)


----------



## Alt (Aug 10, 2009)

I use following prompt

```
set prompt='%{^[[1;50;1m%}[%n@%m %D.%w %t %~]%#%{^[[0;0;0m%} '
```
This gives you prompt like this:

```
[alt@forsakens.ru 10.Aug 9:12am ~]>
```
Its colored and its simple to see history and where is previous command 

P.S. Thanks to Drunky for `grep` coloring info!


----------



## z0ran (Aug 10, 2009)

I found what is @pts/2 means, wasn't there any man page, but today i was reading book..realy good book from Dru Lavigne "BSD HACKS" so many nice staff in there..anyway, she said like, if you like to know who is in which terminal:

```
%who -Hu
NAME             LINE     TIME         IDLE  FROM            
z0ran            pts/0    Aug  8 16:26   .   (:0.0)
z0ran            pts/1    Aug  8 16:32   .   (:0.0)
z0ran            pts/2    Aug  9 23:39 15:42 (:0.0)
```

not so big deal to share my excitement with forum...i liked the prompt, blame @ime


----------



## DutchDaemon (Aug 10, 2009)

Ah, the man file only appeared in FreeBSD 8..
http://www.freebsd.org/cgi/man.cgi?...ktion=4&manpath=FreeBSD+8-current&format=html


----------



## z0ran (Aug 10, 2009)

thanks DutchDaemon


----------



## morbit (Aug 10, 2009)

MG said:
			
		

> I like the tab-completion and Ctrl-R history editing of bash. So bash is my root shell.
> 
> If your /usr/local is another filesystem, be sure bash is in your root filesystem in case your /usr/local refuses to get mounted for any reason.



OR If you nuke /usr/local/lib , you will lose bash also.


----------



## speed (Aug 12, 2009)

I use the followings in .cshrc.


```
setenv  LSCOLORS ExGxFxdxCxegedabagExEx
setenv  CLICOLOR yes

set prompt = "[%{\033[0;32m%}%n%{\033[0m%}@%{\033[0;32m%}%m%{\033[0m%}] %{\033[1;33m%}%/\n%{\033[0;32m%}#%{\033[0m%} "
```


----------



## Alt (Aug 12, 2009)

btw, anyone know where to find a manual describing "{\033[0;32m%}" colors ?


----------



## Voltar (Aug 12, 2009)

Alt said:
			
		

> btw, anyone know where to find a manual describing "{\033[0;32m%}" colors ?



I had this in my bookmarks from awhile back about colorizing scripts, some of it seems to apply here (color codes at least). Might be helpful: http://www.faqs.org/docs/abs/HTML/colorizing.html


----------



## bb (Aug 12, 2009)

tangram said:
			
		

> Essencially there isn't anything that bash does that t/csh can't do.



Almost. Redirecting stderr is one thing. This can be an issue under some conditions, but normally you won't need it.

After all, I think that tcsh is a better interactive shell than bash. Only for more complex tasks or for scripting, the bourne shell should be used. Often, it will be something like that:


```
/some/command | while read line; do ...; done
```

As for the original question: I don't like colors in a terminal, because they can make the output unreadable. Especially, if you don't like prompts and use xterm background colors to not confuse your ssh sessions.


----------



## SirDice (Aug 12, 2009)

bb said:
			
		

> Almost. Redirecting stderr is one thing.


That's >&, >&!, >>& and >>&!.


----------



## john_doe (Aug 12, 2009)

Talking about colorful prompt, wouldn't it be better to use sequences provided by termcap(5)? If terminal type doesn't support colors it would still be readable. Smth like
	
	



```
if ( $?prompt ) then
    # screen(1) title-string escape sequence
    if ( $term != dumb ) then
        set title_esc='\ek\e\\'
    else
        set title_esc
    endif

    set tc_bold       = `echotc md`
    set tc_red        = `echotc AF 1`
    set tc_green      = `echotc AF 2`
    set tc_yellow     = `echotc AF 3`
    set tc_reset_attr = `echotc me`

    set promptchars = '.!'
    set prompt = "%{$tc_bold%}%{$tc_red%}(%{$tc_yellow%}%~%{$tc_red%})%{$tc_green$title_esc%}%#%{$tc_reset_attr%} "
endif
```
So, when you fire up your tcsh(1) and colors are not available it would look like
	
	



```
$ TERM=dumb tcsh
echotc: Unknown capability `md'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `me'.
(~). echo hello
hello
```


----------



## roddierod (Aug 17, 2009)

SirDice said:
			
		

> AFAIK there's no real reason, changing root's shell is just considered to be bad practice.



If you change root to bash which is in /usr/local/bin and something happens to your system and there is a big old crash you can lose root access because the system can't find the shell...it happened to me once so I learned the hard way


----------



## SirDice (Aug 17, 2009)

roddierod said:
			
		

> If you change root to bash which is in /usr/local/bin and something happens to your system and there is a big old crash you can lose root access because the system can't find the shell...it happened to me once so I learned the hard way



If the system cannot find the shell for root it will ask for one


----------



## pali112 (Dec 17, 2018)

In /root/.cshrc


```
setenv  LSCOLORS "ExGxFxdxCxegedabagExEx"
setenv  CLICOLOR yes
```

Work perfect for me.


----------



## SirDice (Dec 17, 2018)

pali112 You are responding to a 9 year old thread.


----------

