Linux › Section M · User environments › Topic M.1

Startup file guidelines and elements

Every time you open a terminal, a few short files decide your command path, your prompt and the permissions of every file you create. Knowing what belongs in them, and what does not, keeps a user environment fast, safe and easy to repair.

Where the system meets the user

Your home directory collects many dot files, most written automatically when a program first runs. The ones you are most likely to edit are the shell startup files, such as .bashrc and .profile, which run each time a shell starts and set up its environment. The topic "Dot files, environment variables, and the command path" introduced dot files, exported variables and PATH. This topic asks a different question: what should a well-made startup file contain, and how do you write one that other people can rely on?

Two guidelines: simple and readable

If a startup file breaks and you are the only user, you fix it once. If it is the default for every new account, copied from /etc/skel when the account is created (see "User management files"), one mistake is repeated in every home directory. So keep the number of files small and each file short: every line is one more thing that can fail. Comment generously, so a user can see what each block does. Change a working distribution default only for a clear reason: a new prompt, locally installed software that genuinely needs it, or a file that is already broken.

The command path

The most important element is PATH. A sensible order is /usr/local/bin, then /usr/bin, then /bin, so that a site-specific program in /usr/local can override the packaged version of the same name. On most current distributions /bin is simply a symbolic link to /usr/bin (the "merged /usr" layout), but listing both does no harm. Personal scripts go first, traditionally in ~/bin or, by the newer convention, ~/.local/bin. Administrators may add /usr/local/sbin, /usr/sbin and /sbin for tools such as fdisk. Rather than adding a new directory for every installed package, place symbolic links to its programs in /usr/local/bin.

Worked example: adding a directory safely

Goal: put ~/.local/bin at the front of PATH without creating duplicates when .bashrc is read twice. 1. Start from a known base: PATH=/usr/local/bin:/usr/bin:/bin. 2. Define a small function: add_path() { case ":$PATH:" in *":$1:"*) ;; *) PATH="$1:$PATH" ;; esac; }. Wrapping PATH in colons lets the pattern match an entry at either end. 3. Call add_path "$HOME/.local/bin" twice. 4. export PATH. 5. Check with echo "$PATH". Run in bash 5.2 with HOME=/tmp/h, it printed /tmp/h/.local/bin:/usr/local/bin:/usr/bin:/bin. The second call changed nothing, which is the point of the test.

Never put the current directory in PATH

Adding a dot (.) to PATH lets you run ./myprog as just myprog, but it is a security hole. At the front of the path, a hostile archive could contain its own program named ls, and simply listing that directory would run it. Even at the end, an attacker can plant likely typos such as sl. It also makes a command behave differently depending on where you happen to stand. Watch for the hidden form too: an empty entry, from a leading or trailing colon or a doubled colon (::), also means the current directory. Type ./ instead; it is two characters.

The manual page path and the pager

Older guides told users to set MANPATH. Leave it unset. The man-db system works out the manual path from your PATH and its configuration file (/etc/manpath.config on Debian and Ubuntu, /etc/man_db.conf on Fedora and RHEL), and setting MANPATH overrides that logic, so newly installed manual pages can vanish. The manpath command prints the path man is actually using. Related defaults are worth setting once: PAGER=less chooses the program that shows long text a screen at a time, and EDITOR and VISUAL name the editor that tools such as crontab -e open.

The prompt

The prompt string PS1 should help, not decorate. Bash replaces backslash escapes in it each time it is shown: \u is the user name, \h the short host name, \w the full current directory with your home shown as ~, \W only its last component, \! the history number, and \$ a $ for ordinary users or # for root. The bash(1) manual page lists the rest. A good everyday choice is PS1='\u@\h:\W\$ ', in single quotes so the escapes reach bash unchanged. Avoid shell special characters such as {, }, =, & and <, and especially >: if you paste a line from your terminal back into it, a > in the prompt becomes a redirection and creates stray files.

Worked example: reading a prompt

Predict what PS1='\u@\h:\W\$ ' shows for user maria on host lab1. 1. \u becomes maria and \h becomes lab1. 2. In /home/maria/projects, \W is projects. 3. maria is not root, so \$ gives $. The prompt is maria@lab1:projects$ followed by a space. 4. In her home directory \W shows ~, giving maria@lab1:~$. 5. Swapping in \w would show ~/projects$ in the first case. To test a prompt without installing it, store it in P and run echo "${P@P}": in bash 5.2 inside a projects directory, the output ended :projects$, as predicted.

Aliases: use sparingly

An alias replaces one word with another string before a command runs. It saves typing, but it has costs. Arguments are awkward to handle, aliases are not passed to child shells, and non-interactive shells such as scripts ignore them by default: in a test, bash -c 'alias ll="ls -l"; ll' failed with "ll: command not found". The classic error is redefining a real command, such as alias ls='ls -F': users forget the extra option, and removing it takes \ls or command ls. Prefer a shell function or a small script; a function can also change the current shell's variables, which a script cannot.

The permissions mask

Put a umask command in a startup file so every program you run creates files with predictable permissions (see "File modes and permissions"). Two choices are reasonable. umask 077 shuts out everyone else: new files get -rw------- (600) and new directories drwx------ (700). It suits shared multi-user machines, though it can frustrate users who want to share. umask 022 lets others read: files get -rw-r--r-- (644) and directories drwxr-xr-x (755). It suits single-user machines and service accounts that need to read your files. Some programs, such as mail clients, set a stricter mask of their own.

Common mistakes to avoid

Putting . or an empty entry in PATH, which lets a planted program run in place of a real one. Prepending to PATH without a guard, so the same directory appears many times after repeated sourcing. Setting MANPATH and losing manual pages that were installed later. Writing PS1 in double quotes, so the shell expands $ and backquotes once at assignment instead of each time the prompt appears. Aliasing a standard command to add options, then forgetting the alias exists; type ls reveals it. Choosing 077 as the system-wide default and then finding that users "fix" sharing problems with world-writable modes.

Key takeaways

Startup files should be few, short and well commented, especially when they become defaults for other users. PATH comes first: personal bin, then /usr/local/bin, /usr/bin and /bin, never a dot or an empty entry, and added with a guard against duplicates. Leave MANPATH unset; set PAGER, EDITOR and VISUAL once. Keep PS1 short and free of shell special characters, written in single quotes. Prefer functions and scripts to aliases. Set umask explicitly: 077 for privacy, 022 when others need to read.

Flashcards

Flashcard

Shell startup file

A file such as .bashrc or .profile that a shell runs as it starts

It sets PATH, the prompt, the umask and other defaults.

Test your understanding

Question 1 of 5

A user's PATH is :/usr/local/bin:/usr/bin:/bin. What hidden risk does it contain?

Question 2 of 5

With umask 077 set in .bashrc, what permissions does a newly created regular file get?

Question 3 of 5

Why does this topic recommend leaving MANPATH unset?

Question 4 of 5

What does PS1 set to a user-name escape, @, a host-name escape, a colon, the last-directory escape and the dollar escape show for root on host db2 in /var/log?

Question 5 of 5

An administrator adds alias rm='rm -i' to the default .bashrc. Which consequence does the topic warn about?

← Previous topicNext topic →