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

env

Show the environment variables, or run a program with a changed environment.

المستوى
امتحان RHCSA
المجموعة
الطرفية والمساعدة
يحتاج root
لا
الحزمة
coreutils

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

ماذا يفعل

Without arguments, env prints all environment variables, one NAME=value per line. These are the variables that programs started from your shell receive.

With a command after it, env runs that command with extra, removed or no variables at all. The main shell does not change.

You also see env in the first line of scripts: #!/usr/bin/env python3 finds python3 through $PATH, so the script works on systems where the program is in another folder.

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

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

الخيارات 13

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

-i, --ignore-environment
Start with an empty environment (only the variables you give).
-u, --unset=name
Remove this variable from the environment of the command.
-C, --chdir=dir
Change to this folder before the command runs.
-S, --split-string=text
Split TEXT into separate arguments (used in shebang lines with options).
-0, --null
End each output line with a NUL byte instead of a new line.
-v, --debug
Print what env does, step by step.
--block-signal[=<sig>]
Block these signals for the command.
--default-signal[=<sig>]
Reset these signals to the default for the command.
--ignore-signal[=<sig>]
Make the command ignore these signals.
--list-signal-handling
Print the non-default signal handling to stderr.
-
Same as -i.
--help
Show help.
--version
Show the version.

أمثلة 11

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

  1. 1Show all environment variables

    env

    Sorted output is easier to read: env | sort.

  2. 2Find one variable

    env | grep -i proxy

    Shows HTTP_PROXY, https_proxy and others. Proxy variables often explain why curl or dnf fails.

  3. 3Run one command with an extra variable

    env LANG=C df -h

    The command runs in English with standard formats. Good for scripts that read command output.

  4. 4Run a command without a variable

    env -u HTTP_PROXY -u HTTPS_PROXY curl -I https://intranet.example.com

    Skip the proxy for one internal test without changing your shell.

  5. 5Run a program in a clean environment

    env -i PATH=/usr/bin:/bin HOME=/tmp bash -c 'env'

    Shows what a program sees with almost no variables. Close to what cron and systemd give a job.

  6. 6Use env in the first line of a script

    head -1 deploy.py
    #!/usr/bin/env python3

    The script uses the first python3 in $PATH, for example from a virtual environment.

  7. 7Pass options to the interpreter in a shebang

    head -1 tool.sh
    #!/usr/bin/env -S bash -euo pipefail

    -S splits the text into several arguments. Without it, the line does not work on Linux.

  8. 8Compare the environment of root and your user

    env | sort > /tmp/me.txt; sudo env | sort > /tmp/root.txt; diff /tmp/me.txt /tmp/root.txt

    sudo resets most variables. This shows what changes.

  9. 9Check the variables of a running process

    tr '\0' '\n' < /proc/$(pgrep -o sshd)/environ

    Each process keeps the environment it started with. Good to debug a service.

  10. 10Run a command in another folder

    env -C /var/www/html ls -l

    The shell stays in its own folder.

  11. 11Print variables safely for a script (NUL separated)

    env -0 | sort -z | tr '\0' '\n' | head

    NUL separators are safe even when a value contains a new line.

انتبه

  • env shows only exported variables. A plain shell variable (X=1 without export) is not in the list; use set for all.
  • Secrets in environment variables can be read by root and by tools that dump the environment. Prefer files with tight permissions.
  • env NAME=value changes the variable only for that one command.

الملفات

/proc/PID/environ
The environment of a running process (NUL separated).
/etc/environment
Variables set for all users at login (simple NAME=value lines).

نصائح للحفظ

  • env = show, env NAME=val cmd = one command with a change, env -i = clean start.

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

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

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