Skip to main content
Engineering LibreTexts

02-E.15: Example: Bash RC file

  • Page ID
    26817
  • EXAM OBJECTIVES COVERED
    2.2 Given a scenario, manage users and groups.

    The ~/.bashrc file

    This file allows the user to modify their environment without impacting other users on the system. Every user's home directory has a .bashrc file - that is why it is often referred to with the tilde: ~/.bashrc. Users can edit this file and add their own customizations, even overriding system settings that are set in the /etc/bash.bashrc file.

    # ~/.bashrc: executed by bash(1) for non-login shells.
    
    # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
    HISTSIZE=1000
    HISTFILESIZE=2000
    
    # some more ls aliases
    alias ll='ls -alF'
    alias la='ls -A'
    alias l='ls -CF'
    
    # Alias definitions.
    # You may want to put all your additions into a separate file like
    # ~/.bash_aliases, instead of adding them here directly.
    # See /usr/share/doc/bash-doc/examples in the bash-doc package.
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    
    export PATH="$HOME/scripts:$PATH"
    export EDITOR=/usr/bin/vi

    Looking at the pieces:

    Some environment variables are set:

    • HISTSIZE is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.
    • HISTFILESIZE is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of your bash session for use in future sessions.

    There are 3 aliases set. Aliases allow users to customize commands to save on typing. In this file there is a command the user creates called ll which actually executes as ls -alF . A user can basically create their own commands based on existing Linux commands and the command options.

    An if statement behaves just like an if statement in any programming language. This particular if statement says, 'if the file ~/.bash_aliases exists then we execute that file.' If it does NOT exist, nothing happens.

    The last two entries are setting environment variables to a new value. The first one is adding the directory /home/pbmac/scripts to the PATH variable, which is the list of directories searched when I issue a command. This allows me to create my own customer scripts to run and be able to run them easily from the command line. The other export is for the EDITOR variable - which is used by the system when I do things like edit the password file. This value tells the system I want to use the vi editor.

    The ~/bash_profile

    This file is slightly different from the ~/.bashrc in that it is only executed when the user initially logs into the system. This file is NOT always present, in which case the ~/.bashrc file is always used.

    What is Difference Between ~/.bashrc and ~/.bash_profile

    The .bash_profile is read and executed when Bash is invoked as an interactive login shell (meaning when the user logs into the system from the login prompt), while the .bashrc is executed for an interactive non-login shell, such as when the user runs a script that they have written.

    A good rule of thumb then is to use .bash_profile to run commands that should run only once, such as customizing the $PATH environment variable.

    Put the commands that should run every time the user launches a new shell in the .bashrc file. This would include a user's aliases, customizations to the prompt, history customizations, etc.

    Typically ~/.bash_profile checks for the existence of the .~/.bashrc file, and if it is present it runs it. We saw this in the example above with the if statement and the .bash_aliases file.

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

    Most Linux distributions are using the ~/.profile instead of ~/.bash_profile because the ~/.profile is read by all shells, while ~/.bash_profile is only used by the Bash shell.

    • Was this article helpful?