Linux source command (shell function library)
The source command is used to read a library file with various functions to the shell in a script file or the command prompt. It searches for library files located in the DIRECTORIES of the PATH variable or a specific file.
Examples:
In the example below, we will create a file called minhabiblioteca.sh with the following content:
eh_o_root () {
[$ (id -u) -eq 0] && return $TRUE || return $FALSE
} This file contains a function called “eh_o_root” that returns true if the logged-in user’s uid equals zero (root) or false when it returns another value.
Now let’s create a script called meuscript.sh that will use minhabiblioteca.sh as a function source:
Now let’s change the permission of meuscript.sh to executable:
When running meuscript.sh:
The source command is very useful for reading a library of functions or variables that can be used by various scripts from shell.
Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it?
Share