The shell plays a very important role in the operation of the Linux family of operating systems. It is used not only by users to work in the terminal, but also by programs and operating system components for exchanging data with each other. For this, environment variables are used. The source command is often used to reload environment variables from a file.

This command allows you to execute a script in the current bash shell process. By default, a separate bash shell is launched to execute each script, storing all its variables and functions. After the script ends, all this is removed along with the shell. The source command allows you to execute a script in the current shell, which means that all variables and functions added in this script will also be available in the shell after it ends. As you already understood, this article will cover the source linux command.

The source linux command

The command syntax is very simple. You need to call the command itself and pass it the path to the executable file:

$ source file_path arguments

No more options are needed. If not an absolute path to a file, but just a file name, the utility will look for an executable file in the current folder and directories specified in the PATH variable . Let’s look at a few examples of working with the utility. Create a script that declares a variable:

vi linuxinfsource

linuxinfWEBSITE=losst

Then load the variable from this file:

source linuxinfsource

Now you can try to display the contents of the variable and make sure everything works:

echo $WEBSITE

source Linux Command

However, the variable exists only in the current shell, it does not exist in other shells. This is the difference between the source command and the export command, which allows you to export environment variables globally.

If the script you are executing needs to pass parameters, you can do so by simply listing them after the path to the script file. We modify our script so that the variable is taken from the first parameter:

vi linuxinfsource

WEBSITE=$1

And we execute again:

source losstsource linuxinf.com

source Linux Command

Functions work the same way. If you declare a function in a bash script and then execute it with the source linux command, the function becomes available in the interpreter:

vi testsource

#!/bin/bash
print_site(){
echo "linuxinf.com"
}

Now you can execute the print_site function in the terminal or any other script:

print_site

source Linux Command

For those who are familiar with C programming, we can draw an analogy with the #include directive, which makes functions from other files available in the current file. If the file whose name is passed as a parameter to the command does not exist, it will return a return code of 1 and exit:

source linuxinfanything

source Linux Command

You can use a dot (.) instead of the source command, but be careful here – there must be a space between the dot and the filename so that bash interprets this dot as a separate command and not as part of the filename:

. linuxinfsource

source Linux Command

However, you cannot write .linuxinfsource or ./linuxinfsource , because the ./ notation is already a reference to the current directory, the script will be executed as usual.

Findings

In this short article, we looked at working with the source linux command. As you can see, this is a very simple and at the same time useful command that greatly simplifies the work in the terminal. It is with its help that Python virtual environments and many other subsystems work.

Source: https://losst.ru/. The article is distributed under the CC-BY-SA license