Categories: How-To

Linux source command (shell function library) [Basic Guide]

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:

$ source funcoes.sh $ source /caminho/ate/diretório/funcoes.sh arg1 arg2

In the example below, we will create a file called minhabiblioteca.sh with the following content:

#! /bin/bash

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:

#! /bin/bash 
# Let's read the minhabiblioteca.sh with the command
source minhabiblioteca.sh
eh_o_root && echo “You're the Root.” || echo “You're an ordinary user.”

Now let’s change the permission of meuscript.sh to executable:

$ chmod +x meuscript.sh

When running meuscript.sh:

$. /meuscript.sh 
You are an ordinary user.

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

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.