Signup/Sign In

Bash Source Command

Posted in Programming   JANUARY 10, 2022

    Bash

    The source command reads and executes commands from the file supplied as its argument in the current shell context. It is handy to load functions, variables, and configuration files into shell programs.

    The source is a shell built-in in Bash and other popular shells used in Linux and UNIX operating systems. Its behavior may vary slightly from cover to shell.

    Source Command Syntax

    The syntax for the source command is as follows:

    source FILENAME [ARGUMENTS] \s. FILENAME [ARGUMENTS]

    • source and. (a period) are the same command.
    • If the FILENAME is not a full path to a file, the command will look for the file in the directories supplied in the $PATH environmental variable. If the file is not found in the $PATH, the command will hunt for the file in the current directory.
    • If any ARGUMENTS are specified, they will become positional parameters to the FILENAME.
    • If the FILENAME exists, the source command exit code is 0. Otherwise, if the file is not discovered, it will return 1.

    Source Command Examples

    In this part, we will look at some simple examples of utilizing the source command.

    Sourcing Functions

    If you have shell scripts employing the same routines, you can extract them in a separate file and then source that file in your scrips.

    In this example, we will construct a file with a bash function that checks whether the user running the script is the root, and if not, it shows a message and exits the script.

    functions.sh 
    check root () () { 
     if [[ $EUID -ne 0 ]]; then 
     echo "This script must be run as root" 
     exit 1 
     fi 
    }


    In each script that has to be launched only by the root user, source the functions.sh file and invoke the function:

    #!/usr/bin/env bash
    
    source functions.sh
    
    check root
    
    echo "I am root" 


    If you run the script above as a non-root user, it will print "This script must be run as root" and exit.

    The advantage of this technique is that your scripts will be smaller and more legible, you can reuse the same function file whenever needed, and in case you need to modify a function, you'll edit only one file.

    Bash Configuration file

    With the source command, you may also read variables from a file. The variables must be set using the Bash syntax, VARIABLE=VALUE.

    Let’s build a test configuration file:

    VAR1="foo"
    
    VAR2="bar"


    In your bash script, use the source command to read the configuration file:

    #!/usr/bin/env bash
    
    source config.sh
    
    echo "VAR1 is $VAR1"
    echo "VAR2 is $VAR2" 


    If you run the script, the output will look like this:

    VAR1 is foo
    VAR2 is bar


    Conclusion

    You have learned how to use the built-in source command in your shell scripts in this post. If you have any questions or feedback, please post a remark.

    Published by: AdarshKumarSingh
    Tags:BashCommands
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS