Until now, to perform a task or set anything for a given shell, we had to execute the needed commands in a shell. One of the main limitations to this approach is that the same configuration won't be available in a new shell. In a lot of cases, a user may want that whenever he or she launches a new shell, whereas instead a new customized configuration on top of the default configuration is available for use. For customizing bash, three files are available in a user's home directory that get executed by default whenever a user launches a new bash. These files are bashrc
, .bash_profile
, and .bash_logout
.
In a graphical system, mostly a non-login shell is used by a user. To run a non-login shell, we don't need the login credentials. Starting a shell in a graphical system provides a non-login shell. When a bash is invoked in non-login mode, the ~/.bashrc
file is invoked and the configuration available in it is executed and applied in any bash shell being launched. Settings that are needed in both the login and non-login shell are kept in the ~/.bashrc
file.
For example, on a Fedora 22 system default, the ~/.bashrc
file looks as follows:
# .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User specific aliases and functions
Any addition done in ~/.bashrc
will be reflected only to the current user's bash shell. We can see that the .bashrc
file also checks whether the etc/bashrc
file is available. If available, that gets executed too. The /etc/bashrc
file contains configuration applied to a bash shell for all users—that is, systemwide. Sysadmin can modify the /etc/bashrc
file if any configuration needs to be applied to all users' bash shells.
The file /etc/bashrc
also looks into the script files available in /etc/profile.d
, which can be confirmed by the following code snippet taken from the /etc/bashrc
file:
for i in /etc/profile.d/*.sh; do if [ -r "$i" ]; then if [ "$PS1" ]; then . "$i"
The following example shows a modified .bashrc
file. Name this file custom_bashrc
:
# custom_bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User added settings # Adding aliases alias rm='rm -i' # Prompt before every removal alias cp='cp -i' # Prompts before overwrite alias df='df -h' # Prints size in human readable format alias ll='ls -l' # Long listing of file # Exporting environment variables # Setting and exporting LD_LIBRARY_PATH variable export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/libs # Setting number of commands saved in history file to 10000 export HISTFILESIZE=10000 # Defining functions # Function to calculate size of current directory function current_directory_size() { echo -n "Current directory is $PWD with total used space " du -chs $PWD 2> /dev/null | grep total | cut -f1 }
The LD_LIBRARY_PATH
environment variable is used to give the runtime shared library loader (ld.so
) an extra set of directories to look for when searching for shared libraries. You can learn more about the shared library at http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html.
Make a backup of your original ~/.bashrc
file before modifying it:
$ cp ~/.bashrc ~/.bashrc.bak
Now, copy the custom_bashrc
file to ~/.bashrc
:
$ cp custom_bashrc ~/.bashrc
To apply modified settings, open a new bash shell. To apply a new .bashrc
in the same bash shell, you can source into a new ~/.bashrc
file:
$ source ~/.bashrc
We can check whether the new settings are available or not:
$ ll /home # Using alias ll which we created
total 24 drwx------. 2 root root 16384 Jun 11 00:46 lost+found drwx--x---+ 41 foo foo 4096 Aug 3 12:57 foo
$ alias # To view aliases
alias cp='cp -i' alias df='df -h' alias ll='ls -l' alias ls='ls --color=auto' alias rm='rm -i' alias vi='vim'
The alias
command displays aliases that we added in .bashrc
—that is, rm
, cp
, df
, and ll
.
Now, call the current_directory_size()
function that we added in .bashrc
:
$ cd ~ # cd to user's home directory $ current_directory_size Current directory is /home/foo with total used space 97G $ cd /tmp $ current_directory_size Current directory is /tmp with total used space 48K
Make sure to move back the original .bashrc
file whose backup we created at the beginning of this example, and source into it to get the settings reflected in the current shell session. This is required if you don't want any of the configuration changes that we did while playing out the preceding example:
$ mv ~/.bashrc.bak ~/.bashrc $ source ~/.bashrc
In a non-graphical system, after a successful login, the user gets a shell. Such a shell is called a login shell. When a bash is invoked as a login shell, first the /etc/profile
file gets executed; this runs the script available in /etc/profile.d/
as well. The following code snippet taken from /etc/profile
also mentions this:
for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else
These are global settings applied to any user's login shell. Furthermore, ~/.bash_profile
gets executed for a login shell. On a Fedora 22 system, the default content of the ~/.bash_profile
file looks as follows:
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH
From the contents, we can see that it looks for the .bashrc
file in a user's home directory. If the .bashrc
file is available in a home directory, it gets executed. We also know that the ~/.bashrc
file executes the /etc/bashrc
file as well. Next, we see that .bash_profile
appends the PATH
variable with the $HOME/.local/bin
and $HOME/bin
values. Furthermore, the modified PATH
variable is exported as an environment variable.
A user can modify the ~/.bash_profile
file as per his/her customized configuration needs, such as default shell, editor for login shell, and so on.
The following example contains a modified configuration in .bash_profile
. We will use bash_profile
as its filename:
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH # Added configuration by us # Setting user's default editor EDITOR=/usr/bin/vim # Show a welcome message to user with some useful information echo "Welcome 'whoami'" echo "You are using $SHELL as your shell" echo "You are running 'uname ' release 'uname -r'" echo "The machine architecture is 'uname -m'" echo "$EDITOR will be used as default editor" echo "Have a great time here!"
Changes are made after the Added configuration by us comment. Before we apply this new configuration to ~/.bash_profile
, we will first make a backup of the original file. This will help us in restoring the original content of the .bash_profile
file:
$ cp ~/.bash_profile ~/.bash_profile.bak
A new file .bash_profile.bak
will be created in the home
directory. Now, we will copy our new configuration to ~/.bash_profile
:
$ cp bash_profile ~/.bash_profile
To see the reflected changes in a login shell, we can either login as a non-graphical interface or just perform ssh
into the same machine to run a login shell. SSH (Secure Shell) is a cryptographic network protocol for initiating text-based shell sessions on remote machines in a secure way. In UNIX and Linux-based systems, SSH to a local or remote machine can be done using the ssh
command. The man
page of ssh
(man ssh
) shows all the capabilities provided by it. To do a remote login on the same machine, we can run ssh username@localhost
:
$ ssh foo@localhost # foo is the username of user
Last login: Sun Aug 2 20:47:46 2015 from 127.0.0.1 Welcome foo You are using /bin/bash as your shell You are running Linux release 4.1.3-200.fc22.x86_64 The machine architecture is x86_64 /usr/bin/vim will be used as default editor Have a great time here!
We can see that all the details added by us are printed in a login shell. Another way to quickly test our new .bash_profile
is by doing source to it:
$ source ~/.bash_profile
Welcome foo You are using /bin/bash as your shell You are running Linux release 4.1.3-200.fc22.x86_64 The machine architecture is x86_64 /usr/bin/vim will be used as default editor Have a great time here!
To reset changes done in the ~/.bash_profile
file, copy from the ~/.bash_profile.bak
file that we created at the beginning of this example and source into it to get the changes reflected in the current shell:
$ mv ~/.bash_profile.bak ~/.bash_profile $ source ~/.bash_profile
The .bash_logout
file present in a user's home directory gets executed every time a login shell exits. This is useful when a user has logged in remotely or has a non-graphical interface. A user can add clean-up tasks to be performed before he/she logs off from a system. A clean-up task may include removing the temporary files created, clearing environment variables, logging off important data, archiving or encrypting certain tasks, uploading onto the Web, and so on.
18.227.102.111