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 shellEdit thisFind out with
zsh — the macOS default~/.zshrcecho $SHELL
bash — most Linux~/.bashrcecho $SHELL
bash, on a login shell~/.bash_profileIt may source .bashrc
Either, for both~/.profileRead 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.

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"; }
Three kinds of thing: exported variables from lesson 16, aliases — a short name for a longer command — and functions, for when an alias cannot take an argument. Note the quoted "\$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.

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.

The mistake that locks you out

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.

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

Preview / المعاينة

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

1. You added a line to ~/.zshrc and nothing changed. Why?

2. Why source ~/.zshrc rather than running it as a script?

3. What is the risk of aliasing rm to rm -i?

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 سبب لكل مُدخَل، ودليل أنك اختبرته
How do you want to submit? / كيف تريد التسليم؟