Table of Contents
BASH - Functions - Local vs Global Functions
Functions can be global, i.e. apply by default to every user on the system.
However each user can also have their own functions which only apply to them, i.e. local functions.
Global Functions
Global functions are specified in a file within the /etc/profile.d/ directory. The name of the file is irrelevant.
We use /etc/profile.d/bash_functions.sh.
NOTE: The permissions of the file should have read permissions.
For example 644.
Local Functions
Local functions are specific to the user.
There is no specific file set aside to hold local functions, but it is recommended to define functions should in a file named .bash_functions within a users home directory.
- ~/.bash_functions
Enable the functions
Have your .bashrc load it, by adding the following to the ./bashrc file:
- ~/.bashrc
if [ -f ~/.bash_functions ]; then . ~/.bash_functions fi
or
- ~/.bashrc
if [ -e $HOME/.bash_functions ]; then source $HOME/.bash_functions fi
To apply the changes immediately to your bash profile without having to log out:
source ~/.bashrc
or
~/.bash_functions
or
. ~/.bashrc
or
. ~/.bash_profile