Sourcing Scripts in Linux using source command
The source
command in Linux is a built-in shell command used to read and execute commands from a file.
This means that any variables or functions that are defined by the script will remain available after the script finishes executing.
Now, let’s start exploring the functionalities of the source
command.
- 1 Subshells and the Source Command
- 2 Sourcing a Script Without Parameters
- 3 Run commands after sourcing files
- 4 Allow sourcing .file even if dotglob disabled
- 5 Read and execute commands from stdin
- 6 Print a message before sourcing each file
- 7 Using Positional Parameters
- 8 Using Special Shell Variables
- 9 Using . (dot) as an alternative to source
- 10 Sourcing Scripts with Functions
- 11 Unsetting Variables and Functions
- 12 Safe Script Sourcing
Subshells and the Source Command
When you run a script in a shell, a subshell is often spawned which creates a separate process for the execution.
This allows isolation, encapsulating variables, and functions within the spawned shell so they do not interfere with the parent environment.
In case of the source command, the shell executes the script in the current shell itself.
So, any variable, function, or alteration made will affect the current environment.
Sourcing a Script Without Parameters
Let’s consider a simple script, script.sh
, with the following content:
#!/bin/bash var="Hello, World!"
We can source this script in the current shell with the source
command:
source script.sh
After running the command, the variable var
becomes available in the current shell. You can confirm this by running:
echo $var
This will output:
Hello, World!
This shows that the source
command has imported the variables from script.sh
into the current shell environment.
Run commands after sourcing files
The -c
or --command
option allows you to run a command in the current shell after sourcing the specified file.
For instance, given script.sh
as before, you can source it and then immediately run a command:
source -c "echo $var" script.sh
This will output:
Hello, World!
This command sources script.sh
, and then executes echo $var
in the current shell.
Allow sourcing .file even if dotglob disabled
The -d
or --dot
option allows sourcing files that start with a dot, even if the dotglob
option is disabled in your shell. Consider a file .file
:
#!/bin/bash dot_var="Hidden message!"
You can source this file as follows:
source -d .file
Then, echo $dot_var
will output:
Hidden message!
Read and execute commands from stdin
The -s
or --stdin
option allows you to read and execute commands from the standard input (stdin). You can use it as follows:
echo "stdin_var='Reading from stdin'" | source -s
Then, echo $stdin_var
will output:
Reading from stdin
This shows that -s
allows us to source content passed to it through a pipe.
Print a message before sourcing each file
The -v
or --verbose
option prints a message before sourcing each file. This can be helpful when debugging complex scripts. Consider again script.sh
from earlier, if we use:
source -v script.sh
This will output:
source script.sh Hello, World!
It provides a message displaying the name of the file it’s sourcing. This allows you to track the execution of multiple sourced files.
Using Positional Parameters
Positional parameters can be used when sourcing a script to pass arguments to the script. Consider a script greet.sh
:
#!/bin/bash echo "Hello, $1!"
You can source this script with a positional parameter as follows:
source greet.sh World
This will output:
Hello, World!
This shows that positional parameters can be passed to scripts when they are sourced, similar to how they are passed when scripts are run normally.
Using Special Shell Variables
Special shell variables can also be accessed from a sourced script. Let’s create special.sh
:
#!/bin/bash echo "Number of parameters: $#" echo "All parameters: $@" echo "PID of shell: $$"
Now, source special.sh
:
source special.sh Hello World
This will output:
Number of parameters: 2 All parameters: Hello World PID of shell: 1260
The special shell variables $#
, $@
, and $$
were accessed from the sourced script and have the usual meanings: the number of parameters, all parameters, and the PID of the shell, respectively.
Using . (dot) as an alternative to source
The .
(dot) command is equivalent to source
in many shells. To illustrate, you can use it like this:
. script.sh
After running this command, echo $var
will output:
Hello, World!
The .
command provides a shorter alternative to source
, while offering the same functionality.
Difference between source and . in different shells
In Bourne-derived shells like Bash, source
and .
are interchangeable. However, in the C shell (csh) and its derivatives, only the source
command is available.
Moreover, some shells don’t recognize source
as a command and only accept .
.
So, it is generally safer to use .
for compatibility.
Sourcing Scripts with Functions
Just as with variables, functions defined in a sourced script are also available in the current shell. Consider functions.sh
:
#!/bin/bash function hello() { echo "Hello, function!" }
You can source functions.sh
and then call hello
:
source functions.sh hello
This will output:
Hello, function!
Unsetting Variables and Functions
After sourcing a script, you may want to unset variables or functions that were defined in that script.
You can do this with the unset
command. Consider unset_var
and hello
from earlier scripts:
unset persist_var unset -f hello
After running these commands, echo $persist_var
and hello
will return nothing, indicating that the variable and function have been unset.
Safe Script Sourcing
When sourcing scripts, it’s important to consider that the script will be executed with the current shell’s permissions.
This means that the script could potentially do a lot of damage if it contains harmful commands. Always review a script’s content before sourcing it.
You should only source scripts from trusted sources.
If you’re unsure about a script, you can run it in a subshell instead, where it cannot affect the parent shell’s environment:
bash script.sh
This runs the script in a new shell, which will not affect the current shell’s environment.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.