Bootstrap FreeKB - Linux Commands - source (export variable)
Linux Commands - source (export variable)

Updated:   |  Linux Commands articles

The source command can be used to:

  1. make variables set in some other script available in your shell
  2. make lists/arrays set in some other script available in your shell
  3. make functions set in some other script available in your shell

Let's consider a BASH shell script named example.sh that contains the following.

#!/bin/bash
my_variable="hello"
my_array=(hello world)
function my_function {
  return "world"
}

 

Running the script and then attempting to echo $my_variable will produce no output.

~]# bash example.sh
~]# echo $my_variable

 

The source command can be used make the variables, lists/arrays/, and functions set in the script available in your shell.

~]# source example.sh

 

Or, just a single period can be used.

~]# . example.sh

 

Now, echo $my_variable should produce output.

~]# echo $my_variable
Hello World

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 5bfd85 in the box below so that we can be sure you are a human.