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 into a script, and executes them in the shell environment. The file content is read by source and then passed to the Tool Command Language (TCL) interpreter, which executes the commands.


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


Use source to run commands on a file

To use the source command, you’ll first need to create a sample file and complete 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 type some basic shell commands.

touch test.txt
vi test.txt


echo "Hello!"
pwd
ls


. test.txt
source text.txt

Write 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 return an output.

Now that you know the basics of how to use the source, let’s try changing the Bash shell environment. Before proceeding however, you should 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 are immutable. If you run a Bash shell script that modifies a $TEST environment variable, the script will spawn a new forked shell process, and the $TEST modification will take place within it instead of the original Bash environment.

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

export TEST="deb"
vi example


export TEST="bed"
echo $TEST


chmod +x example
./example
echo $TEST

This is where the source command comes into play. With the source command, you can run a script in the same shell environment, which would otherwise have been forked. So if you get a script that modifies the environment variables, it will make the changes to the Bash shell environment.

To try it out, write the commands in the code box above to 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 main process. This is the main use case of the source command, i.e. to modify the main environment.

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

Difference between Linux bash and source commands

The difference between the Linux bash and source commands is that when you run a script with the bash command, you tell 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 built-in shell that reads and evaluates a file within the current shell process. Therefore, all changes made by the script will be preserved in the Bash shell.

Troubleshooting source command errors

On some Linux systems, you may encounter a “font not found” error when trying to use the font command. You can fix this error in two ways:

1. Change the case

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

Start a new terminal after changing your shell and try using the source command. If the new shell supports provisioning, then it should work without a problem.

2. Use the dot/dot syntax

Some shell environments do not support “font“syntax but the alternative”.” syntax. The “source” syntax is a synonym for dot in Bash. But this doesn’t work in the POSIX shell, so for maximum compatibility, use the dot 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 is sure to pay off as you venture into the world of Linux.

Leave a Comment