Linux 101: What does "sourcing a file" mean in Linux?

2 weeks ago 11
PR Distribution

Jack Wallen explains the Linux root bid by mode of an example.


Image: Larich/Shutterstock

Sourcing a record successful Linux is simply a precise important concept, but it mightiness not beryllium 1 you'll usage aboriginal connected successful your Linux career. Even so, I'm going to effort to explicate this challenging conception successful a mode you tin recognize it. 

Sourcing a record makes it imaginable for an executable to "source" accusation from a publication arsenic though the publication had printed its output to the terminal. It's not an casual conception to grasp, truthful I'm going to amusement you by mode of an example. 

SEE: 5 Linux server distributions you should beryllium using (TechRepublic Premium)

Let's commencement with a ammunition script, named that contains a fewer elemental lines of codification that pass immoderate publication that uses its output indispensable beryllium tally with sudo privileges. That publication mightiness look thing similar this:

check_root () { if [[ $EUID -ne 0 ]]; then echo "You indispensable tally this publication with sudo privileges" exit 1 fi }

Next, we'll make an executable file, named, that volition usage the output of and people retired if the execution has been done with sudo privileges. That record mightiness look thing similar this:

#!/usr/bin/env bash source check_root echo "I person sudo privileges"

The record volition root the record and usage the resulting output. 

Before we really execute the file, contented the bid chmod u+x Now, contented the bid ./ and you should spot printed "You indispensable tally this publication with sudo privileges." 

However, if you tally the bid sudo ./ you'll spot I person sudo privileges printed out. So the 2nd publication uses the output of the archetypal arsenic a root for input. 

SEE: Rust: What developers request to cognize astir this programming connection (free PDF) (TechRepublic)

By utilizing this device, you tin make much streamlined scripts that root aggregate files. You could besides usage the root bid successful your scripts to work configuration files, to prevention abstraction and time. 

And that's the root bid successful a nutshell ... oregon a bash shell, however, you privation to look astatine it.

Open Source Weekly Newsletter

You don't privation to miss our tips, tutorials, and commentary connected the Linux OS and unfastened root applications. Delivered Tuesdays

Sign up today

Also spot

Read Entire Article