Working on the command line

Working in the shell itself: the difference between a shell variable and an environment variable, how the shell decides which program a name refers to, and the handful of commands that tell you where you are.

Lesson 1 of 8 in objective 103. GNU and Unix commands, part of LPIC-1 Exam 101-500.

What bash tries when you type a name, and where it stops. A search over 4 places, tried in this order: the first hit ends it, and everything under the one that hits is never looked at. Going down a step means there is no such name here. First, Alias; a hit there means that is what runs, and nothing below it is tried. Then, Shell function; a hit there means that is what runs, and nothing below it is tried. Then, Builtin (the shell runs its own time, not /bin/time); a hit there means that is what runs, and nothing below it is tried. Last, PATH (each directory, left to right); a hit there means the first directory holding the file wins. A miss at every one of them: command not found. Alias that is what runs, and nothing below it is tried there is no such name here Shell function that is what runs, and nothing below it is tried Builtin the shell runs its own time, not /bin/time that is what runs, and nothing below it is tried PATH each directory, left to right the first directory holding the file wins command not found
What bash tries when you type a name, and where it stops.

Variables, and the word export does all the work

A plain assignment — NAME=value, with no spaces around the equals sign — creates a shell variable, visible to this shell and to nothing else. export NAME turns it into an environment variable, which means it is copied into every process the shell starts afterwards. That is the whole distinction, and it explains the classic failure: a variable set but not exported is invisible to the script that needed it.

set with no arguments lists shell variables and functions; env lists the environment. env is also a command that runs a program with a modified environment, which is why env VAR=x command is the idiom for setting something for one command only. unset removes a variable. Changes made in a shell die with it, so anything that must survive goes into a startup file — ~/.bashrc for an interactive shell, ~/.bash_profile for a login shell, /etc/profile and /etc/profile.d/ for everyone.

One word, export, decides which processes can see a variable. Shell variable — Created by: NAME=value, no spaces; Who sees it: This shell, and nothing else; Listed by: set. Environment variable — Created by: export NAME; Who sees it: Every process the shell starts afterwards; Listed by: env Shell variable Environment variable Created by NAME=value, no spaces export NAME Who sees it This shell, and nothing else Every process the shell starts afterwards Listed by set env
One word, export, decides which processes can see a variable.

Which program actually runs

When you type a name, bash checks in order: aliases, shell functions, builtins, then each directory in PATH from left to right. type NAME reports which of those it found and is therefore the honest answer; which searches PATH only and will happily tell you about /bin/time while the shell runs its own builtin. Adding a directory to PATH is PATH=$PATH:/new/dir — losing the $PATH is how a shell ends up unable to find ls.

history recalls previous commands, stored in ~/.bash_history and controlled by HISTSIZE (how many are kept in memory) and HISTFILESIZE (how many in the file). !! repeats the last command and !n repeats command n. uname reports the kernel — uname -r for the release, uname -a for everything — and is the command a question means when it asks how to identify a running kernel version.

Quoting, and the manual

Double quotes suppress globbing and word splitting but still expand $variables and backticks. Single quotes suppress everything, so the text is literally what you typed. A backslash escapes exactly the next character. Getting this wrong is how a script passes a literal $HOME to a program.

man PAGE reads the manual; man -k or apropos searches the short descriptions when you do not know the page name. Manual sections matter: 1 is user commands, 5 is file formats, 8 is administration, so man 5 passwd is the file format and man 1 passwd is the command. uname, echo, pwd and exec round out the vocabulary — exec replaces the shell with the program rather than starting a child, which is why a script ending in exec leaves no extra process behind.

The stronger the quoting, the less the shell expands. A range from everything is expanded to nothing is expanded. No quotes: globs and splits. Double quotes: $VAR still expands. Single quotes: not even $VAR. everything is expanded nothing is expanded No quotes globs and splits Double quotes $VAR still expands Single quotes not even $VAR
The stronger the quoting, the less the shell expands.

Worth carrying in

export VAR=value
Make a variable part of the environment inherited by child processes.
env
Print the environment, or run a command with a modified one.
set
Shell variables and functions, and shell options.
unset VAR
Remove a variable.
type cmd
What the shell would actually run: alias, function, builtin or file.
which cmd
Search PATH only. Blind to aliases, functions and builtins.
history
Previous commands. HISTSIZE in memory, HISTFILESIZE in ~/.bash_history.
uname -r
The running kernel release. -a for everything at once.
man -k word
Search manual page descriptions. Same as apropos.

What the exam does with this

Objective
103. GNU and Unix commands
Share of the exam
43.33% (the whole objective)
Questions in this lesson
20
Signed for by a person
0

Partly checked. None of the 20 questions here has been read against the cited source by a person. 20 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Working on the command line

Questions in this lesson

Practise Working on the command line

The rest of objective 103