Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Source Command - DesignLinux

Aug 10 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

How to Install Tor Browser on Ubuntu 20.04

Prev Post

How to Install Apache Cassandra on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 1,051
  • 609,499

DesignLinux.com © All rights reserved

Go to mobile version