Linux › Section B · Basic commands and the directory hierarchy › Topic B.5
Dot files, environment variables, and the command path
Your shell carries settings with it: hidden configuration files in your home directory, variables that shape how programs behave, and a search path that decides which program a command name runs. Understanding them explains many puzzling "command not found" moments.
Your password and your shell
Two account settings are yours to change. passwd changes your password: it asks for the current one, then the new one twice. Length is the strongest defence, so a long passphrase of several unrelated words beats a short string of odd symbols, as the Computer Science topic "Operating system security" explains. chsh changes your login shell, the shell started when you log in, to another installed one such as zsh; for ordinary users it accepts only shells listed in /etc/shells. This course assumes bash, so examples may behave differently in another shell.
Dot files
Run ls in your home directory, then ls -a, and extra names appear, each starting with a dot, such as .bashrc, .profile and .ssh. These dot files usually hold settings. Nothing about them is special except the convention that tools hide them: ls skips them unless you use -a, which also lists . and .., or -A, which omits those two. Shell globs skip them too, so by default * never matches .bashrc; a pattern must start with a dot to match one. Older bash versions let .* match . and .. as well, a trap in commands such as rm -r .*; bash 5.2 skips them by default.
Shell variables
The shell can store named text values. Assign one with an equals sign and no spaces: STUFF=blah. Read it by putting $ in front: echo $STUFF prints blah. The spaces matter, because STUFF = blah makes the shell run a command called STUFF with arguments = and blah, and bash replied STUFF: command not found. Some variables control the shell itself; for example, bash reads PS1 to draw your prompt. Variables are also central to scripting, as the topic "Special variables and exit codes" shows. A plain shell variable belongs to the current shell only.
Environment variables and export
Every process, not just the shell, has a set of environment variables, and when a process starts a program, the program receives a copy of them. export STUFF promotes a shell variable into the environment so that programs you run can see it. Writing NAME=value command sets a variable in the environment of that one command only. Many programs read the environment for settings: HOME holds your home directory, USER your username, SHELL your login shell, LANG your language and character set, and LESS default options for less. printenv HOME prints one variable, and env or printenv alone lists the whole environment.
Worked example: shell or environment?
All of these lines were run in bash 5.2. 1. STUFF=blah, then echo $STUFF printed blah: the shell knows it. 2. bash -c 'echo "child sees: [$STUFF]"' printed child sees: []. The single quotes kept the outer shell from expanding $STUFF, so a new child bash expanded it and found nothing, because STUFF was not in its environment. 3. After export STUFF, the same command printed child sees: [blah]. 4. GREETING=hi bash -c 'echo "one-off: $GREETING"' printed one-off: hi, yet echo "after: [$GREETING]" then printed after: []. Check: exporting reached later children; the one-off form reached only one command.
The command path
When you type a command name without a slash, the shell looks for a program of that name in the directories listed in the PATH environment variable. echo $PATH shows them separated by colons, typically something like /usr/local/bin:/usr/bin:/bin. The directories are searched in order and the first match wins, so a program in /usr/local/bin takes priority over one with the same name in /usr/bin. The current directory is normally not in PATH, for safety, so to run a program in it you type ./program. A name that contains a slash, such as ./hello or /usr/bin/ls, is run directly without searching PATH.
Changing PATH safely
To add a directory, refer to the old value inside the new one. PATH=~/bin:$PATH puts ~/bin first, so it is searched before everything else; PATH=$PATH:~/bin puts it last. A typing slip can wipe the path: PATH=~/bin:$PTH, with a misspelt variable, leaves only ~/bin, and then even ls fails with ls: command not found. Do not panic. The change affects only that shell, so closing the terminal and opening a new one restores the normal path. To make a change permanent you add the line to a startup file such as ~/.bashrc, as the topic "Startup file guidelines and elements" explains.
Worked example: running your own script
A small script called hello, which prints Hello from my script, is saved in /tmp/practice/bin and made executable. 1. From /tmp/practice, typing hello gave bash: hello: command not found, because bin is not in PATH. 2. cd bin, then ./hello, ran it: the slash means run this exact file. 3. Back in /tmp/practice, PATH=/tmp/practice/bin:$PATH added the directory at the front. 4. Now hello printed Hello from my script from anywhere. 5. type hello printed hello is /tmp/practice/bin/hello; after one use it printed hello is hashed (/tmp/practice/bin/hello), because bash remembers found paths. Check: no file moved; only the search list changed.
type, which and alias
Several things can answer to a command name, and type tells you which. type cd prints cd is a shell builtin; type hello printed the file it found in PATH; type -a echo lists every match, here the builtin first and then echo programs in the path. which normally searches only PATH for a program file, so it misses builtins and aliases. An alias is a shortcut defined in the shell: alias ll='ls -l' makes ll run ls -l, and type ll then prints ll is aliased to `ls -l'. Aliases last only for the current shell unless placed in a startup file, and bash expands them in interactive shells, not in scripts.
Common mistakes to avoid
First, putting spaces around = in an assignment. Second, expecting a program to see a shell variable you never exported. Third, setting PATH=~/bin, without $PATH, and losing every other directory. Fourth, adding . to PATH, which lets a planted file in any directory you visit run in place of a real command. Fifth, trusting which to explain a command; type also reports builtins and aliases. Sixth, forgetting that the first match in PATH wins, so a stray program earlier in the list can hide the real one.
Key takeaways
passwd changes your password and chsh your login shell, from /etc/shells. Dot files hold settings; ls -a or -A shows them, and globs skip them. NAME=value, with no spaces, sets a shell variable and $NAME reads it. export puts it in the environment that child processes inherit, and NAME=value command sets it for one command; printenv and env show the environment. PATH is a colon-separated list searched in order, first match winning; . is not in it, so use ./program. Extend it with PATH=dir:$PATH. type explains what a name runs, which searches PATH only, and alias creates shortcuts.
Flashcards
Flashcard
Changes your password
Test your understanding
Question 1 of 5
You run COLOR=red and then bash -c 'echo $COLOR'. Why does the child print an empty line?
Question 2 of 5
Which command adds ~/tools to the end of the search path without losing the existing directories?
Question 3 of 5
A script named build is in your current directory, which is not in PATH. How do you run it?
Question 4 of 5
PATH is /usr/local/bin:/usr/bin, and a program called report exists in both directories. What runs when you type report?
Question 5 of 5
You want to know whether ll is an alias, a builtin or a program file. Which command answers that best?
