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

export, unset

Make a shell variable available to the programs you start (export), or remove a variable (unset).

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

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

ماذا يفعل

A variable you set in Bash (NAME=value) is only known to that shell. export NAME puts it in the environment, so every program and script started from this shell gets a copy.

unset NAME removes a variable (or a function with unset -f).

To keep a variable for future logins, write the export line in ~/.bashrc (for you) or in a file in /etc/profile.d/ (for everybody).

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

export [-fn] [NAME[=VALUE] ...]
export -p
unset [-fv] NAME [NAME ...]

الخيارات 6

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

NAME=VALUE
Set the value and export it in one step.
-p
List all exported variables (as export lines).
-n
Remove the export mark: the variable stays in the shell but is no longer passed on.
-f
The names are shell functions (export a function to child shells).

unset

-v
The names are variables (the default).
-f
The names are functions.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Set and export a variable in one step

    export APP_ENV=staging
    bash -c 'echo $APP_ENV'
    staging

    The new shell (a child program) sees the value.

  2. 2Add a folder to the PATH

    export PATH="$PATH:$HOME/bin"

    Keep the old PATH and add your folder at the end. Put the line in ~/.bashrc to keep it.

  3. 3Keep a variable for every login

    echo 'export EDITOR=vim' >> ~/.bashrc

    Tools like crontab -e, visudo and git use the EDITOR variable.

  4. 4Set a variable for all users

    echo 'export HISTTIMEFORMAT="%F %T "' > /etc/profile.d/history-time.sh

    Every user gets times in history after the next login.

  5. 5Use a proxy for the tools in this shell

    export http_proxy=http://proxy.example.com:3128 https_proxy=http://proxy.example.com:3128

    curl, dnf and pip read these variables.

  6. 6Remove a variable

    unset http_proxy https_proxy

    Turns the proxy off for this shell again.

  7. 7List the exported variables

    export -p | head

    Same information as env, but in a form you could paste back into a shell.

  8. 8Stop exporting a variable but keep it

    export -n APP_ENV

    The value stays in this shell; new programs no longer get it.

  9. 9Export a function to a child script

    log() { echo "$(date +%T) $*"; }
    export -f log
    bash -c 'log "hello from child"'

    Child Bash processes can use the function.

  10. 10Pass settings to a container or a tool without editing files

    export KUBECONFIG=$HOME/.kube/staging.yaml
    kubectl get nodes

    One variable switches the cluster for this terminal only.

  11. 11Remove a secret from the shell after use

    export DB_PASSWORD='s3cret'; ./migrate.sh; unset DB_PASSWORD

    Shorter life for the secret in the shell. Better: let the tool read it from a protected file.

انتبه

  • No spaces around =: export A = 1 is wrong.
  • A child can never change the variables of its parent. A script that exports a variable does not change your shell, unless you run it with source.
  • unset PATH breaks the shell until you set PATH again.

الملفات

~/.bashrc
Your exported variables for every shell.
/etc/profile.d/*.sh
Exported variables for all users.

نصائح للحفظ

  • Set: NAME=value. Pass on: export NAME. Both: export NAME=value. Remove: unset NAME.

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

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

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