Read

Module 7 · The system, disks and the environment

The shell startup files and PATH

Where your environment comes from at login (/etc/profile, ~/.profile, $ENV), how to make a setting permanent and test it, how PATH decides which program runs, and an exam over the whole module.

What you will learn

  • Know which startup files a login shell and an interactive ash shell read, and in what order.
  • Make a setting permanent in `~/.profile` and test it with `.` and `sh -l`.
  • Explain how PATH is searched, and inspect it with `which`, `command -v` and `type`.
  • Combine the module's tools (uname, /proc, du, hostname, date, env) in practical tasks.

Lesson 69 left a question open: if variables die with the shell, who sets PATH, HOME and PS1 every time you log in? The answer is a handful of plain shell scripts that the shell runs on itself at startup. Which ones it reads depends on what kind of shell it is.

Login shells and /etc/profile

A login shell is the first shell of a session: the one started by login, by SSH, or here by init on each console. You can spot it by the dash in front of its name: echo $0 prints -/bin/sh. A login shell first runs /etc/profile, the system-wide file the administrator controls, and then ~/.profile, your personal one, if it exists. An interactive shell additionally runs the file named in the variable ENV, if it is set (ash's equivalent of bash's ~/.bashrc). A non-interactive shell, such as one running a script, reads none of them: scripts inherit the environment and nothing else.

~% echo $0
-/bin/sh
~% cat /etc/profile
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
...
export PAGER='/bin/more '
export EDITOR='/bin/vi'

# Source configuration files from /etc/profile.d
for i in /etc/profile.d/*.sh ; do
	if [ -r "$i" ]; then
		. $i
	fi
	unset i
done
...
PS1='\W% '
HOME='/root/'
cd ~

The lab's /etc/profile is a good specimen. It exports PATH, PAGER and EDITOR, runs every *.sh file in /etc/profile.d/ (a drop-in directory, so packages can add settings without editing the main file), mounts filesystems, and finally sets the \W% prompt and the HOME with a trailing slash that you met in lesson 69.

FileRead byUse it for
/etc/profileEvery login shellSystem-wide defaults (root edits it)
/etc/profile.d/*.shSourced by /etc/profileDrop-in snippets
~/.profileYour login shellsYour PATH, exports, EDITOR…
$ENV (e.g. ~/.ashrc)Interactive ash shellsAliases, prompt tweaks
~/.bashrc, ~/.bash_profilebash on full distributionsThe same ideas, bash's names

Testing a change

~% echo 'export LAB70=yes' >> ~/.profile
~% echo "[$LAB70]"
[]
~% sh -l -c 'echo "login shell: [$LAB70]"'
login shell: [yes]
~% . ~/.profile
~% echo "[$LAB70]"
[yes]

Editing ~/.profile changes nothing in the shell you are typing in: it is only read at startup. Two ways to try it. . FILE (or source FILE) *sources* the file: runs it inside the current shell, so its exports and cds stick. sh -l starts a fresh login shell that reads the startup files from scratch, the honest test of what your next login will see. Do not run it as sh ~/.profile: that executes it in a child shell, and every setting dies with the child. And be careful with /etc/profile: a syntax error there breaks every login, so test with sh -l before you close your working session.

PATH: how a name becomes a program

~% echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin
~% which ls
/bin/ls
~% type cd
cd is a shell builtin
~% command -v ls
/bin/ls
~% sysinfo
-/bin/sh: sysinfo: not found
~% PATH="$PATH:/root/bin"
~% sysinfo
sysinfo-ok

When you type a name without a slash, the shell first checks its builtins and functions, then tries each directory of PATH, left to right, and runs the first match; exit status 127 means none matched. A name containing a slash, such as ./script or /bin/ls, skips the search entirely. The current directory is deliberately *not* in PATH, so a malicious file called ls in some folder never runs by accident. which prints the file PATH would find, command -v does the same and also knows builtins, and type says what the name is. To add your own directory, append it with PATH="$PATH:/root/bin" (appending lets system commands keep priority) and put that line, with export, in ~/.profile to keep it. Lesson 93 builds on this to install your own commands.

Module review

QuestionTool
Which kernel, which architecture, what name?uname -r, uname -m, hostname (61)
Up for how long, how loaded?uptime, /proc/loadavg (61)
How much memory is really free?free -m, /proc/meminfo (62)
Which filesystem is full? What is filling it?df -h (63), du -sh (64)
Attach or detach a filesystem; which disks exist?mount, umount (65), blkid, fdisk -l (66)
What did the kernel complain about?dmesg | tail (67)
What time is it, and is it right?date, hwclock (68)
What do my programs inherit?env, export, ~/.profile, PATH (69-70)

Commands in this lesson

CommandWhat it does
echo $0A leading dash (`-/bin/sh`) means a login shell.
cat /etc/profileThe system-wide login script.
echo 'export VAR=x' >> ~/.profileMake a setting permanent for your logins.
. ~/.profileSource it: apply it to the current shell now.
sh -lStart a fresh login shell to test startup files.
PATH="$PATH:/root/bin"Append a directory to the command search path.
which NAME / command -v NAME / type NAMEWhere a command is found, or what it is.

Quiz

  1. Which uname option prints the machine architecture, such as i686?

    • -r
    • -m
    • -n
    • -s
  2. `free` shows little free memory but a lot of cached memory. Is the machine short of RAM?

    • Yes, add swap now
    • Not necessarily: cache is given back as soon as programs need it
    • Yes, cache is memory lost to the kernel
    • Only if uptime is high
  3. `df` says a filesystem is 95 % full. Which command finds what is using the space?

    • df -i
    • du -sh on its directories
    • mount
    • blkid
  4. How do you mount a filesystem image stored in a regular file?

    • mount disk.img /mnt -t file
    • mount -o loop disk.img /mnt
    • blkid -m disk.img
    • cp disk.img /dev/sda
  5. Where do you look first after plugging in a device that does not appear?

    • dmesg | tail
    • env
    • date
    • uptime
  6. You add `export EDITOR=vi` to `~/.profile`. When does your current shell see it?

    • Immediately
    • After `. ~/.profile`, or in the next login shell
    • After `sh ~/.profile`
    • Never
  7. PATH is `/root/bin:/bin`, and both directories contain an `ls`. Which one runs when you type `ls`?

    • /bin/ls, system commands win
    • /root/bin/ls, the first match in PATH order
    • Both, one after the other
    • Neither: it is ambiguous
  8. After `date -s` set a wrong time, which command restores it from the hardware clock?

    • hwclock -w
    • hwclock -s
    • date -u
    • uptime -s

Practice

  1. An executable `/root/bin/sysinfo` exists, but `/root/bin` is not in PATH. Append a line to `/root/.profile` so that every future login shell has `/root/bin` at the end of its PATH.

  2. Build a small system report in `/root/lab/l70/report.txt` with three lines: the kernel release (`uname -r`), the `MemTotal` line of `/proc/meminfo`, and the total usage of `/etc` as printed by `du -s /etc`.

  3. Rename the machine to `exam70`, then write one line into `/root/lab/l70/stamp.txt` with the hostname and today's date, like `exam70 2026-10-07`, built with command substitution.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.