Introduction
This lab focuses on understanding and using the source command in Linux, which is a powerful tool for executing scripts in the current shell. Unlike regular script execution that runs in a separate subprocess, the source command executes commands from a file in the current shell environment. This allows variables and functions defined in the script to persist in your current session.
Throughout this lab, you will learn:
- How to create and execute scripts using the
sourcecommand - How to pass parameters to scripts when using
source - How the
sourcecommand differs from regular script execution - How to use the
sourcecommand to set up environment variables
By the end of this lab, you will be comfortable using the source command for efficient Linux shell scripting and environment management.
Creating and Sourcing a Basic Script
In this step, we will create a simple script file and use the source command to execute it in our current shell environment. This will help us understand how source differs from regular script execution.
When you run a script normally (using ./script.sh), the shell creates a new subprocess to execute the script. Any variables or functions defined in that script only exist within that subprocess and disappear when the script finishes. However, when you use the source command (or its shorthand .), the commands in the script execute in your current shell environment, allowing variables and functions to persist after the script completes.
Let's create a basic script to demonstrate this concept:
1. Navigate to the project directory
First, make sure you're in the correct directory:
cd ~/project
2. Create a simple script file
Create a new file named variables.sh using the nano editor:
nano variables.sh
Add the following content to the file:
#!/bin/bash
## This script sets an environment variable
export WEATHER="Sunny"
echo "The weather is now set to: $WEATHER"
Press Ctrl+O to save the file, then Enter to confirm the filename, and finally Ctrl+X to exit nano.
3. Make the script executable
Before we can run the script, we need to make it executable:
chmod +x variables.sh
4. Run the script normally
First, let's run the script in the traditional way:
./variables.sh
You should see output similar to:
The weather is now set to: Sunny
Now, check if the WEATHER variable exists in your current shell:
echo $WEATHER
You'll likely see nothing output, or an empty line. This is because the variable was only set in the subprocess that ran the script, not in your current shell.
5. Source the script
Now, let's use the source command to run the script:
source variables.sh
You'll see the same output:
The weather is now set to: Sunny
Check the WEATHER variable again:
echo $WEATHER
This time, you should see:
Sunny
The WEATHER variable now exists in your current shell because you used the source command to execute the script.
6. Using the shorthand notation
You can also use the dot (.) shorthand notation for the source command:
. variables.sh
This will produce the same result as using source variables.sh.
Using Source with Parameters
In this step, we'll learn how to pass parameters to a script when using the source command. This is useful when you need to make your scripts more flexible and reusable.
Just like regular script execution, you can pass arguments to a script when using the source command. Inside the script, these arguments are accessible through the standard positional parameters ($1, $2, etc.), just as they would be when running the script directly.
1. Creating a script that accepts parameters
Create a new file called greeting.sh in the ~/project directory:
cd ~/project
nano greeting.sh
Add the following content to the file:
#!/bin/bash
## This script accepts a name parameter and sets a greeting variable
## Check if a parameter was provided
if [ -z "$1" ]; then
NAME="Guest"
else
NAME="$1"
fi
## Set the greeting environment variable
export GREETING="Hello, $NAME!"
echo "$GREETING"
Save and exit nano by pressing Ctrl+O, Enter, and then Ctrl+X.
2. Make the script executable
chmod +x greeting.sh
3. Source the script with a parameter
Let's source the script and pass a name parameter:
source greeting.sh "Alice"
You should see the output:
Hello, Alice!
Now check if the GREETING variable is set in your current shell:
echo $GREETING
You should see:
Hello, Alice!
4. Source with a different parameter
Try sourcing the script again with a different name:
source greeting.sh "Bob"
Output:
Hello, Bob!
Check the GREETING variable again:
echo $GREETING
Output:
Hello, Bob!
Notice how the GREETING variable has been updated with the new value.
5. Source without parameters
You can also source the script without any parameters:
source greeting.sh
Output:
Hello, Guest!
The script uses a default value ("Guest") when no parameter is provided.
Check the GREETING variable one more time:
echo $GREETING
Output:
Hello, Guest!
This demonstrates how you can make your scripts adaptable by processing different input parameters.
Creating a Development Environment Script
In this step, we'll create a more practical example: a script that sets up a development environment with multiple variables. This is a common use case for the source command in real-world scenarios.
1. Creating the development environment script
Create a new file called dev_env.sh in the ~/project directory:
cd ~/project
nano dev_env.sh
Add the following content to the file:
#!/bin/bash
## Development environment setup script
## Database connection settings
export DB_HOST="localhost"
export DB_PORT="5432"
export DB_USER="dev_user"
export DB_PASSWORD="dev_password"
export DB_NAME="dev_database"
## API settings
export API_URL="http://localhost:3000/api"
export API_KEY="dev_api_key_123"
## Application paths
export APP_ROOT="/home/labex/project/app"
export LOG_DIR="/home/labex/project/logs"
## Create directories if they don't exist
mkdir -p $APP_ROOT
mkdir -p $LOG_DIR
## Display confirmation message
echo "Development environment configured with the following settings:"
echo "-----------------------------------------------------------"
echo "Database: $DB_USER@$DB_HOST:$DB_PORT/$DB_NAME"
echo "API URL: $API_URL"
echo "App Root: $APP_ROOT"
echo "Log Directory: $LOG_DIR"
echo "-----------------------------------------------------------"
echo "Environment is ready for development!"
Save and exit nano by pressing Ctrl+O, Enter, and then Ctrl+X.
2. Make the script executable
chmod +x dev_env.sh
3. Source the development environment script
Now, let's source the script to set up our development environment:
source dev_env.sh
You should see output similar to:
Development environment configured with the following settings:
-----------------------------------------------------------
Database: dev_user@localhost:5432/dev_database
API URL: http://localhost:3000/api
App Root: /home/labex/project/app
Log Directory: /home/labex/project/logs
-----------------------------------------------------------
Environment is ready for development!
4. Verify that the environment variables are set
Let's check some of the environment variables that were set by the script:
echo "Database connection string: $DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME"
Output:
Database connection string: dev_user:dev_password@localhost:5432/dev_database
Check the API settings:
echo "API settings: $API_URL with key $API_KEY"
Output:
API settings: http://localhost:3000/api with key dev_api_key_123
5. Verify that the directories were created
Check if the directories specified in the script were created:
ls -la ~/project/app ~/project/logs
You should see output showing that both directories exist.
This example demonstrates how you can use the source command to set up a complete development environment with multiple environment variables and configurations. This approach is commonly used in development workflows to switch between different environments (development, testing, production, etc.).
Summary
In this lab, you have learned how to use the source command in Linux to execute scripts in the current shell environment. Here's a recap of what you've accomplished:
Creating and Sourcing Basic Scripts: You learned how using
sourcediffers from regular script execution, as it allows variables and functions defined in the script to persist in your current shell session.Passing Parameters to Sourced Scripts: You explored how to make scripts more flexible by passing parameters to them when using the
sourcecommand, and how to process those parameters within the script.Setting Up Development Environments: You created a practical script that sets up a development environment with multiple environment variables and configurations, which is a common real-world use case for the
sourcecommand.
The source command is a powerful tool in Linux scripting that allows you to:
- Set environment variables that persist across your shell session
- Define functions that can be used later in your session
- Create modular configurations that can be reused
- Set up complex environments quickly and consistently
These skills are fundamental for anyone working with Linux systems, particularly for developers, system administrators, and DevOps engineers. By mastering the source command, you can create more efficient and organized scripts for managing your Linux environment.



