• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash Source Command

Written by Admin, Updated On August 10, 2020
bash, terminal
Source Command

The source is a shell built-in command which is used to read and execute the content of a file, passed as an argument in the current shell script. It is useful to load functions, variables, and configuration files into shell scripts.

Source Command Syntax#

The following is the basic syntax for the Source Command:

source FILENAME [ARGUMENTS]

Here,

  • FILENAME – If the FILENAME not provided with full path then command will search for the file in the directories specified in the $PATH environmental variable.
  • ARGUMENTS – You can pass additional arguments after FILENAME.

Source Command Examples#

Now we will shows few examples of how to use the source command:

Sourcing Functions#

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

For an example, we will create a file with a bash function that will check whether the user running the script is the root, and if not, it shows a message and exits the script.

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

Now in each script that needs to be run only by the root user, simply source the functions.sh file and call the function:

#!/usr/bin/env bash

source functions.sh
check_root

echo "It's root"

As a result, if you run the script above as a non-root user, it will print “You must run this script as root” and exit.

By this way you can reuse this function whenever needed and it’s easy to edit at one file only.

Bash Configuration file#

The source command is also used to read variables from a file. The variables must be set using the Bash syntax, VARIABLE=VALUE.

For example, create a test configuration file named config.sh:

VAR1="welcome"
VAR2="again"

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"

It should show output as following:

VAR1 is welcome
VAR2 is again

Conclusion#

In this article, you have learned how to use the source built-in command in your shell scripts.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Run Sudo Command Without Password
Next Article   How to Install Jenkins on Ubuntu 20.04

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook