Computing and the Command Line

Startup Files and the Prompt

Login and non-login shells, and which startup files each reads: /etc/profile, ~/.profile, /etc/bash.bashrc, and ~/.bashrc. Where to put PATH and EDITOR versus aliases, functions, and the prompt; applying changes with source; a tour of Ubuntu's default ~/.bashrc; history settings; and building your own prompt with PS1 escape codes and colors.

  • 6 min
  • 7 steps
  • 3 questions
  • Lesson 15 of 80

In this lesson

  1. Two kinds of shell
  2. What goes where
  3. Applying changes
  4. A tour of Ubuntu’s ~/.bashrc
  5. Your own prompt
  6. Your turn
  7. So

Two kinds of shell

When bash starts, it reads startup files: little scripts that set up your environment. Which ones depends on the kind of shell 1 2:

  • A login shell is one you logged in to: a text console, an SSH session, or the shell WSL opens for you. It reads /etc/profile, then the first one it finds of ~/.bash_profile, ~/.bash_login, and ~/.profile 2.
  • An interactive non-login shell is one started inside a session you’re already logged in to, like a new terminal window on a Linux desktop. It reads /etc/bash.bashrc and ~/.bashrc, and inherits environment variables from the session that started it 1 2.

Files under /etc apply to everyone; files in your home are yours to edit. Ubuntu gives each user a ~/.profile (no .bash_profile), and that .profile reads ~/.bashrc, so in practice both kinds of shell end up reading ~/.bashrc 1.

Left, a login shell, such as a console or SSH login or WSL's shell, reads /etc/profile for every user, then the first one found of ~/.bash_profile, ~/.bash_login, and ~/.profile; Ubuntu uses ~/.profile, which adds ~/bin and ~/.local/bin to PATH and then reads ~/.bashrc. Right, an interactive non-login shell, such as a new terminal window on a Linux desktop, reads /etc/bash.bashrc for every user and then ~/.bashrc, and inherits environment variables like PATH and EDITOR from the login session it was started from. Below, where to put things: ~/.profile for export PATH and export EDITOR; ~/.bashrc for aliases, functions, PS1, and shopt settings; and source ~/.bashrc to apply changes now.
Two kinds of shells, two sets of files, one place for each kind of setting. Credit: StudyCorner diagram · CC BY 4.0 · Source

What goes where

  • ~/.profile: environment variables, which every program inherits. PATH additions and export EDITOR=nano belong here 1.
  • ~/.bashrc: things for interactive use: aliases, functions, the prompt, history settings, shell options 1.

The reason for the split: environment variables need setting once per login and are inherited from there, while aliases and functions aren’t inherited, so every new shell has to read them.

Back up a startup file before editing it: cp ~/.bashrc ~/.bashrc.orig. A mistake in it shows up as errors every time a terminal opens, and the backup gets you out.

Quick check

Where’s the right place for export EDITOR=nano?

Applying changes

Startup files run when a shell starts. After editing one, either open a new terminal or load it into the current shell with source 1:

me@linuxbox:~$ nano ~/.bashrc
me@linuxbox:~$ source ~/.bashrc         # or the short form: . ~/.bashrc

source runs a file’s commands in your current shell, rather than in a new one, which is why it can change your aliases and variables.

Quick check

You added an alias to ~/.bashrc, but typing it says command not found. What did you forget?

A tour of Ubuntu’s ~/.bashrc

Open yours with less ~/.bashrc. Highlights of Ubuntu’s default:

HISTCONTROL=ignoreboth      # don't save duplicates or lines starting with a space
shopt -s histappend         # add to the history file instead of overwriting it
HISTSIZE=1000               # commands kept in memory
HISTFILESIZE=2000           # commands kept in ~/.bash_history
shopt -s checkwinsize       # keep line wrapping right when the window resizes

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

The last part matters: Ubuntu’s .bashrc reads ~/.bash_aliases if it exists. That’s the tidy place for your own aliases and functions; create the file and leave .bashrc itself alone. A leading space keeps a command out of history (that’s what ignoreboth includes), handy for commands with a password in them. HISTSIZE=10000 is a common, worthwhile change 2.

Your own prompt

The prompt is the variable PS1. Backslash codes in it are replaced each time the prompt is drawn 1:

Code Shows
\u user name
\h host name
\w current directory (~ for home)
\W just the last part of the directory
\t, \A time, 24-hour (with and without seconds)
\$ $, or # for root
\n a new line
\[ ... \] wraps codes that print nothing, like colors

