An alias you type directly into the terminal is gone as soon as the window closes. To still have it tomorrow, you have to write the alias in a file the shell reads every time a terminal opens. These settings files are called dotfiles, because their names start with a dot. Most of them are in your home folder (~): ~/.zshrc, ~/.bashrc, ~/.gitconfig. The dot in front of the name hides the file from a plain ls. Picture a shopkeeper who reads a morning checklist every time the shop opens: switch on the lights, place the price board outside. The shopkeeper is the shell, the checklist is ~/.zshrc, and the shop opening is a new terminal opening. A task you add to the list at noon only gets done the next morning, unless someone reads the list again right now. In the terminal, the command that reads that list again is source, explained below.
Dotfiles you'll meet often
| File | What it holds |
|---|---|
~/.zshrc |
settings for zsh, the macOS default shell: aliases, functions, exports |
~/.bashrc |
the same, for bash, the default shell on most Linux |
~/.gitconfig |
your name, email and aliases for git |
~/.ssh/config |
a list of SSH servers with their user and key |
.env (in a project folder) |
passwords, API keys and settings for one project |
.gitignore (in a project folder) |
the files that must not go into git |
lshides names that start with a dot.ls -a(a = all) shows them.- If you are not sure which shell you have, run
echo $SHELL. If it says/bin/zsh, edit~/.zshrc. If it says/bin/bash, edit~/.bashrc. - Note that
$SHELLshows the default shell of your account, not the shell that is open now. If you start bash inside zsh by typingbash,$SHELLis still/bin/zsh. To see the shell that is running now, useecho $0: it prints, for example,bash,zshor-zsh(the-means a login shell, explained below).
What's in ~/.zshrc: four kinds of line
This is an example ~/.zshrc, with one line of each kind:
# ~/.zshrc: read every time a new zsh terminal opens
alias gs='git status'
export EDITOR=vim
source ~/.oh-my-zsh/oh-my-zsh.sh
- comment: a line starting with
#. It is skipped, it's only a note. - alias:
alias name='command', a command abbreviation as in the Shell Functions & Aliases lesson. - export:
export NAME=valuemakes an environment variable, which is passed to every program started from that terminal.EDITORtells programs like git which editor to open when you need to write something. Here the editor is vim, a text editor that runs in the terminal. - source:
source fileruns another file's contents in this shell. In this example the file belongs to Oh My Zsh, a collection of settings and plugins (extra features) for zsh. If that file doesn't exist, zsh shows an error every time a terminal opens.
In the last three kinds, the first word (alias, export, source) is always followed by a space. So alias gs=... is an alias line, but aliases=3 is not: that line sets a variable called aliases. Any other line, like PATH=/usr/bin without export, sets a variable in that shell. A new variable set without export is not passed to other programs. But PATH is already exported when the terminal opens, so its new value is passed on.
A terminal that's already open doesn't know yet: source
This example uses a zsh terminal, in a ~/shop-project folder that holds a git repo with no new changes. The first line adds alias gs='git status' to ~/.zshrc. Guess first: does gs work right away in the same terminal?
$ echo "alias gs='git status'" >> ~/.zshrc
$ gs
zsh: command not found: gs
$ source ~/.zshrc
$ gs
On branch main
nothing to commit, working tree clean
>>adds one line at the end of the file, as in the Piping lesson.- zsh only reads
~/.zshrconce, when the terminal opens. This terminal opened before you edited that file, so this terminal doesn't know the aliasgsyet. source ~/.zshrcreads that file again and runs it in this terminal. The short form is. ~/.zshrc. Opening a new terminal tab works too, because a new tab reads the file from the start.
The same is true for export:
$ echo 'export EDITOR=vim' >> ~/.zshrc
$ source ~/.zshrc
$ echo $EDITOR
vim
📌 Why ~/.bashrc is sometimes not read
One kind of shell reads different files: a login shell, a shell that is opened as if you had just logged in (signed in to your account). The terminal on macOS opens a login shell, so zsh reads ~/.zprofile and then ~/.zshrc. A terminal on Linux usually opens a plain bash, which reads ~/.bashrc. But when you log into a server over SSH, bash is a login shell and reads ~/.bash_profile (or ~/.profile), not ~/.bashrc. On Ubuntu this problem is already solved: Ubuntu's default ~/.profile loads ~/.bashrc too. If your server doesn't do this, add this line to ~/.bash_profile:
[ -f ~/.bashrc ] && source ~/.bashrc
It means: if the file ~/.bashrc exists (-f), run its contents with source.
.env: secrets for one project
An example .env:
# .env in the shop-project folder
DB_HOST=localhost
DB_PASS=secret123
API_KEY=sk-abc123
.envholds passwords and API keys, so don't commit it to Git. If the repo is public, or is opened to other people one day, anyone can read what is inside.- Put
.envin.gitignorebefore the first commit:
$ git status --short
?? .env
$ echo ".env" >> .gitignore
$ git status --short
?? .gitignore
??means git isn't tracking that file yet (it has never recorded it). Once.envis in.gitignore, git stops showing it, sogit add .won't put it into a commit.- Commit a
.env.exampleinstead: the same variable names, with empty or sample values, so teammates know what to fill in. - Frameworks (structures for building apps) like Laravel or Vite read
.envthemselves. The shell doesn't load it automatically.
📌 Keep your dotfiles in Git
Many developers keep their dotfiles in a repo of their own, for example ~/dotfiles. Then they link them into the home folder with a symlink (a pointer file that leads to another file): ln -s ~/dotfiles/.zshrc ~/.zshrc. On a new laptop you only need to clone that repo (copy it to the laptop) and make the symlinks, and every alias and setting is there. Never put .env or SSH keys in that repo.
⚠️ Edits that have no effect, and an .env that leaks
- Editing
~/.zshrcand forgettingsource. You getzsh: command not foundin the terminal that's already open. - Editing the wrong file. You use zsh, but write in
~/.bashrc(or the other way round). Check first withecho $SHELL. - Spaces around
=.export EDITOR = vimsets nothing: zsh answerszsh: bad assignment, and bash answersbash: export: `=': not a valid identifier. .envalready committed. Adding it to.gitignoreafterwards doesn't remove it from the Git history.git rm --cached .envmakes git stop tracking the file, but its contents are still in the old commits. So change the passwords and API keys.
TL;DR: Dotfiles are settings files whose names start with a dot, like ~/.bashrc, ~/.zshrc and ~/.gitconfig. They are hidden from a plain ls (use ls -a to see them). The shell reads ~/.zshrc (zsh) or ~/.bashrc (bash) once, when the terminal opens. So after editing it, run source ~/.zshrc to read it again, or open a new tab. The file holds four kinds of line: comment (#), alias, export NAME=value, and source file, with no spaces around =. .env keeps a project's secrets, such as passwords. Put its name in .gitignore so it stays out of Git, and commit a .env.example (only the variable names, without the secret values) instead. If .env was already committed, change the secrets.
⚡ Settings files with a dot
Guess first: why doesn't ~/.zshrc appear when you run ls in your home folder?
Its name starts with a dot, and a plain ls hides names that start with a dot. ls -a shows them.
Meaning: Dotfiles are settings files in the home folder whose names start with a dot, like ~/.bashrc, ~/.zshrc and ~/.gitconfig.
⚡ Four kinds of line
Write:
# read every time a new zsh terminal opens
alias gs='git status'
export EDITOR=vim
source ~/.oh-my-zsh/oh-my-zsh.sh
Meaning: A ~/.zshrc usually holds comments (#, notes that are skipped), aliases (command abbreviations), exports (they make environment variables) and sources (they run another file's contents). No spaces around =.
⚡ Edit, then source
Write:
source ~/.zshrc
Meaning: The shell reads ~/.zshrc only once, when the terminal opens. After you edit it, source reads the file again in the same terminal. Opening a new tab works too.
⚡ Don't commit .env
Write:
echo ".env" >> .gitignore
Meaning: .env holds a project's passwords and API keys. Put its name in .gitignore (the list of files that must not go into git) before the first commit, and commit a .env.example instead: the same variable names, without the secret values.