Lesson 18 / الدرس 18
Startup files: making it stick / ملفات البدء: جعل الإعداد يدوم
Everything you set in a terminal disappears when you close it. One hidden file in your home directory is read at the start of every new shell, and that is where a setting goes to survive.
كل ما تضبطه في طرفية يختفي حين تغلقها. وملف مخفي واحد في مجلد منزلك يُقرأ عند بدء كل shell جديدة، وهناك يذهب الإعداد لينجو.
A shell reads a file of commands when it starts, and runs every line in it as if you had typed them. Which file depends on which shell you are running — the one thing you must get right before editing anything.
| Your shell | Edit this | Find out with |
|---|---|---|
| zsh — the macOS default | ~/.zshrc | echo $SHELL |
| bash — most Linux | ~/.bashrc | echo $SHELL |
| bash, on a login shell | ~/.bash_profile | It may source .bashrc |
| Either, for both | ~/.profile | Read by many shells |
Editing the wrong one is the commonest cause of "I added it and nothing happened". Check echo $SHELL first — this is the same question lesson 2 asked you to answer.
echo $SHELL أولًا — وهو السؤال نفسه الذي طلب منك الدرس 2 الإجابة عنه.What to put in it
# ~/.zshrc
export PATH="/opt/homebrew/bin:$PATH"
export EDITOR="nano"
alias ll='ls -lah'
alias gs='git status'
alias ..='cd ..'
mkcd() { mkdir -p "$1" && cd "$1"; }
"\$1" in the function: that is lesson 15's rule, and it is what makes mkcd "my project" work. Run the demonstration for the ones worth having.
"\$1" المقتبس في الدالة: فتلك قاعدة الدرس 15، وهي ما يجعل mkcd "my project" يعمل. شغّل العرض للجديرة بالاقتناء.
A change takes effect in new terminals only, because the file is read at startup. To apply it to the shell you are already in, run source ~/.zshrc — that reads the file into the current shell rather than starting a new one. Running it as a script would not work, for exactly the reason lesson 16 gave: a child cannot change its parent's environment.
source ~/.zshrc — فذلك يقرأ الملف داخل الـshell الحالية بدل بدء واحدة جديدة. أما تشغيله نصًا برمجيًا فلن يعمل، للسبب نفسه الذي أعطاه الدرس 16: فلا يستطيع الابن تغيير بيئة أبيه.The mistake that locks you out
PATH المكتوب خطأً هو المتهم المعتاد — فكما بيّن الدرس 17، قد يترك كل شيء غير قابل للإيجاد. وحمايتان: اختبر دائمًا بـsource ~/.zshrc في shell مفتوحة عندك قبل إغلاقها، واحتفظ بنسخة — cp ~/.zshrc ~/.zshrc.bak — قبل أي تعديل جوهري. وإن أُقفل عليك، فمعظم الطرفيات تستطيع بدء shell بـzsh -f، وهو يتخطى ملف البدء كله.
One alias deserves a second thought before you add it. Aliasing rm to rm -i feels safe, and it is — on your own machine. The danger is that you stop reading rm as dangerous, and then one day you are on a server where the alias does not exist. A habit that only protects you on one machine is a habit that will let you down somewhere else.
rm كنية لـrm -i يبدو آمنًا، وهو كذلك — على جهازك أنت. والخطر أن تكفّ عن قراءة rm بوصفه خطرًا، ثم تكون يومًا على خادم لا وجود للكنية فيه. والعادة التي لا تحميك إلا على جهاز واحد عادة ستخذلك في مكان آخر..zshrc و.gitconfig وسواهما يعني أن جهازًا جديدًا يُجهَّز بأمر واحد، وأن لكل تغيير في بيئتك تاريخًا تقرؤه وتتراجع عنه. ويسمّي الناس هذه dotfiles، وهي أفضل عادة في هذا الدرس.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
You added a line to ~/.zshrc and nothing changed. Why?
~/.zshrc ولم يتغير شيء. لماذا؟The shell you are in already read the file, before your edit existed. source reads it again into the current shell.
source يقرؤه مرة أخرى داخل الـshell الحالية.
2.
Why source ~/.zshrc rather than running it as a script?
source ~/.zshrc لا تشغيله نصًا برمجيًا؟It is lesson 16's rule again. source runs the lines in your current shell, which is the only way the changes can stay.
source ينفّذ الأسطر في shell الحالية، وهي الطريقة الوحيدة لبقاء التغييرات.
3.
What is the risk of aliasing rm to rm -i?
rm كنية لـrm -i؟The protection is real but local. A habit that only works on one machine is one you will carry to a server where it does not.
Score / النتيجة: 0 / 3
Your task / مهمتك
Set up your own startup file properly. Add at least one exported variable, three aliases you will genuinely use, and one function that takes an argument. Take a backup first, test with source, and present the file as a page with a line explaining why each entry earns its place. Say which file you edited and how you knew it was the right one.
جهّز ملف بدئك كما ينبغي. أضف متغيّرًا مصدَّرًا واحدًا على الأقل، وثلاث كُنى ستستخدمها فعلًا، ودالة واحدة تأخذ وسيطًا. وخذ نسخة احتياطية أولًا، واختبر بـsource، واعرض الملف صفحةً مع سطر يشرح لماذا يستحق كل مُدخَل مكانه. وقل أي ملف عدّلت وكيف عرفت أنه الصحيح.
- The right file for your shell, with how you knew الملف الصحيح لـshell عندك، مع كيف عرفت
- One exported variable, three aliases, one function متغيّر مصدَّر، وثلاث كُنى، ودالة
- The function quotes its argument الدالة تقتبس وسيطها
- A reason for each entry, and proof you tested it سبب لكل مُدخَل، ودليل أنك اختبرته