Try one out in the current shell (it lasts until you close it) 1:

me@linuxbox:~$ PS1='\A \w\$ '
09:41 ~$ cd /etc
09:41 /etc$

Colors are escape sequences such as \e[1;32m (bright green) and \e[0m (back to normal). Wrap each in \[ and \] so bash knows they take no space on screen; otherwise long lines wrap wrongly 1:

PS1='\[\e[1;32m\]\u@\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\$ '

That’s roughly Ubuntu’s default: a green user@host, a blue directory. When you’ve got one you like, add the PS1= line to the end of ~/.bashrc.

Quick check

In PS1, what does \w show?

Your turn

Exercises

  1. Back up your .bashrc, then create ~/.bash_aliases with two aliases of your own and a mkcd function. Load it with source ~/.bashrc and test them.
  2. Add export EDITOR=nano to ~/.profile. How do you make it take effect?
  3. Raise your history to 10,000 commands in memory and 20,000 in the file.
  4. Design a prompt that shows the time and just the last part of the directory, then make it permanent.
  5. Find out whether your current shell is a login shell: run shopt login_shell.
Answers
  1. cp ~/.bashrc ~/.bashrc.orig, then put lines like alias up='sudo apt update && sudo apt upgrade' and mkcd() { mkdir -p "$1" && cd "$1"; } in ~/.bash_aliases, then source ~/.bashrc.
  2. Log out and back in (close and reopen WSL), or source ~/.profile in the current shell. New programs started from that shell then see EDITOR.
  3. In ~/.bashrc, change the lines to HISTSIZE=10000 and HISTFILESIZE=20000, then source ~/.bashrc.
  4. PS1='\A \W\$ ', then add that line to the end of ~/.bashrc.
  5. login_shell on in a WSL window or SSH session; off in a new desktop terminal window.

So

Login shells read /etc/profile and ~/.profile; interactive shells read ~/.bashrc, and on Ubuntu .profile reads .bashrc too. Put environment variables and PATH in ~/.profile, and aliases, functions, history settings, and the prompt in ~/.bashrc (or ~/.bash_aliases), then source the file to apply it.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Up next · 6 min

What a Process Is

Next lesson
Sources for this lesson
  1. 1
    William Shotts. The Linux Command Line, Seventh Internet Edition (25.12A). LinuxCommand.org (print edition by No Starch Press). 2026. verifiedFree CC BY-NC-ND 3.0 book, release 25.12A of July 18, 2026. Part 1, Learning the Shell: the shell and terminal emulators, prompts ($ vs. # for the superuser), command history (most distributions keep the last 1,000 commands), Shift-Ctrl-C/V for copy and paste; navigation and the directory tree; exploring the system (ls options and the long listing, file, less, the guided tour of /, symbolic links); manipulating files (wildcards and character classes, mkdir, cp, mv, rm, ln; no undelete, test wildcards with ls first); working with commands (four kinds of commands, type, which, help, --help, man and its sections, apropos, whatis, info, alias); redirection; expansion and quoting; Readline keyboard tricks, completion, history search; permissions; processes. Later parts cover the environment, vi, packages, storage, networking, find, archiving, regular expressions, text processing, and shell scripting.
  2. 2
    Chet Ramey, Brian Fox. Bash Reference Manual, Edition 5.3. GNU Project, Free Software Foundation. 2025. verifiedThe reference for Bash 5.3 (May 18, 2025). Redirections (3.6) are processed left to right and order matters: ls > dirlist 2>&1 sends both streams to dirlist, while ls 2>&1 > dirlist sends only standard output there. With set -o noclobber, > fails on an existing regular file and >| overrides it. &> word is equivalent to > word 2>&1 and &>> word to >> word 2>&1. Here documents (<<word, with <<- stripping leading tabs; quoting word disables expansion) and here strings (<<<). Expansions (3.5) happen in a fixed order: brace; tilde, parameter, arithmetic, and command substitution left to right; word splitting; filename expansion; quote removal last. Startup files (6.2): an interactive login shell reads /etc/profile then the first of ~/.bash_profile, ~/.bash_login, ~/.profile; an interactive non-login shell reads ~/.bashrc. HISTCONTROL (ignorespace, ignoredups, ignoreboth), HISTSIZE, HISTFILESIZE; set -x traces expanded commands; shell functions and variables, export.