Linux Source Command with Examples

Introduction

In Linux systems, source is a built-in shell command that reads and executes the file content in the current shell. These files usually contain a list of commands delivered to the TCL interpreter to be read and run.

This tutorial will explain how the source command works and when to use it.

Linux source command with examples

Prerequisites

Source Command Syntax

The source command uses the following syntax:

source [filename] [arguments]

Where:

  • [filename]: The name or path to the file you want the source command to execute.
  • [arguments]: Any arguments you provide become positional parameters when the file is executed.

Note: If you don’t provide the full path to the file you want to execute, source will search the $PATH variable for the file. If it does not find the file there, source will search the current directory.

The dot (period) character can be used in place of the source command, resulting in the same output:

. [filename] [arguments]

Linux Source Command Examples

Here are some of the ways you can use the source command:

Pass Arguments

Create a text file called example.txt in the Home directory with the following content:

pwd
date
time

Use the source command to pass the content of this file as an argument:

source example.txt
Using the source command to pass a file as an argument

The output shows that the source command moves line by line, executing any commands listed in example.txt.

For a more complex example, change the content of example.txt to:

echo "The current directory is:"
pwd
echo "Today's date is:"
date
echo "The time is:"
time

Move the file to Home/source_command/example. With the source command, pass the content of the file as an argument using the full path to the file:

source source_command/example/example.txt
Using the source command with the full path to the file

Note: The source command also allows you to run scripts in the current shell environment, unlike the bash command, which creates a new shell environment.

Read the Configuration File

The source command also allows you to read variables from a file. Start by creating an example configuration file example_config.sh in the Home directory and adding the following content:

VAR1="a"
VAR2="b"
VAR3="c"

Create a bash script called example_bash.sh and add the following:

#!/usr/bin/env bash

source example_config.sh

echo "VAR1 is $VAR1"
echo "VAR2 is $VAR2"
echo "VAR3 is $VAR3"

The source command allows example_bash.sh to read the variables VAR1, VAR2, and VAR3 you defined in example_config.sh.

Run the example_bash.sh script using the source command:

source example_bash.sh
Using the source command to read variables from a configuration file

Source Functions

If you have functions you are using in several different scripts, you can save them as separate files and use the source command to refer to them when writing scripts.

For instance, start by creating a function check_root.sh that checks whether the user running the script is a root user:

check_root() {
  if [[ $EUID -ne 0 ]]; then
    echo "You must run this script as root" 
    exit 1
  fi
}

Create a script called example_script.sh and use the source command to insert the check_root.sh function:

#!/usr/bin/env bash

source check_root.sh
check_root

echo "This is the root user"

Running this script as a non-root user produces "You must run this script as root" as the output and exits the script:

bash example_script.sh
Running the script as a non-root user

Running the script as a root user shows "This is the root user" as the output:

sudo bash example_script.sh
Running the script as a root user

Note: Using the source command to run example_script.sh executes the script in the current shell environment. Since you cannot run the source command as the root user, starting the script causes the terminal window to close. Use the bash command to execute the script instead.

Refresh the Current Shell Environment

For this example, we are creating an alias command ll:

alias ll = 'ls -l'

This command lists the files in the current directory using the extended format:

Using the new ll command to list the files in the current directory

However, this command only works in the current shell session. To make it permanent, open the bashrc file with:

sudo nano ~/.bashrc

Under the #some more ls aliases section, add the following:

alias ll = 'ls -l'
Adding the new ll command alias in the bashrc file

Refresh the current shell environment with the source command:

source ~/.bashrc

Conclusion

After reading this tutorial, you should know how to use the source command in Linux to run multiple commands from a single file.

For a more comprehensive overview of useful commands, check out our Linux commands cheat sheet.

原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/224405.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

相关推荐

发表回复

登录后才能评论