Finding Help, Your Environment, and Command Locations
Unix systems are easier to use when you can answer three questions yourself: what does this command do, what will the shell run, and what settings is the program receiving? The core ideas are shared across POSIX-like systems, while manuals, shells, startup files, and command options vary among Linux distributions, BSDs, and macOS.
Read the Local Documentation
man displays installed manual pages. A page name can appear in more than one section, so the section identifies its kind.
| Section | Typical contents |
|---|---|
1 | User commands |
2 | System calls |
3 | Library functions |
5 | File formats and configuration files |
8 | System administration commands |
man ls # read the ls manual
man 5 passwd # the passwd file format, not the passwd command
man -k permission # search manual-page descriptions (often called apropos)
apropos permission # equivalent search where available
whatis ls # one-line description for an exact name
Press q to leave the usual manual-page viewer; /word searches in it. Minimal installations may not include all pages, and apropos can require a locally built index. man -k and whatis are related but not identical searches, so read their local manuals when results surprise you.
Many programs offer --help, but that long option is a widespread convention, not a POSIX guarantee. Some BSD and macOS utilities differ from GNU/Linux versions in options and output. Prefer man command for the system you are actually using. GNU systems may also have info coreutils 'ls invocation'; the info reader and its manuals are not installed everywhere.
Commands Are Not Always Files
The shell can run a built-in command, a keyword, a function, an alias, or an executable file. Consequently, which can be misleading: on some systems it is an external program and may only search PATH, missing an alias, function, or built-in. Use shell-aware inspection first.
# POSIX shell: print how the shell would resolve a name
command -V cd
command -V printf
command -V ls
# Common Bash/Zsh built-in; details vary by shell
type cd
type -a ls
# Bash has built-in help for its built-ins
help cd
command -v name is commonly used in portable shell scripts to test whether a command is available, but its exact output is shell-dependent. It is preferable to parsing which. Shell aliases are often interactive conveniences, and functions can shadow a command; scripts should not assume either exists.
PATH and Executable Lookup
When you type a simple command name without a slash, the shell searches the colon-separated directories in PATH, generally from left to right. A command containing a slash, such as ./tool or /usr/bin/env, names a path directly instead.
printf '%s\n' "$PATH"
command -v sh
command -V sh
# Show each PATH directory on its own line
printf '%s\n' "$PATH" | tr ':' '\n'
The first matching executable normally wins. Avoid adding the current directory (.) at the beginning of PATH: a program placed in the current directory could unexpectedly run instead of a trusted system command. Use ./program when you intentionally mean the local file.
Environment Variables
An environment variable is a name/value setting inherited by child processes. A shell variable is not inherited until exported. Program-specific meanings vary, so setting a variable does not guarantee every program will use it.
project='training'
printf '%s\n' "$project" # shell variable
export project # children now receive it
env | grep '^project=' # show it in a child environment
# Set a variable for this command only; the shell's LANG is unchanged
LANG=C sort names.txt
# Start a command with a deliberately small environment
env -i PATH="$PATH" sh -c 'printf "%s\n" "$PATH"'
| Variable | Common purpose |
|---|---|
HOME | Your home directory |
USER | Login name; not set in every context |
SHELL | Configured login shell, not necessarily the shell currently running |
TERM | Terminal capability type |
LANG | Default locale, affecting language, sorting, and character handling |
PAGER | Preferred text pager for programs that honor it |
EDITOR | Preferred text editor for programs that honor it |
Inspect values safely with printf '%s\n' "$HOME", not with assumptions about their contents. Do not put passwords, API tokens, or other secrets in environment variables on shared systems: depending on the system and permissions, other processes or diagnostic tools may be able to expose them.
Startup Files and Interactive Customization
A shell reads startup files according to both the shell and whether it is a login or interactive shell. Bash commonly uses ~/.bashrc for interactive shells and one of ~/.bash_profile, ~/.bash_login, or ~/.profile for login shells. Zsh commonly uses ~/.zshrc; many POSIX shells use ~/.profile for login sessions. Exact behavior differs, and macOS has used Zsh as the default interactive login shell since Catalina while still providing other shells.
Put shell-specific syntax only in that shell's files. Aliases are usually disabled in non-interactive scripts, and functions are not automatically available to child shells. Keep scripts explicit: set needed variables, use full paths when justified, and name the interpreter in a shebang when relying on non-POSIX features.
Exit Status and “Command Not Found”
After a command, $? holds its exit status. Zero conventionally means success; nonzero means failure. Check it immediately, because the next command replaces it.
grep -q 'ready' status.txt
printf 'grep status: %s\n' "$?"
When the shell says “command not found,” diagnose methodically rather than installing a random package:
- Check spelling and whether the command needs a path:
command -V nameandcommand -v name. - Inspect
PATHone directory per line. Confirm the intended directory is present and ordered appropriately. - If you expected a local program, verify it exists and is executable:
ls -l ./name; run it as./name, not merelyname. - Check whether documentation names a different utility on your OS, then use
man -kor the system's package documentation to find the provider. - If it worked in another terminal, compare shell, login status, and relevant startup files. Do not copy another machine's
PATHblindly.
Practice
- Use
man 1 printfand thencommand -V printf. Explain why their results may describe different implementations. - Run
command -V cd,command -V echo, andcommand -V ls. Identify at least one shell built-in and one external command. - Create
colour=blue, print it, export it, and confirm it appears inenv. Open a new shell afterward and observe that a non-startup-file assignment did not persist. - Temporarily run
LANG=C date, then rundatenormally. Compare the output without changing your permanent configuration.
dispelled