Posts Tagged “.bashrc”

If you do use Terminal.app more then once a week it makes sense to customize your Terminal to your needs.

Personaly i prefer bash so the next steps are focused on the bash shell.

1. Creating a .bashrc:

  • Open Terminal.app
  • Open Terminal Preference Dialog
  • Navigate to the Startup section
  • Make sure you have selected “command (complete path)” under “Shell open with” section. The Full path should be “/bin/bash” in our example
  • Close the Preference dialog
  • Navigate to your users home folder if you aren’t there already. This is usualy done with: “cd ~”
  • Enter: “touch .bashrc” to create a new empty file called .bashrc

Now we have done the first steps. We are sure we do use bash and we have an empty .bashrc ….so no function till now

2. Creating a .bash_profile:

Now we have to create a .bash_profile in our Users home directory too.

  • Open Terminal.app (be sure that you are in your users home directory)
  • Enter the following to create a bash profile: touch .bash_profile
  • Open the new bash profile. Enter: vi .bash_profile
  • Fill the new bash profile with the following:

if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi

3. Forcing Terminal.app to load our bashrc at start:

Now we have to make sure Terminal.app uses our new .bashrc while starting.

  • Open Terminal.app
  • Open Terminal Preference Dialog
  • Navigate to the Startup section
  • Change “/bin/bash” to “/bin/bash –rcfile /Users/yourusername/.bashrc

On the next start of Terminal.app it should use our empty .bashrc file. So now lets fill it a bit

4. Define an example alias:

  • Open Terminal.app
  • Open your .bashrc with a texteditor (ie: vi .bashrc)
  • Insert the following example alias to check if our .bashrc works: alias ls=”ls -la”
  • Save and close the file
  • Restart Terminal.app

After relauching Terminal.app the “ls” command should output the same in terminal then “ls -la” does

5. Tweak the history of your shell:

I love my bash history, but why not tweaking its default behaviour ?

  • Open Terminal.app
  • Open your .bashrc with a texteditor (ie: vi .bashrc)
  • Enter the following lines to your .bashrc:

# history handling
#
# Erase duplicates
export HISTCONTROL=erasedups
# resize history size
export HISTSIZE=5000
# append to bash_history if Terminal.app quits
shopt -s histappend

After restarting your Terminal your history should we teaked as follows.

  • The first command deletes duplicate-commands out of your .bash_history file which contains your bash history.
  • The second command defined the size of your history
  • The third line appends the history to your .bash_history to make sure you have all sessions saved.

Well that was the basic first step. You could now continue adding colors to your .bashrc, configuring how the prompt will look like and many other nice things.

Here my final example .bashrc

# Define how Bash prompt looks like:
#
# User @ Host - working dir
#export PS1=”\u@\h\w$ ”
export PS1=”\u@\h\w: ”

# Cli Colors
export CLICOLOR=1
# use yellow for dir’s
export LSCOLORS=dxfxcxdxbxegedabagacad

# Alias
#
#
alias ls=’ls -la’

# history handling
#
# Erase duplicates
export HISTCONTROL=erasedups
# resize history size
export HISTSIZE=10000
# append to bash_history if Terminal.app quits
shopt -s histappend

Comments 6 Comments »

1