How to read and execute from a file using Linux source command

Linux neofetch command

source is an easy-to-use built-in shell command that accepts arguments, parses commands in a script, and executes them in a shell environment. The file content is read by source and then passed to the Tool Command Language Interpreter (TCL), 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 a bash command.

Use source to execute commands in a file

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

touch test.txt
vi test.txt

echo "Hello!"

. 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 the output.

Now that you know the basics of how to use the source, let’s try changing the Bash shell environment. However, before we proceed, you should 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 not immutable. If you execute a Bash shell script that modifies the $TEST environment variable, the script will result in a new forked shell process, and $TEST will be modified inside it instead of the original Bash environment.

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

export TEST="deb"
vi example

export TEST="bed"
echo $TEST

chmod +x example
echo $TEST

This is where the source command comes in. With the source command, you can execute a script in the same environment as the shell, which would otherwise be forked. So, if you create a script that modifies the environment variables, the changes will be made to the Bash shell environment.

To test it, type the commands in the above code box 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, using the source command, I was able to update the environment variables of the parent process. This is the basic use case of the source command, i.e. to modify the parent environment.

These are all the steps you need to update the Bash shell environment with the source command.

Difference between Linux bash commands and source

The difference between Linux bash and source commands is that when a script is executed 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 builtin wrapper 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.

Source troubleshooting

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

1. Shell change

Some shells do not support source command. In this case, you will need to change your shell environment with an extension chsh -s ($ which shell_name) ordering.

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

2. Use the point/period formula

Some shell environments do not support “source“Syntax but alternative”.The syntax. The ‘infinitive’ syntax is synonymous with the period 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 core of using *NIX systems.

While you don’t need to know all the commands, getting to know some useful commands will surely prove fruitful as you are venturing deep into the world of Linux.

Related posts

Leave a Comment