source Command in Linux



The Linux source command is a very useful feature which allows you to run a file or a script directly in your current shell environment, rather than having to spawn a new shell session. If the script manipulates any setting, such as environment variables, or even establishes functions, that becomes usable as soon as it's executed by the command line, without ever having to terminate your terminal shell.

It's usually employed to reload config files, impose new settings, or execute scripts with less hassle of having to make them executable. Rather than taking the inconvenience of closing and re-opening your terminal just to view the changes, the source command helps you update your environment efficiently and fast.

Table of Contents

Here is a comprehensive guide to the options available with the source command −

Syntax for the source Command

The basic syntax for the source command is as follows −

source [filename]

Here, [filename] is the name of the script or file you want to execute in the current shell. The file can contain environment variable declarations, function definitions, or general shell commands.

Examples of the source Command in Linux

Let's explore a few practical examples of the source command in Linux environment −

  • Reloading Environment Variables
  • Running a Script in the Current Shell
  • Defining Functions from a File
  • Testing Scripts Without Executable Permission
  • Switching Between Configuration Files

Reloading Environment Variables

To make changes to .bashrc or .bash_profile instantly without restarting the terminal, you can use source ~/.bashrc. This helps the new environment variables or aliases be applied immediately in your existing session.

source ~/.bashrc

This executes the changes of your .bashrc file within your current shell session instantly.

source Command in Linux1

Running a Script in the Current Shell

Running ource myscript.sh executes the script in the current shell rather than as a new process. This makes variables such as MY_VAR="Hello, World!" available for use throughout the session when referenced with echo $MY_VAR.

export MY_VAR="Hello, World!"

To run this script and export the MY_VAR variable into your current shell, use −

source myscript.sh

After running this command, typing echo $MY_VAR will display −

source Command in Linux2

Defining Functions from a File

Assume you have a file functions.sh containing the following −

greet() {
	echo "Welcome, $1!"
}

By using the source command −

source functions.sh

You can now invoke the greet function directly in the current shell −

greet Awais
source Command in Linux3

Testing Scripts without Executable Permissions

Normally, scripts need execution permissions to be run (chmod +x script.sh). However, sourcing a script like source test.sh allows it to run even without execution rights, which is helpful when testing or debugging scripts.

source test.sh

This is especially useful during testing or debugging scripts without changing their file permissions.

Switching Between Configuration Files

You can use the source command to load different configuration files for a tool or environment. For instance, if you have multiple configuration files for a software application −

source config1.sh

Later, you can switch to another configuration file −

source config2.sh

This method allows you to test or apply different settings seamlessly.

When to Use the source Command?

You would use the source command in the following cases −

  • Reload configuration files like .bashrc or .bash_profile to implement changes immediately.
  • Execute scripts in the current shell to preserve variables and functions without an extra shell session.
  • Specify reusable functions from other files to enhance workflow efficiency.
  • Debug scripts without executing permission to test commands without changing file attributes.
  • Use different settings dynamically when dealing with multiple configuration files.

Common Mistakes When Using source

A lot of users either don't know how source is supposed to work, or forget to use it when they need to. A common error is running a script normally (./) instead of using the proper execution method (source). Since the scripts you kick off run in a subshell by default, any variables you define inside of them are not available afterwards.

One common mistake is omitting export when defining environment variables inside sourced scripts: these variables may not be accessible outside the script. Also sourcing a file with syntax errors will lead to unintended behavior as commands will be executed in the current shell session directly.

Differences Between source and Executing a Script Normally

The key difference between sourcing a script (e.g., source script.sh) and running it the regular way (e.g., ./script.sh) lies in their impact on the shell environment. Normally, running a script spawns a subshell, which means any variables or functions created within that script vanish once it's finished.

In contrast, sourcing a script executes all commands within the current shell session, ensuring that variables and functions stick around. This makes sourcing perfect for changes you want to persist, whereas regular execution is more suitable for scripts that don't require altering the current environment.

Conclusion

The source command in Linux is a really handy tool for keeping your shell environment organized. It lets you run scripts, refresh config files, and create functions you can use over and over again, all within your current shell session. This means you don't have to waste time restarting your shell or making scripts executable just for testing.

Whether you're tweaking environment variables, swapping between different setups, or troubleshooting shell scripts, the source command makes everything flow smoothly and gives you results right away. It's so useful that anyone using Linux, from total newbies to seasoned pros, will find it helpful.

Advertisements