Introduction
In Linux systems, source
is a built-in shell command that reads and executes the file content in the current shell. These files usually contain a list of commands delivered to the TCL interpreter to be read and run.
This tutorial will explain how the source command works and when to use it.
Prerequisites
- A system running a Linux distribution (learn how to install Ubuntu 20.04, how to install CentOS 7, or how to install Arch Linux)
- An account with sudo privileges
- Access to the terminal window/command line
Source Command Syntax
The source command uses the following syntax:
source [filename] [arguments]
Where:
[filename]
: The name or path to the file you want the source command to execute.[arguments]
: Any arguments you provide become positional parameters when the file is executed.
Note: If you don’t provide the full path to the file you want to execute, source
will search the $PATH
variable for the file. If it does not find the file there, source
will search the current directory.
The dot (period) character can be used in place of the source
command, resulting in the same output:
. [filename] [arguments]
Linux Source Command Examples
Here are some of the ways you can use the source
command:
Pass Arguments
Create a text file called example.txt in the Home directory with the following content:
pwd
date
time
Use the source
command to pass the content of this file as an argument:
source example.txt
The output shows that the source
command moves line by line, executing any commands listed in example.txt.
For a more complex example, change the content of example.txt to:
echo "The current directory is:"
pwd
echo "Today's date is:"
date
echo "The time is:"
time
Move the file to Home/source_command/example. With the source
command, pass the content of the file as an argument using the full path to the file:
source source_command/example/example.txt
Note: The source
command also allows you to run scripts in the current shell environment, unlike the bash
command, which creates a new shell environment.
Read the Configuration File
The source
command also allows you to read variables from a file. Start by creating an example configuration file example_config.sh in the Home directory and adding the following content:
VAR1="a"
VAR2="b"
VAR3="c"
Create a bash script called example_bash.sh and add the following:
#!/usr/bin/env bash
source example_config.sh
echo "VAR1 is $VAR1"
echo "VAR2 is $VAR2"
echo "VAR3 is $VAR3"
The source
command allows example_bash.sh to read the variables VAR1
, VAR2
, and VAR3
you defined in example_config.sh.
Run the example_bash.sh script using the source
command:
source example_bash.sh
Source Functions
If you have functions you are using in several different scripts, you can save them as separate files and use the source
command to refer to them when writing scripts.
For instance, start by creating a function check_root.sh that checks whether the user running the script is a root user:
check_root() {
if [[ $EUID -ne 0 ]]; then
echo "You must run this script as root"
exit 1
fi
}
Create a script called example_script.sh and use the source
command to insert the check_root.sh function:
#!/usr/bin/env bash
source check_root.sh
check_root
echo "This is the root user"
Running this script as a non-root user produces "You must run this script as root"
as the output and exits the script:
bash example_script.sh
Running the script as a root user shows "This is the root user"
as the output:
sudo bash example_script.sh
Note: Using the source
command to run example_script.sh executes the script in the current shell environment. Since you cannot run the source
command as the root user, starting the script causes the terminal window to close. Use the bash
command to execute the script instead.
Refresh the Current Shell Environment
For this example, we are creating an alias command ll
:
alias ll = 'ls -l'
This command lists the files in the current directory using the extended format:
However, this command only works in the current shell session. To make it permanent, open the bashrc file with:
sudo nano ~/.bashrc
Under the #some more ls aliases
section, add the following:
alias ll = 'ls -l'
Refresh the current shell environment with the source
command:
source ~/.bashrc
Conclusion
After reading this tutorial, you should know how to use the source command in Linux to run multiple commands from a single file.
For a more comprehensive overview of useful commands, check out our Linux commands cheat sheet.
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/224405.html