FactorPad
Faster Learning Tutorials

Linux source command summary with examples

source allows you to read and execute commands from a file.
  1. Purpose - Learn what source is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with source.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 13, 2016
Updated: July 24, 2018
In this tutorial, 87 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / source command


The Linux source command

Intermediate

Learn to read and execute commands at the Linux command line.

Video Tutorial

Linux source command summary with examples (3:38)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our eighty-seventh word, or command to memorize is source from our category Workflow.

source allows you to read and execute commands from a file.

Recall from videos (tutorials) #47 and #49 we saw shell built-in programs that modified variables and here we'll learn another shell built-in that reads and executes commands as we transition to our last section on writing scripts to pull together what we've learned here.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The source command has no options, and the argument is the file.

Unlike most commands, help is not available with double-dash --help and information is found in the lengthy man page for bash.

So why is source an important command? Well, at times you need to run scripts in the current setting without say logging out and back in to take effect, like a configuration file. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Start with mkdir from video (tutorial) #22 to create a directory called bin and put it in our home directory using the tilde ~/.

$ mkdir ~/bin

So source reviews directories in the PATH, but bin won't be there (meaning typing the name of the script won't work until you add this directory to your PATH). So you can add a line export PATH=~/bin:, you need the colon, and then this section here for the PATH variable "$PATH" to your .bashrc file so that bash at startup adds this directory to the PATH each time.

$ export PATH=~/bin:"$PATH"

Okay, let's move on the script. I used nano to create funscript, and at this point it prints text and the date. We'll add to it in coming videos (tutorials).

$ cd bin $ nano funscript

Please pause the video to read and create this on your end and save it as funscript.

GNU nano 2.2.6 File: funscript #!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 87, print text and the current date/time echo -e "\nThe current date and time:" date [ Read 10 lines ] ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

Okay next, use chmod 700 so the owner can read, write and execute funscript.

$ chmod 700 funscript

And last, let's type source funscript.

The current date and time: Sun Nov 13 22:09:10 PST 2016

And there it is, how fun. 8D

A Final Tip

Okay now you know how to use source. And you know the syntax for commands, options and arguments.

One last tip about the source command. So there is a synonym for source that you really need to know and it's simply a period .. That's it.

Okay, thanks for visiting today. I hope this was a helpful introduction to the source command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

We'll keep building on this script as we explore the Intermediate-level topic of shell scripting. It may help to supplement this text-based tutorial with our YouTube videos for the more difficult section ahead. Subscribe here.

  • For the Outline to all 100 command, click Outline.
  • To go back to the ssh command, hit Back.
  • To see how fast things are running and review the time command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / source command



 
 
Keywords:
Linux source command
execute commands in linux
add to path
reload configuration files
Unix source
dot filename meaning in linux
what is dot in linux
linux dot operator
dot slash in linux
The period character in Linux
bash source
shell source
read files
read commands
bash script
execute scripts
period character in linux