How to Read and Execute From a File With the Linux source Command

source is a handy built-in shell command that accepts arguments, parses commands in a script, and executes them in the shell environment. The file’s content is read by source and then passed on to the Tool Command Language (TCL) interpreter, which executes the commands.

Let’s learn how to use source to execute commands and work with shell environments on Linux, along with a brief explanation of how it differs from the bash command.

Use source to Execute Commands in a File

To use the source command, you’ll first need to create a sample file and populate it with some shell commands. Start by creating a new file with the touch command. Open the file in a text editor of your choice and write a few basic shell commands.

touch test.txt
vi test.txt

echo "Hello!"

. test.txt
source text.txt

Write out the text file and proceed to pass it as an argument to the source command. The system will execute the commands in the file and will return an output.

Now that you know the basics of how to use source, let’s try changing the Bash shell environment. However, before you proceed, you must have a fundamental understanding of environment variables in Linux.

Use the source Command to Update the Bash Shell Environment

On Linux, environment variables are inherited but immutable. If you execute a Bash shell script that modifies an environment variable $TEST, the script will spawn a new, forked shell process, and modification of $TEST will take place within it instead of the original Bash environment.

The child process will not be able to modify the parent process’s environment. It can only modify its own environment. Let’s understand this with a practical example:

export TEST="deb"
vi example

export TEST="bed"
echo $TEST

chmod +x example
echo $TEST

Here’s where the source command comes into play. With the source command, you can execute a script in the same shell environment, which otherwise would have been forked. So, if you source a script that modifies environment variables, it will make the changes for the Bash shell environment.

To test it out, write the commands in the code box above in a new file and use the source command instead of making it executable.

vi test file
. test_file
echo $TEST

As you can see, using the source command, you were able to update the environment variables of the parent process. This is the primary use case of the source command, i.e., to modify the parent environment.

That’s all the steps you need to update the Bash shell environment with the source command.

Difference Between the Linux bash and source Commands

The difference between Linux bash and source commands is that when you execute a script with the bash command, it tells the Linux kernel to create a new Bash process to read and execute the script, copy the output to the original shell process, and display it.

But the source command is a shell built-in that reads and evaluates a file within the current shell process. So, all changes made by the script will be retained in the Bash shell.

Troubleshooting the source Command Errors

In some Linux systems, you might run into an error “source not found” while trying to use the source command. You can fix this error in two ways:

1. Change the Shell

Some shells do not support the source command. In that case, you’ll need to change your shell environment with the chsh -s ($which shell_name) command.

Fire up a new terminal after changing your shell, and try using the source command. If the new shell supports sourcing, then it should work flawlessly.

2. Use the Dot/Period Syntax

Some shell environments do not support “source” syntax but the alternative “.” syntax. The “source” syntax is a synonym for dot in Bash. But this does not work in the POSIX shell, so for maximum compatibility, use the period syntax.

Basic Linux Commands You Should Know

Linux shell commands are a great way to interact with the system using the terminal. They are the very essence of using *NIX systems.

While you don’t need to know all the commands, being familiar with a few useful ones will surely prove fruitful as you venture deep into the world of Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker