Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

source

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

Niveau
RHCSA-examen
Groep
Shell en hulp
Root nodig
Nee
Pakket
bash (built-in)

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

source FILE [ARGUMENTS]
. FILE [ARGUMENTS]

Opties 2

Meest gebruikt

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.

Voorbeelden 10

$ als gewone gebruiker# als root (of met 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.

Let op

  • 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 om te onthouden

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

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man source