Source in Linux

The command shell plays a very important role in the operating system Linux. It is used not only users work in the terminal, but also programs and operating system components to communicate with each other. For this purpose, environment variables. To reload the environment variables from a file is often used source.

This command allows you to execute the script in the current process of the bash shell. The default for each script starts a separate bash shell that holds all of its variables and functions. After the script completes all of this is removed together with the shell. Command source lets you run a script in the current command interpreter, and this means that all variables and functions added in this script will be available also in the envelope after its completion. As you already understood, this article will be considered source linux.

Team source linux

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

$ source the filepath arguments

Any options no longer need. If an absolute path to the file, just the file name, then the utility will look for the executable file in the current directory and the directories specified in the variable PATH. Let’s look at some examples of using this utility. Create a script that declares a variable:

vi losstsource

WEBSITE=losst

Then load the variable from that file:

source losstsource

Now you can try to output the variable and make sure that everything works:

echo $WEBSITE

However, the variable exists only in the current command interpreter in the other shells it is not. This difference commands from a source command export allows you to export the environment variables globally.

If you are running the script you need to pass parameters, you can do it, just listing them after the path to the script file. Let’s modify our script so that variable was taken from the first parameter:

vi losstsource

WEBSITE=$1

And again executed:

source losstsource losst.ru

Are similar function. If you declare a function in a bash script and then execute it with the command source linux, the function will be available in the interpreter:

vi testsource

#!/bin/bash
print_site(){
echo "losst.ru"
}

You can now perform the function print_site in the terminal or any other script:

print_site

For those who are familiar with programming in the C language, you can draw an analogy with the #include Directive, making available in the current file functions from other files. 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 will end on:

source losstanything

Instead of source you can use the dot (.) but be careful — between the point and the name of the file should be a space to bash interpreted this point as a separate command, not as part of the name of the file:

. losstsource

However, it is impossible to write .losstsource or ./losstsourcebecause marking ./ — this is a reference to the current directory, the script will be executed as usual.

Insights

In this small article we reviewed the work with source linux. As you can see, it is very simple and at the same time useful command, very greatly facilitate the work in the terminal. With its help, working of virtual environments Python and many other subsystems.

Source: losst.ru

(Visited 4 times, 1 visits today)