2
# ~/.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"
fi

Can anyone explain the contents of the ~/.profile file? So when you enter into the ~/.profile file what does all the writing mean?

kos
  • 35,891
Jansen Roy
  • 49
  • 1
  • 1
  • 3
  • See also http://stefaanlippens.net/bashrc_and_others and http://www.linuxfromscratch.org/blfs/view/6.3/postlfs/profile.html and – Panther Mar 28 '16 at 13:20
  • 1
    @bodhi.zazen How is the linked question a duplicate of this question? They're asking to explain what ~/.profile does (not to mention ~/.profile != ~/.bash_profile). – kos Mar 28 '16 at 14:22

1 Answers1

2

Simplified version:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

This 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"
fi

This 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
fi

This 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"
fi

This 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.

Zanna
  • 70,465
kos
  • 35,891
  • Is there any you can simplify what you have written? I have only just started looking into Ubuntu – Jansen Roy Apr 05 '16 at 18:33
  • @JansenRoy I tried to simplify it a bit. Any better? If there's something specific you don't understand feel free to ask. – kos Apr 05 '16 at 19:01
  • Thank you for the help so far...but I do not understand most of the language used and also do not know much about bash and the profile file – Jansen Roy Apr 05 '16 at 21:01
  • @JansenRoy Perhaps you should start here: https://www.gnu.org/software/bash/manual/bashref.html. If you have more specific questions check if they've been asked before on the site and check the related answers, that should help; I'm happy to help with a specific concern, but it'd be too extensive (both for the comments section and for the chat) to address all the basics. If you have a specific question about ~/.profile you can drop by the chat and ping me there with @kos, I'm lurking there most of the times. – kos Apr 05 '16 at 21:19
  • if you don't know much about bash or profile, just leave the file alone. It has it's reason to be there. When starting to get apt with linux and bash you automatically acquire an understanding – xeruf May 30 '18 at 20:45