Lesson 17 / الدرس 17
Where a command comes from / من أين يأتي الأمر
You type php and something runs. Which something is decided by one variable, searched left to right — and that search explains almost every "wrong version" problem you will hit.
تكتب php فيعمل شيء. وأي شيء يقرره متغيّر واحد يُبحث فيه من اليسار لليمين — وذلك البحث يفسّر كل مشكلة "النسخة الخطأ" تقريبًا ستقابلها.
A command is usually a file somewhere on the disk. When you type a bare name with no path, the shell has to find that file, and it does so by walking through the directories listed in PATH in order, taking the first match and stopping. Not the newest, not the best — the first.
PATH بالترتيب، فتأخذ أول مطابقة وتتوقف. لا الأحدث ولا الأفضل — بل الأولى.$ echo $PATH
/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin
└──────┬──────┘ └──────┬─────┘ └──┬───┘
searched then this then this ...
first
php in the first directory wins over a php in the third, whatever their version numbers. php في المجلد الأول يغلب php في الثالث، أيًّا كانت أرقام نسختيهما.Asking which one you have
| Command | Answers |
|---|---|
| which php | The path of the one that would run |
| type php | What kind of thing it is: file, built-in, alias or function |
| command -v php | The same as which, but reliable in scripts |
| which -a php | Every match on PATH, in search order |
| php --version | What that one actually is |
which -a is the one that solves the mystery: it shows every candidate, so you can see the one that is winning and the one you wanted sitting behind it.
which -a هو الذي يحل اللغز: فهو يعرض كل مرشح، فترى الفائز والذي أردته جالسًا خلفه.$ which -a php
/opt/homebrew/bin/php ← 8.3, found first, so this is what runs
/usr/bin/php ← 8.1, shipped with the system, never reached
type tells you more than which.
type أكثر من which.Changing the order
To make a directory win, put it at the front: export PATH="/opt/php83/bin:$PATH". Read that carefully — the new directory comes first and the old value is appended after it, so nothing is lost. Putting it at the end instead means it is only reached when nothing earlier matched, which is usually not what you wanted.
export PATH="/opt/php83/bin:$PATH". واقرأ ذلك بعناية — فالمجلد الجديد يأتي أولًا والقيمة القديمة تُلحق بعده، فلا يضيع شيء. ووضعه في الآخر بدلًا من ذلك يعني ألا يُبلغ إلا حين لا يطابق شيء قبله، وهو ليس ما أردت عادةً.export PATH="/opt/php83/bin" دون :$PATH في آخره. فذلك يستبدل بالقائمة كلها بدل أن يضيف إليها، فيكفّ كل أمر كان يعيش في المجلدات التي أسقطتها عن أن يُوجد — بما فيه ls وgrep والمحرر الذي كنت ستتراجع به. والـshell ليست معطلة؛ بل لم تعد تجد شيئًا. وفتح طرفية جديدة يعيدك.
Note also that the current directory is not on PATH, deliberately. To run a program sitting in front of you, you must write ./deploy.sh — the ./ is not decoration. If the current directory were searched, anyone who could put a file named ls into a directory you visit could have you run their program by accident.
PATH، عمدًا. فلتشغيل برنامج أمامك يجب أن تكتب ./deploy.sh — و./ ليست زينة. فلو كان المجلد الحالي مبحوثًا فيه، لاستطاع كل من يضع ملفًا اسمه ls في مجلد تزوره أن يجعلك تشغّل برنامجه دون قصد.PATH أول ما تتحقق منه. فالمهمة المجدولة تعمل بـPATH أقصر بكثير من shell التفاعلية، ولهذا كانت النصيحة المعتادة للمهام المجدولة أن تكتب المسار المطلق الكامل لكل أمر.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. Two versions of a program are installed. Which one runs?
The search stops at the first match. Version numbers and install dates play no part, which is why which -a is the command that explains the surprise.
which -a هو الأمر الذي يفسّر المفاجأة.
2.
Why must you type ./script.sh rather than script.sh?
./script.sh لا script.sh؟If the current directory were searched, a file named ls dropped into a directory you visit would run instead of the real one.
ls وُضع في مجلد تزوره بدل الحقيقي.
3.
What is wrong with export PATH="/opt/bin"?
export PATH="/opt/bin"؟Adding means "/opt/bin:$PATH". Without the old value on the end, every other directory is gone from the search.
"/opt/bin:$PATH". فبدون القيمة القديمة في الآخر، تختفي كل المجلدات الأخرى من البحث.Score / النتيجة: 0 / 3
Your task / مهمتك
Print your own PATH and take it apart: list the directories in search order and say what lives in each. Then pick a command you use often and investigate it with which -a and type — report whether more than one copy exists, which one wins, and why.
اطبع PATH الخاص بك وفككه: عدّد المجلدات بترتيب البحث وقل ما الذي يعيش في كل واحد. ثم اختر أمرًا تستخدمه كثيرًا وافحصه بـwhich -a وtype — وأبلغ هل توجد أكثر من نسخة، وأيها يفوز، ولماذا.
- Your real PATH, split into its directories in order PATH الحقيقي مقسّمًا إلى مجلداته بالترتيب
- What each directory is for ما وظيفة كل مجلد
- One command investigated with which -a and type أمر واحد مفحوص بـwhich -a وtype
- Which copy wins, and why أي نسخة تفوز، ولماذا