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.

Sign up forfree

Forgot your password?

Create an account

*Required: 8 chars, 1 capital letter, 1 number

By continuing, you agree to thePrivacy PolicyandTerms of Use.You also agree to receive our newsletters, you can opt-out any time.

4

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 withthe touch command. Open the file in a text editor of your choice and write a few basic shell commands.

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.

Linux neofetch command

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 ofenvironment 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:

article limit background

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.

output of sourcing the file

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.

using the source command to update environment variables

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 isa shell built-inthat 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 thechsh -s ($which shell_name)command.

Fire up a new terminal afterchanging 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.

Want to gain familiarity with Linux? Start with these basic Linux commands to learn standard computing tasks.

You don’t need to fork out for expensive hardware to run an AI on your PC.

Now, I actually finish the books I start.

These plugins will make you wonder why you used Photoshop in the first place.

I found my TV was always listening—so I shut it down.

Revolutionize your driving experience with these game-changing CarPlay additions.

Technology Explained

PC & Mobile