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.
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.
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.
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
PATHonly. Blind to aliases, functions and builtins. - history
- Previous commands.
HISTSIZEin 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
- A variable is only inherited by child processes if it was exported. Expect a scenario that turns on exactly this.
- type is the correct answer when the question involves aliases, functions or builtins; which is not.
- Single quotes stop variable expansion, double quotes do not.
- 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
- A user starts a new terminal window in a running graphical desktop session, which launches bash as an interactive shell that is NOT a login shell. Which file in the user's home directory does bash read in that case? machine-checked
- In bash, which history expansion re-runs the entire previous command line? machine-checked
- Which bash command prints the literal five characters $USER instead of the current user name? machine-checked
- You want /opt/bin searched for executables, after all the directories already in PATH, in the current bash session and in every command started from it. Which command does that? machine-checked
- There is a passwd manual page in section 1 (the command) and another in section 5 (the /etc/passwd file format). Which command opens the section 5 page? machine-checked
- Select the TWO true statements about shell variables and the environment in bash. machine-checked
- Type the single command that prints the absolute path of the shell's current working directory. machine-checked
- A colleague reports that `ls` on their account always prints in colour, and you suspect an alias rather than a different binary. Which command tells you whether the name `ls` currently resolves to an alias, a shell function, a builtin or a file on disk? machine-checked
- A vendor's driver package requires you to report the exact kernel release the machine is running, and nothing else. Which command prints only that? machine-checked
- Your bash session keeps thousands of commands available with the up arrow, but after logging out and back in only the last few hundred survive. Which variable governs how many lines are kept in ~/.bash_history when the shell exits? machine-checked
- You have run a long sequence of commands in one terminal and want them written to ~/.bash_history right now, so a second terminal can read them, without closing the session and without discarding the history the file already holds. Which command does that? machine-checked
- In a directory containing several files, you type `echo *` and get a list of file names rather than an asterisk. Which command prints a single literal asterisk instead? machine-checked
- A sort must run with LC_ALL set to C so that it collates byte by byte, but the rest of your interactive session should keep its normal locale. Which command achieves that? machine-checked
- You are in /home/ann/tools, where deploy.sh exists, begins with #!/bin/bash and carries the execute bit for your user. Typing `deploy.sh` returns "command not found". What is the cause, and what runs it? machine-checked
- Type the complete command, including the single option, that prints every field uname reports at once — kernel name, network node name, kernel release, kernel version, machine and operating system — on one line. machine-checked
- Earlier in this session you ran `export EDITOR=vim`. You now want commands started from this shell to stop receiving EDITOR, while the shell itself keeps using the value in its own expansions. Which command does that? machine-checked
- Select the THREE true statements about quoting in bash. machine-checked
- You know there is a command that reports information about the running kernel but cannot remember its name. Which command searches the names and one-line descriptions of all manual pages for the word kernel? machine-checked
- The output of `history` shows the long rsync command you need as entry number 512. Which is the shortest way to run exactly that entry again? machine-checked
- A shell prompt for input must leave the cursor on the same line as the text. Which bash command writes `Continue? ` with no trailing newline? machine-checked
Practise Working on the command line