# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH"
fiCan anyone explain the contents of the ~/.profile file? So when you enter into the ~/.profile file what does all the writing mean?
1 Answer
Simplified version:
if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi
fiThis parts checks wheter ~/.profile itself is being sourced by a Bash instance, and if that's the case sources in turn ~/.bashrc; this is a way to include the user's settings stored in ~/.bashrc e.g. also in login shells, which normally don't source ~/.bashrc;
if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH"
fiThis part checks whether ~/bin exists, and if that's the case prepends ~/bin to the current value of $PATH; this is done so that potential executables / scripts present in ~/bin take precedence over executables / scripts present in other paths included in $PATH (e.g. by placing an executable named cat in ~/bin, when running cat that executable would be run in place of the usual /bin/cat).
Detailed version:
if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi
fiThis part checks whether the expansion of $BASH_VERSION has a non-zero lenght (if [ -n "$BASH_VERSION" ]), and if that's the case, if the expansion of $HOME/.bashrc exists and is a regular file (if [ -f "$HOME/.bashrc" ]), the expansion of $HOME/.bashrc is sourced.
Since Bash sets $BASH_VERSION upon invocation, checking whether $BASH_VERSION has a non-zero lenght is a robust way of determining whether the file itself is being sourced by a Bash instance.
This is why when invoking Bash as a login shell on Ubuntu the user's settings stored in ~/.bashrc are included (this is not necessarily the case for other distributions); Bash itself only sources ~/.profile when invoked as a login shell, and this is a way to go around that;
if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH"
fiThis part checks whether the expansion of $HOME/bin exists and is a directory (if [ -d "$HOME/bin" ]), and if that's the case prepends the expansion of $HOME/bin to the current value of $PATH (PATH="$HOME/bin:$PATH"; $HOME is normally set to the user's home directory).
This is done so that potential executables / scripts present in the expansion of $HOME/bin take precedence over executables / scripts present in other paths included in $PATH.