Dotfiles & Env Management — Terminal

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)…

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

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

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

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
$ git status --short
?? .env
$ echo ".env" >> .gitignore
$ git status --short
?? .gitignore

📌 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

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.

What you will learn