UNIX Source Command: Read And Execute Commands From File

The source command in Unix/Linux allows you to run commands from a file in the current shell environment. The source command is often used to run shell scripts and shell functions, and can be useful for configuring the shell environment, setting environment variables, and defining shell functions.

Here’s an example of how to use the source command:

  1. Create a file with the commands you want to run. For example:
    #!/bin/bash
    echo "This is a test."

    Save the file with a .sh extension, such as test.sh.

  2. Make the file executable:
    chmod +x test.sh
  3. Run the file using the source command:
    source test.sh

    The output of the file will be displayed in the terminal:

    This is a test.

Note that when you run a script with the source command, the changes made to the shell environment (such as setting environment variables) persist after the script has completed. When you run a script with the ./ command, the changes are only temporary and are not saved after the script has completed.

Leave a Comment