Loading
0x130Lesson 20 of 28

Make the shell yours: dotfiles

Learn which startup files run when, and keep your aliases, PATH and prompt.

22 min 6-question quiz 1 code exercise
By the end of this lesson you can
  • Explain what .bashrc and .bash_profile do, and when each one runs
  • Add aliases, PATH entries and functions that survive a restart
  • Customize the prompt, and keep your dotfiles in Git

Your alias vanished when you closed the terminal, because it only lived in that shell. To keep something, put it in a startup file. These are called dotfiles because their names start with a dot, which hides them from a plain ls.

Which file runs when?

  • ~/.bashrc runs for each new interactive shell, like a new terminal tab. Aliases, functions and your prompt go here.
  • ~/.bash_profile (or ~/.profile) runs for login shells, such as when you connect over SSH. Environment variables go here. Most people make it load .bashrc too.
  • Scripts run neither, which is why an alias that works at your prompt fails inside a script.

A change doesn’t affect shells that are already open. To apply it right now, run source ~/.bashrc, which runs the file in your current shell.

macOS note: its default shell is now zsh, so the equivalent file is ~/.zshrc.

Try it

Up to 15 XP

Set up your workstation

Set up your shell on Kestrel. Remember that >> adds to a file, and > would wipe your whole config.

Mission 1 of 4

Find your dotfiles

Ada: “Your config is hidden. List it, and tell me which editor is the default.”

Simulated shell. Try ls, or type help to see the commands.

Up arrow: history. Tab: complete. Ctrl-L: clear. Ctrl-C: cancel.
  1. Find your dotfiles
  2. Keep an alias
  3. Load it now
  4. Extend your PATH

Keep your config safe

Many engineers keep their dotfiles in a Git repository and link them into place, so a new machine feels like home in a minute. Treat that repository as public, even if it’s private: API keys and tokens don’t belong in dotfiles. Load those from a secrets manager or a separate, ignored file.

Try it

Up to 15 XP

Which file does this belong in?

Decide where each setting should go.

0 of 4 sortedScore 0/0
  • “alias gs="git status"”

  • “A custom PS1 prompt”

  • “export PATH="$HOME/bin:$PATH", set once at login”

  • “export API_TOKEN=abc123”

Key takeaways

  • .bashrc is for interactive shells, and .bash_profile is for logins.

  • Scripts read neither.

  • source applies a change to your current shell.

  • Keep dotfiles in Git, and keep secrets out of them.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Bash scripts

Write a script in the editor and run it for real against sample input. Each run gets a fresh Linux sandbox with Bash 5.2 and the GNU tools on Wandbox, a free public service - so experiment freely, even with rm. Your script and test input are sent there.

Exercise 1

Read the PATH

+25 XP

stdin is a PATH value. Print each folder on its own line, numbered from 1 like 1: /usr/bin. Then print Search order is top to bottom.

  • Three folders
  • One folder
script.sh
Loading editor…

Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…