• Blogs (9)
    • đŸ“± 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • Linux: .bash_profile vs. .bash_rc

    Blogs20112011-01-24


    Here I summary 2 basic Linux files which initializes user command-line environment. They are .bash_profile and .bash_rc (Suppose default shell is /bin/bash).

    .bash_profile and .bash_rc are hidden in each user’s $HOME/ directory. To check it:

    $ cd $HOME
    $ ls -la .bash_profile .bash_rc

    with $HOME/.exrc and other hidden files in $HOME dir (start with .), they build the user’s basic command-line environment: path, alias, term attributes, initial scripts etc. e.g., I use ‘set -o vi’ (in .bashrc) or ‘export EDITOR=vi’ (in .bash_profile) for quick retrieve, and a lot of ‘alias ’ to simplify operation.

    To distinguish the 2 files is easy:

    • .bash_profile is executed automatically when user login, When we login (type username and password) via console, or via ssh, .bash_profile is executed to configure command-line env, setup path before the initial command prompt.
    • After login, when running scripts or opening new term, .bashrc is executed automatically. If we’ve already logged into Linux and open a new terminal window (xterm) inside Gnome or KDE or via term like vt220, then .bashrc is executed before the window command prompt.

      Or, when a script with shebang at the first line of the script, it hides to call .bashrc to initialize a new env for script to running.

      #! /bin/bash

    Why two different files?

    Say, you’d like to print some lengthy diagnostic information about your machine each time you login (load average, memory usage, current users, etc). You only want to see it on login, so you only want to place this in your .bash_profile. If you put it in your .bashrc, you’d see it every time you open a new terminal window.

    Most of the time you don’t want to maintain two separate config files for login and non-login shells — when you set a PATH, you want it to apply to both. You can fix this by sourcing .bashrc from your .bash_profile file, then putting PATH and common settings in .bashrc. To do this, add the following lines to .bash_profile:

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