الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

source

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

المستوى
امتحان RHCSA
المجموعة
الطرفية والمساعدة
يحتاج root
لا
الحزمة
bash (built-in)

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

source FILE [ARGUMENTS]
. FILE [ARGUMENTS]

الخيارات 2

الأكثر استخدامًا

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.

أمثلة 10

$ مستخدم عادي# بصلاحية root (أو مع 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.

انتبه

  • 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 ..

نصائح للحفظ

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

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man source