Saturday, October 10, 2015

Color up Mac OS X Terminal

The default Mac OS X terminal ships without any color. That is, when you search for directories and files using ls, the contents are all displayed in dull and monotonous color unlike what you would get in Ubuntu.
Terminal on Mac OS X

Terminal on Ubuntu


In order to obtain the color features on Mac terminal, one needs to edit ~/.bash_profile file, which will be read every time you launch the terminal. One can use vim to open up the file (or create)
$ vim ~/.bash_profile
and edit the file with the following content
export CLICOLOR=1
export LSCOLORS=DxFxCxGxBxegedabagaced


Now, whenever you open up the terminal on Mac, this file will be read and you will see pretty colors with ls command similar to Ubuntu.  You can change the color option if you want to more resemble Ubuntu. Oh, by the way, I also added
if [[ $- =~ "i" ]]; then
     echo "~/.bash_profile being read"
fi
in order to remind me that this file is being read. This message appears every time I open up the terminal as well. Here, the if statement tells it to print out only when it is in the interactive mode; this is because scp expects to transfer data via stdin and stdout, and I want to make sure that the echo statement does not get in the way. I also have added
if [[ $- =~ "i" ]]; then
     echo "/etc/bashrc being read"
fi
in the /etc/bashrc file. Since this file is protected, one needs to edit this with sudo command
$ sudo vim /etc/bashrc


So, now when I open up the terminal and enter ls command, I get to see some colors as desired, along with the messages that remind me of the two files being read.


To tell you more about how bash on Mac reads files as it starts, the order is as follows.
1. read /etc/bashrc
2. If ~/.bash_profile exists, read it and go to #5. If not, go to #3
3. If ~/.bash_login exists, read it and go to #5. If not, go to #4
4. If ~/.profile exists, read it.
5. Done

This pertains to login shell; that is when you open up the terminal. If you were to run bash from the terminal by the command 
$ bash
this will only read ~/.bashrc file. I have edit this file to have
if [[ $- =~ "i" ]]; then
     echo "~/.bashrc file being read"
fi
so whenever I open up the bash from the terminal, I see this message. However, since the color feature has been already set when I opened up the terminal, I am still able to see the colors!


If, however, I execute the bash shell with the --login option,
$ bash --login
it will read the files as if I started the new terminal.


Lastly, when you type
$ exit
 to exit, or
$ logout
to logout from the login shell. When you are exiting or logging out the login-shell, the file ~/.bash_logout will be read. I again have edited this file to include
if [[ $- =~ "i" ]]; then
     echo "~/.bash_logout being read"
fi
and this is what I get when I exit terminal.


By the way, how Mac OS X's terminal deals with bash is a bit different from how Ubuntu deals with. I will go through how Ubuntu deals with bash in the later post.

No comments:

Post a Comment