Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

source

Run the commands of a file in the current shell, so its variables, aliases and functions stay.

Level
RHCSA exam
Group
Shell and help
Needs root
No
Package
bash (built-in)

What it does

A script normally runs in a new child shell: when it ends, its variables are gone. source FILE (or the short form . FILE) reads the file and runs its lines in your current shell. Variables, aliases, functions and cd changes stay.

Use it to load ~/.bashrc after an edit, to load settings from a file (.env), or to use a library of shell functions in a script.

How to write it

source FILE [ARGUMENTS]
. FILE [ARGUMENTS]

Options 2

Most used

FILE
The file to read. Without a slash, Bash looks for it in $PATH, then in the current folder.
ARGUMENTS
Become $1, $2 … inside the file while it runs.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Load your .bashrc after an edit

    source ~/.bashrc

    New aliases and variables work at once, without logging out.

  2. 2The short form with a dot

    . ~/.bashrc

    Exactly the same as source.

  3. 3Load variables from a settings file

    cat app.env
    APP_PORT=8080
    APP_ENV=staging
    set -a; source app.env; set +a

    set -a exports every variable that is set, so programs started later get them.

  4. 4Use shared functions in a script

    cat /usr/local/lib/ops-lib.sh
    log() { echo "$(date +%F_%T) $*"; }
    cat deploy.sh
    #!/bin/bash
    source /usr/local/lib/ops-lib.sh
    log "deploy started"

    One library file, many scripts. Fix a function once.

  5. 5Activate a Python virtual environment

    source ~/venvs/ansible/bin/activate

    The activate script changes PATH in your shell, so it must be sourced, not run.

  6. 6Check what a script changes before you trust it

    bash -n setup.sh && source setup.sh

    bash -n checks the syntax without running anything.

  7. 7Load the environment of a service in a test

    set -a; source /etc/sysconfig/myapp; set +a

    Many services read their options from a file in /etc/sysconfig. Load it to test the same settings by hand.

  8. 8Pass arguments to the sourced file

    source ./set-cluster.sh production

    Inside the file, $1 is production.

  9. 9Make a change of folder stay

    cat goto-logs.sh
    cd /var/log/myapp
    source goto-logs.sh; pwd
    /var/log/myapp

    Run as ./goto-logs.sh, the cd would happen in a child shell and you would not move.

  10. 10Reload the shell profile for all users after a change (current shell)

    source /etc/profile

    Other users get it at their next login.

Watch out

  • source runs the file with your rights in your shell. Never source a file you do not trust.
  • An exit inside a sourced file closes your shell. Use return in files that are meant to be sourced.
  • source needs Bash (or another shell that has it). In plain sh scripts, use ..

Tips to remember

  • Run a script = new child shell. source = this shell.
  • source and . are the same.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man source