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 contents of the file are read from source and then passed to the Tool Command Language (TCL) interpreter, which executes the commands.


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


Use source to run commands in a file

To use the source command, you first need to create a sample file and populate it with some shell commands. First, create a new file using the touch command. Open the file in your favorite text editor and write some basic shell commands.

touch test.txt
vi test.txt


echo "Hello!"
pwd
ls


. test.txt
source text.txt

Write out the text file and pass it as an argument to the source command. The system runs the commands in the file and returns an output.

Now that you know the basics of using source, let’s try to change the bash shell environment. Before proceeding, however, you must have a basic 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 run a bash shell script that changes a $TEST environment variable, the script spawns a new, forked shell process, and the $TEST change takes place in it instead of the original bash environment.

The child process cannot change the environment of the parent process. It can only change its 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. The source command allows you to run a script in the same shell environment that would otherwise have been forked. So if you get a script that changes environment variables, it makes the changes for the bash shell environment.

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


vi test file
. test_file
echo $TEST

As you can see, the source command allowed you to update the environment variables of the parent process. This is the primary use case of the source command, i.e. changing the parent environment.

That are all the steps you need to update bash shell environment with source command.

Difference between the Linux bash and source commands

The difference between Linux bash and source commands is that when running a script with the bash command, the Linux kernel is instructed to create a new bash process to read and run the script, converting the output to the original one Copy and view shell process.

But the source command is a shell built-in command that reads and evaluates a file within the current shell process. So any changes made by the script will remain in the bash shell.

Troubleshooting the source command errors

On some Linux systems, when trying to use the source command, the source not found error may occur. You can fix this error in two ways:

1. Change the shell

Some shells do not support the source command. In this case you need to change your shell environment with the chsh -s ($which shell name) Command.

After changing your shell, launch a new terminal and try the source command. If the new shell supports sourcing, it should work fine.

2. Use dot/dot syntax

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

Basic Linux commands you should know

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

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

Read  How to watch House of the Dragon Episode 1: Stream Game of Thrones spin-off free in the UK

Leave a Comment

Your email address will not be published.