User Tools

Site Tools


bash:functions:local_vs_global_functions

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
bash/functions/local_vs_global_functions.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki