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
A variable is read with a \$ 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.

The ones already set for you

VariableHoldsWhere it shows up
HOMEYour home directoryWhat ~ expands to
PATHWhere to look for commandsLesson 17, entirely
USERYour usernamePrompts and log lines
SHELLWhich shell you are runningThe answer to lesson 2's task
PWDThe current directoryWhat pwd prints
LANGYour language and character setWhether Arabic displays correctly
EDITORWhich editor other programs should opengit, 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.

$ GREETING=hello
$ echo $GREETING
hello
$ bash -c 'echo $GREETING'
                          ← empty. The new shell never received it.
$ export GREETING
$ bash -c 'echo $GREETING'
hello
The demonstration of what 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.

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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What does export add to setting a variable?

2. A script sets a variable and exits. Why is it not set in your shell?

3. Why is export DB_PASS=secret a poor way to set a password?

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 جلسة حقيقية تُظهر الضبط في مقابل التصدير
How do you want to submit? / كيف تريد التسليم؟