Home Linux System AdministrationBasics Source Command on Linux Explained

Source Command on Linux Explained

by schkn

The source command on Linux is a pretty popular function run by system administrators daily.

But what is the function of the source command?

Used to refresh the current shell environment, the source command can also be used in order to import functions into other bash scripts or to run scripts into the current shell environment.

In today’s tutorial, we are going to see how the source command should be used on Linux.

The commands will be executed in a Debian 10 environment with GNOME, but they will work for every distribution.

Source command internals

Before starting, it is important to have a complete understanding of what environment and shell variables are.

By default, on Linux, your system already owns a couple of environment variables to store various information such as the shell to use, your hostname or your current username.

Environment variables are initialized by the system and are inherited by all processes on the system. As a consequence, when you are running a shell instance, you are to get the value of environment variables on your system.

$ echo $USER
devconnected

On the other hand, shell variables are variables declared within the context of a shell instance and they are not shared with other shells or with child processes.

$ VARIABLE=devconnected
$ echo $VARIABLE
devconnected

On Linux, when you execute a script, it is most likely executed in a subshell.

As a consequence, you won’t be able to have the variables defined in your first shell running in the script, they simply don’t share the same environment.

This is what the source command solves.

The source command is used in order to evaluate the file passed as an argument in the context of the current execution. Shortened as ‘.’, the source command is mostly used in the context of shells run in terminal windows.

$ source filename [arguments]

Note that when arguments are provided, they are set as positional parameters for the script specified.

Note : the source documentation is located inside the bash documentation. To read it, type “man bash” and search for the source command paragraph.

source command documentation on linux

Source to update your current shell environment (.bashrc)

One of the main reasons to use source is to refresh the current shell environment by running the bashrc file.

As a reminder, .bashrc is a script file executed whenever you launch an interactive shell instance.

It is defined on a per-user basis and it is located in your home directory.

Let’s say for example that you want to add a new alias to your shell environment.

Open your .bashrc file and a new entry to it.

alias ll='ls -l'

Now try to run your new alias command directly in the terminal.

alias command not found on linux

As you can see, the changes were not directly applied in your current environment.

For the changes to be applied, run the source command with the .bashrc file as an argument.

$ source ~/.bashrc

An alternative way to do it is to run it with the dot syntax

$ . ~/.bashrc

If you try to execute your alias again, you should be able to run it

Current shell environment refreshed on Linux

Source to execute a script in the current context

As explained before, the source command can be used to execute a script in the context of the current environment.

To illustrate this point, let’s define a local shell variable in your current shell environment.

$ VARIABLE=devconnected

Now, create a new script and paste the following content in it

#!/bin/bash

echo $VARIABLE
echo $LOGNAME

The VARIABLE variable represents the local shell variable we created before and the LOGNAME variable is an environment variable on your host.

Save your script and give execute permissions to your newly created script (you will need sudo privileges to execute this command)

$ sudo chmod ugo+x <script>

If you try to execute the script, you should see the following result

Executing a script without source

As you can see, the $VARIABLE local variable was not printed to the standard output.

This is because the script was executed in a subshell having its own set of local variables. The environment variable was printed however.

In order to execute this script in the context of our current shell, we are going to use the source command.

Executing a script with the source command on Linux

As you can see, in this case, the $VARIABLE variable was correctly printed to the standard output.

Source to import a script function

One of the great features of the source command is to update a script function, allowing a greater reusability of your existing shell functions.

To illustrate this point, let’s take the example of a function that prints the current user to the standard output.

Create a new script file and paste the following content inside.

$ nano script

#!/bin/bash
# Print the current user to the standard output
printUser() {
   echo $USER
}

Create another script and use the source function in order to import your printUser function.

$ nano import-script

#!/bin/bash
source ./script
printUser

Assign the correct permissions to the file you want to execute. You don’t need to have the execute permissions for the file containing the function you want to import.

$ sudo chmod u+x import-script
$ ./import-script

This is what you should see on your screen.

Importing a function with the source command on Linux

As you can see, the function was correctly imported and executed, printing the name of the current user of my host.

Source to read variables from a file

Another great usage of the source command on Linux is to read variables from a file.

Let’s say for example that you have a file with the following content in a file named “variables

NAME=devconnected
WEBSITE=devconnected.com
DESCRIPTION="best educational website online"

In another script file, you would be able to use those variables by using the source command to read variables.

Create a script and paste the following content in it.

$ nano variable-script

#!/bin/bash
source ./variables
echo $NAME is a website located at $WEBSITE and it is probably the $DESCRIPTION
Importing variables with the source command on Linux

Troubleshooting

In some cases, you may run into errors when trying to source your files using the source command.

Here is a list of the common errors when trying to source files.

Source command not found on Linux

In some cases, you may run into this error

$ source: command not found

Or using sudo, you might also have a source command not found error.

$ sudo: source: command not found

The easy fix here is to execute the command as the root user by using “su”.

$ sudo -s
$ source .bashrc

Note that you can also use the “dot syntax” which is an equivalent to the source command.

$ . .bashrc

Conclusion

In today’s tutorial, you learnt what is the source command on Linux and how you can use to execute scripts in the context of the current environment.

You also learnt that it can be used to import functions or variables from a simple file to a script file.

If you want more tutorials related to Linux system administration, we have a complete category dedicated to it on devconnected. Click on the image below to read them.

Until then, have fun, as always.

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.