- Beginners Guide for Source Command in Linux
- Tutorial Details
- Syntax of the Source Command
- Evaluate the Contents of a Given File
- Specifying the Path of a Given File
- Reading the Bash Configuration File
- Sourcing Functions
- Linux source Command
- What is Linux source command?
- Syntax of Linux source command
- Using Linux source command
- Using Linux source command with functions
- Using Linux source command with variables
- Using Linux source command to modify PATH variable
- Additional Subheadings and Examples
- Using Linux source command to source bash aliases
- Using Linux source command in shell scripts
- Using Linux source command in virtual environments
- Using Linux source command with Git
- Conclusion
Beginners Guide for Source Command in Linux
The source is a built-in command-line tool that reads and executes the commands from the specified file (in order) as its argument in the current shell.
Each command from the specified file will be delivered to the TCL interpreter to be read and run, making it useful to load functions, variables, and configuration files.
Even when you modify the bash configuration (“~/.bashrc“) file, it is recommended to load the changes in the current shell environment using the source command due to its nature of running the script in the current shell environment instead of creating a new shell environment as the bash command does.
Tutorial Details
Description | Source |
Difficulty Level | Low |
Root or Sudo Privileges | No |
Host System and Architecture | Ubuntu 22.10 (x64) |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | source |
Internet Required | No |
Discussed Tools in this Article |
Syntax of the Source Command
The source command takes two arguments: one is the filename (name or path of the file), and the other is the arguments (it will become the positional parameter for the file).
Note that the “.” (period) is a synonym for the source command, so both work the same way.
$ source [FILENAME] [ARGUMENTS] $ . [FILENAME] [ARGUMENTS]
And do not confuse between “.” (period) and “./” (relative positioning), because both are different.
- The “./script.sh” will run the script using bash by creating a new shell environment.
- Whereas, “source script.sh” or “. script.sh” will read and execute the commands from the specified script or file into the current shell environment.
Evaluate the Contents of a Given File
Create a new text file with the name “commands.txt” in your current working directory and add the following commands to it.
ls whomi pwd
Use the file path as a source command argument.
$ source commands.txt commands.txt Documents Music Public Templates Desktop Downloads Pictures snap Videos linuxtldr /home/linuxtldr
As you can see, the source command will read and execute the content of this file line by line in the current shell environment.
Specifying the Path of a Given File
In the previous example, the source command was able to read and execute the content of the file even without specifying the file’s absolute or relative path.
It is because if you do not specify them, the source command will search the “$PATH” environment variable for files. If it does not exist, source will search for the file in the current directory.
To demonstrate, I have renamed the previous file from “commands.txt” to “file” name (there is already a command with file name) as shown.
$ mv commands.txt file
Now, if you specify the “file” argument to the source command without specifying the absolute or relative path, it will search the “$PATH” variable for the specified file.
$ source file bash: source: /usr/bin/file: cannot execute binary file
As you can see above, there is a binary file with the name “file” in the “$PATH” variable that source found and tried to read and execute, but the file was a binary file, so source failed to read it.
To prevent this issue, you can specify the absolute or relative path for the specified file.
Example of an absolute path:
$ source /home/linuxtldr/file Desktop Downloads Music Public Templates Documents file Pictures snap Videos linuxtldr /home/linuxtldr
Example of a relative path:
$ source ./file Desktop Downloads Music Public Templates Documents file Pictures snap Videos linuxtldr /home/linuxtldr
Reading the Bash Configuration File
Using the source command, you can read variables from another file and load them into your existing file. To do that, you must declare the variables using the Bash “KEY=VALUE” syntax.
Create a text file in your current directory and copy the following content into it, then save the file with any name (ex: “env“).
VAR1="value one" VAR2="value two" VAR3="value three"
Now, create a bash script in the same directory with the name “script.sh” and add the following content.
#!/usr/bin/env bash source ./env echo "VAR1 is $VAR1" echo "VAR2 is $VAR2" echo "VAR3 is $VAR3"
If you run the “script.sh” file using the source command, it will read the “VAR1“, “VAR2“, and “VAR3” variables defined in the “env” file and execute the specified commands in your current shell environment as shown.
$ source script.sh VAR1 is value one VAR2 is value two VAR3 is value three
Sourcing Functions
If you have a block of commands or functions that you are using in multiple scripts, you can save them in a separate file and use the source command to refer to them when writing scripts.
For example, you can use the following “check_root” function to check whether the user running the script is a root user.
Copy and save the above functions in the “check_root.sh” file.
Than create a second script with the name “script.sh” and simply use the source command to source the “check_root.sh” function.
#!/usr/bin/env bash source ./check_root.sh check_root echo "I am root"
If you run the above script as a non-root user, it will print “You must run this script as root!” and exit.
Note : You can source the “check_root.sh” file into “script.sh” to load the function into the current shell environment. However, if you try to execute “script.sh” using the source command, your terminal window will close because source is not allowed to be run with sudo; instead, use the bash command.
$ bash script.sh You must run this script as root!
However, if you run your script as root or a sudo user, it will produce “I am root” and exit.
$ sudo bash script.sh I am root
The advantage of using this approach when creating scripts is that it will make the script smaller and more readable, reduce the repetition of codes, and make it easier to reflect changes in the file where the script is being used.
I hope this article makes it easier for you to understand the use of the source command.
If you have any questions regarding this topic, feel free to ask them in the comment section.
Linux source Command
The Linux source command is an essential tool for Linux users and administrators. It is used to execute a script file in current shell environment, and it allows you to modify current shell environment in same way that you would if you had typed commands manually. In this article, we will explore Linux source command and its various applications.
What is Linux source command?
The Linux source command is a shell command that reads and executes commands from a file in current shell environment. file is typically a shell script, but it can also be any text file containing a series of commands. source command is often used to set environment variables, define functions, and execute initialization scripts.
Syntax of Linux source command
The syntax of Linux source command is straightforward −
In this syntax, filename parameter represents name of file containing commands that you want to execute.
Using Linux source command
To use Linux source command, you must first create a script file containing commands that you want to execute. Once you have created script file, you can use source command to execute commands in file.
#!/bin/bash export MY_VAR="Hello World"
In this example, we’ve created a simple script file that defines an environment variable called MY_VAR and sets its value to «Hello World». To execute this script using source command, we would run following command −
This command would read contents of myscript.sh file and execute commands in current shell environment. After running this command, MY_VAR environment variable would be set to «Hello World».
The source command can also be used to execute initialization scripts. For example, many Linux systems include an initialization script that is executed when system boots up. You can use source command to execute this script manually if you need to −
This command would execute rc.local script located in /etc/init.d directory.
Using Linux source command with functions
The Linux source command is particularly useful when working with shell functions. Shell functions are a way to group a set of commands together and give them a name. You can then call function by its name to execute commands.
Here’s an example of a simple shell function −
In this example, we’ve defined a function called myfunction that simply echoes message «Hello World». To execute this function using source command, we would run following command −
source myscript.sh myfunction
This command would read contents of myscript.sh file and define myfunction function in current shell environment. We could then call function using its name to execute commands.
Using Linux source command with variables
The Linux source command can also be used to set and modify variables in current shell environment. For example, let’s say we have a script file called myvars.sh that defines two variables −
#!/bin/bash MY_VAR1="Hello" MY_VAR2="World"
To set these variables in current shell environment using source command, we would run following command −
After running this command, MY_VAR1 and MY_VAR2 variables would be defined in current shell environment, and we could access their values using $MY_VAR1 and $MY_VAR2 variables.
Using Linux source command to modify PATH variable
The Linux source command can be particularly useful when modifying PATH environment variable. PATH variable is used by shell to locate executable files, and it typically includes a list of directories separated by colons.
To modify PATH variable, you can create a script file that defines new PATH value and then use source command to execute script in current shell environment. Here’s an example of a script file that adds a directory to PATH −
#!/bin/bash export PATH="/my/new/directory:$PATH"
In this example, we’ve added /my/new/directory directory to beginning of PATH variable. To execute this script using source command, we would run following command −
After running this command, PATH variable would be updated to include new directory at beginning of list.
Additional Subheadings and Examples
Using Linux source command to source bash aliases
The source command can be used to source Bash aliases. An alias is a way to create a shorter name for a longer command, making it easier to type and remember. To use source command to source Bash aliases, you would create a Bash file with alias commands and then run source command on that file. Here’s an example −
# in your ~/.bash_aliases file: alias ll='ls -alF' # source ~/.bash_aliases file source ~/.bash_aliases
Now, when you run ll command in your terminal, it will be translated to ls -alF.
Using Linux source command in shell scripts
The source command can also be used in shell scripts to load environment variables and functions from other files. This can be useful when you have common functionality that you want to use across multiple scripts. Here’s an example −
# in your common_functions.sh file: my_function() < echo "Hello World!" ># in your script.sh file: source common_functions.sh my_function
In this example, source command loads my_function function from common_functions.sh, which is then called in script.sh.
Using Linux source command in virtual environments
The source command can be used in virtual environments to activate them. Virtual environments are isolated Python environments that can have their own installed packages and versions. source command can be used to activate virtual environment and load its dependencies. Here’s an example −
# create and activate a virtual environment python -m venv myenv source myenv/bin/activate
In this example, source command activates virtual environment created in myenv directory.
Using Linux source command with Git
The source command can be used with Git to load aliases and functions that can make working with Git easier. Git aliases are shortcuts for longer Git commands. Here’s an example of how to use source command with Git −
# in your ~/.gitconfig file: [alias] st = status co = checkout br = branch # source ~/.gitconfig file source ~/.gitconfig
In this example, source command loads Git aliases defined in ~/.gitconfig.
Conclusion
The Linux source command is a powerful tool that can be used to execute script files in current shell environment. It is particularly useful when working with shell functions and modifying environment variables such as PATH. By using source command, you can modify shell environment in same way that you would if you had typed commands manually. This can save you time and make your work more efficient.