My .bash_profile on My Mac

Added Jun 25, 2017: Kary, 我衰記性差,好對唔住,唔應該開你玩笑,求你原諒🌹

Original text:

I’m currently using an CUSTOMIZED Mac😁 I like the default shipped within macOS. I have changed the appearance a little bit, so it look nice to me. I also wanted to customize it even more, to enable it to sync command history between all open terminal sessions. I have tried

and some combinations of them in my .bash_profile, but none of them worked for me properly, so I gave up the idea of syncing. Problems include wrong command history number, failing to remove duplicate command, etc.

Here is the current settings in .bash_profile I’m happy with.

  • To append command to the history file when the shell exits, rather than overwriting the history file (see shopt Man Page for more information) :

  • To delete consecutive duplicate command and remove duplicate command in history file:

  • But the above setting only works in memory. When terminal exits, the duplicate commands are still written to history file, so to delete duplicate commands every time terminal opens:


update Jun 24, 2017:

This is still not a perfect solution, as I want to show the git branch that is checked-out when in a git repo, after I made some changes to my PS1 variable, I issued a command

to make the change effective right now. This erases all the history from the current terminal session, as all the commands I issue in the current terminal session resides only in the memory, not written to the history file before I quit the session, so I lost all them. I should have saved current commands to the history file first by

So be careful to source any config file!!!

update Jun 25, 2017:

After a little more exploring, I’m able to customize more. These are the resources I’m referencing, 1. Customize Your Shell & Command Prompt, 2. What color codes can I use in my PS1 prompt?

My current PS1 variable is set as

And this is how it looks like

According to reference 1, Mac uses the following template to colorize the prompt:

To make it clear, here is a color chart from reference 2:

Back to my PS1 environment variable, \[\033[36m\]\u  means to display the current username with the color #36. As pointed in goldilocks’ answer, in linux system, 38;5; denotes the color be used for the foreground and 48;5; for the background. I tried it on the Mac, and it’s used the same way. So  [\033[38;5;167m\]@  means an @ sign with the color #167 as its foreground. \[\033[32m\]\h:  means hostname followed by a colon in the color #32. \[\033[38;5;221m\]\w  means the working directory in the color #221 for foreground.  \[\033[38;5;209m\]\$(__git_ps1 '(->%s)')  is actually for git, it prints the current branch, with the color #209. As it uses git command, this line must be placed in a latter order than source git-completion.bash. Then \[\033[38;5;104m\]\$  means a dollar sign with a color #104. At the end is [\033[38;5;188m\] , which add a space, and sets the command text being typed the color #188.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.