Lesson 16 / الدرس 16
Environment variables / متغيّرات البيئة
Every program you start inherits a set of named values from the shell. They are how a program is told where things are, which language you read, and — badly — where the passwords are.
كل برنامج تشغّله يرث مجموعة من القيم المسمّاة من الـshell. وبها يُخبَر البرنامج أين الأشياء، وبأي لغة تقرأ، و— على نحو سيئ — أين كلمات المرور.
The environment is a list of name-and-value pairs that the shell hands to every program it starts. The program can read them but changing them affects only its own copy — a child cannot alter its parent's environment, which is a rule worth remembering when a script seems to have no effect.
echo $HOME print one variable
env print all of them
env | sort | less all of them, readably
GREETING=hello set one, for this shell only
export GREETING=hello set one, and pass it to programs you start
unset GREETING remove it
\$ in front of it and set without one. The difference between the fourth and fifth lines is the whole lesson: without export, the value exists in your shell and no program you run can see it. \$ أمامه ويُضبط بدونها. والفرق بين السطرين الرابع والخامس هو الدرس كله: فبدون export تكون القيمة موجودة في shell ولا يراها أي برنامج تشغّله.The ones already set for you
| Variable | Holds | Where it shows up |
|---|---|---|
| HOME | Your home directory | What ~ expands to |
| PATH | Where to look for commands | Lesson 17, entirely |
| USER | Your username | Prompts and log lines |
| SHELL | Which shell you are running | The answer to lesson 2's task |
| PWD | The current directory | What pwd prints |
| LANG | Your language and character set | Whether Arabic displays correctly |
| EDITOR | Which editor other programs should open | git, crontab, and many more |
EDITOR is the one people set and then forget they set. When git opens an editor you did not expect, this variable chose it.
EDITOR هو الذي يضبطه الناس ثم ينسون أنهم ضبطوه. فحين يفتح git محررًا لم تتوقعه، فهذا المتغيّر اختاره.$ GREETING=hello
$ echo $GREETING
hello
$ bash -c 'echo $GREETING'
← empty. The new shell never received it.
$ export GREETING
$ bash -c 'echo $GREETING'
hello
export means, and it is worth typing yourself. Run the panel for the rule in one sentence — this is the cause of a whole category of "but I set that!" confusion.
export، ويستحق أن تكتبه بنفسك. شغّل اللوحة للقاعدة في جملة — فهذا سبب صنف كامل من حيرة "لكنني ضبطته!".Configuration without editing code
This is what environment variables are for in practice. The same application runs on your laptop against a local database and on the server against the real one, with no difference in the code — the connection details come from the environment. It is why a password should never be written into a file you commit, and why a .env file from lesson 11 sits beside the code rather than inside it.
.env من الدرس 11 بجانب الشيفرة لا بداخلها.export DB_PASS=secret — تُحفظ في سجل أوامرك نصًا عاديًا، وتبقى فيه شهورًا. ضع الأسرار في ملف بصلاحية 600 وحمّلها منه.LANG=C ls ينفّذ ls بتلك اللغة ويترك shell دون مساس. وهذه أنظف طريقة لتجربة شيء دون تغيير جلستك، وهي الصيغة التي ستراها أكثر في التوثيق.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What does export add to setting a variable?
export إلى ضبط متغيّر؟Without it the value exists only in your shell and no program you run can see it. It is not permanent either way — lesson 18 covers making it survive a new terminal.
2. A script sets a variable and exits. Why is it not set in your shell?
The environment is inherited downwards only. The script changed its own copy, and its copy disappeared when it exited.
3.
Why is export DB_PASS=secret a poor way to set a password?
export DB_PASS=secret طريقة سيئة لضبط كلمة مرور؟The history file keeps it for months, and the whole environment is passed to every child process. A file with permission 600 is the safer place.
Score / النتيجة: 0 / 3
Your task / مهمتك
Print your own environment and report on it: pick six variables that are genuinely set on your machine, say what each holds and what would break if it were wrong. Then demonstrate the difference between setting and exporting, with a real session showing a variable invisible to a program before export and visible after.
اطبع بيئتك أنت وأبلغ عنها: اختر ستة متغيّرات مضبوطة فعلًا على جهازك، وقل ما يحمله كل واحد وما الذي سينكسر لو كان خطأً. ثم اعرض الفرق بين الضبط والتصدير، بجلسة حقيقية تُظهر متغيّرًا خفيًا عن برنامج قبل export وظاهرًا بعده.
- Six real variables from your own environment ستة متغيّرات حقيقية من بيئتك
- What each holds and what would break if wrong ما يحمله كل واحد وما ينكسر لو كان خطأً
- A real session showing set against exported جلسة حقيقية تُظهر الضبط في مقابل التصدير