- What the source command is in Linux, how it works, and how to use it to execute commands from a file in the current shell.
- Why the source command is useful for refreshing environment variables and making changes to the current shell environment.
If you are a Linux user, you may have come across the source command, which is a handy utility that can be used to refresh environment variables, among other things. But what exactly does the source command do, and how can you use it effectively? In this blog post, we will explain what the source command is in Linux, how it works, and how to use it to execute commands from a file in the current shell.
Table of Contents
- What is the Source Command in Linux?
- How Does the Source Command Work?
- Why Use the Source Command?
- How to Use the Source Command Effectively?
- Frequently Asked Questions (FAQs)
- Question: What is the difference between source and execute in Linux?
- Question: How do I know if a file is sourced or executed in Linux?
- Question: How do I exit a sourced file in Linux?
- Summary
What is the Source Command in Linux?
The source command is a shell built-in command that executes commands from a file in the current shell. It can also be used to refresh environment variables and to be honest, the primary use of source command is to refresh environment variables.
The syntax of this command is simple:
source filename [arguments]
You can also use a dot (.) instead of the source command like this:
. filename [arguments]
The filename argument is the name of the file that contains the commands you want to execute. The arguments are optional and are passed to the file as positional parameters.
How Does the Source Command Work?
The source command works by reading and executing commands from the specified file in the current shell environment. This means that any changes made by the commands in the file are reflected in the current shell.
For example, let’s say you have a file called hello.sh that contains the following commands:
#!/bin/bash
echo "Hello, $USER"
name="Alice"
echo "Your name is $name"
If you run this file as an executable script, like this:
chmod +x hello.sh
./hello.sh
You will see the following output:
Hello, bob
Your name is Alice
However, if you run this file using the source command, like this:
source hello.sh
You will see the same output, but also notice that the value of the name variable has changed in your current shell. You can verify this by typing:
echo $name
You will see:
Alice
This is because the source command executed the commands from hello.sh in your current shell, and changed the value of the name variable. If you had run hello.sh as an executable script, the value of name would have remained unchanged in your current shell.
Why Use the Source Command?
One of the main reasons to use the source command is to refresh environment variables. Environment variables are variables that affect the behavior of your shell and other programs. For example, the PATH variable determines where your shell looks for executable files, and the PS1 variable determines how your prompt looks like.
Environment variables are usually defined in configuration files, such as ~/.bashrc or ~/.profile. These files are read and executed when you start a new shell session. However, if you make any changes to these files, they will not take effect until you start a new shell session or use the source command to reload them.
For example, let’s say you want to add a new directory to your PATH variable. You can do this by editing your ~/.bashrc file and adding a line like this:
export PATH="$PATH:/home/bob/bin"
However, if you try to run any executable file from /home/bob/bin in your current shell session, you will get an error like this:
bash: foo: command not found
This is because your current shell session does not know about the new directory you added to your PATH variable. To make it aware of it, you need to use the source command to reload your ~/.bashrc file, like this:
source ~/.bashrc
Now, if you try to run any executable file from /home/bob/bin in your current shell session, it should work fine.
Another reason to use the source command is to execute commands from a file without creating a new subshell. A subshell is a child process that inherits the environment of its parent process but does not affect it. When you run an executable script, it creates a new subshell and executes commands in it. This means that any changes made by the script are confined to the subshell and do not affect your current shell.
However, sometimes you may want to execute commands from a file and make changes to your current shell. For example, you may want to define some functions or aliases that you want to use in your current shell session. In that case, you can use the source command to execute commands from a file in your current shell and make changes to it.
For example, let’s say you have a file called functions.sh that contains some useful functions, like this:
#!/bin/bash
function backup() {
cp "$1" "$1.bak"
echo "Backup created for $1"
}
function restore() {
cp "$1.bak" "$1"
echo "Backup restored for $1"
}
If you run this file as an executable script, like this:
chmod +x functions.sh
./functions.sh
You will not see any output, and the functions will not be available in your current shell session. This is because the functions are defined in a subshell created by the script, and are not exported to your current shell.
However, if you run this file using the source command, like this:
source functions.sh
You will not see any output either, but the functions will be available in your current shell session. You can verify this by typing:
type backup
type restore
You will see:
backup is a function
backup ()
{
cp "$1" "$1.bak";
echo "Backup created for $1"
}
restore is a function
restore ()
{
cp "$1.bak" "$1";
echo "Backup restored for $1"
}
Now, you can use these functions in your current shell session, like this:
backup foo.txt
restore foo.txt
You will see:
Backup created for foo.txt
Backup restored for foo.txt
How to Use the Source Command Effectively?
Here are some tips and best practices for using the source command effectively:
- Use the source command to refresh environment variables after making changes to configuration files, such as ~/.bashrc or ~/.profile.
- Use the source command to execute commands from a file in your current shell and make changes to it, such as defining functions or aliases.
- Use the dot (.) as a shortcut for the source command. They are equivalent and interchangeable.
- Use the full path or relative path of the file you want to source. For example, source /home/bob/functions.sh or source ./functions.sh. Do not use just the filename, such as source functions.sh, unless the file is in your current directory or in your PATH variable.
- Make sure the file you want to source is readable and executable. You can use the chmod command to change the permissions of the file if needed.
- Make sure the file you want to source does not contain any harmful commands that could damage your system or data. Only source files from trusted sources or that you have written yourself.
Frequently Asked Questions (FAQs)
Question: What is the difference between source and execute in Linux?
Answer: The difference between source and execute in Linux is that source executes commands from a file in the current shell environment, while execute runs a file as an executable script in a new subshell environment. Source affects your current shell, while execute does not.
Question: How do I know if a file is sourced or executed in Linux?
Answer: You can use the echo command to print the value of a special variable called $0, which contains the name of the file being executed. If the file is sourced, $0 will contain the name of your current shell, such as bash or zsh. If the file is executed, $0 will contain the name of the file itself.
For example, let’s say you have a file called test.sh that contains the following command:
echo $0
If you source this file, like this:
source test.sh
You will see something like this:
bash
If you execute this file, like this:
chmod +x test.sh
./test.sh
You will see something like this:
./test.sh
Question: How do I exit a sourced file in Linux?
Answer: To exit a sourced file in Linux, you can use the return command. This will stop executing commands from the sourced file and return to your current shell. Do not use the exit command, as this will terminate your current shell session.
For example, let’s say you have a file called test.sh that contains the following commands:
echo "Before return"
return
echo "After return"
If you source this file, like this:
source test.sh
You will see something like this:
Before return
The echo “After return” command will not be executed, as the return command will stop sourcing the file.
Summary
The source command is a useful utility that can be used to refresh environment variables and execute commands from a file in your current shell environment. It works by reading and executing commands from the specified file in your current shell. You can also use a dot (.) as a shortcut for the source command. To use it effectively, you need to provide the full or relative path of the file you want to source, make sure it is readable and executable, and avoid any harmful commands.
Disclaimer: This blog post is for informational purposes only and does not constitute professional advice. The author and publisher are not liable for any damages or losses that may result from using or relying on any information contained in this blog post. Always consult a qualified Linux expert before using any commands or files mentioned in this blog post.