Customizing the shell environment
The files bash reads when it starts, which of them apply to a login shell and which to every shell, and how to set variables, aliases and functions so they are there next time.
Lesson 1 of 2 in objective 105. Shells and shell scripting, part of LPIC-1 Exam 102-500.
Login shells and interactive shells read different files
A LOGIN shell — the one you get from a console login or an ssh session — reads /etc/profile, then the first it finds of ~/.bash_profile, ~/.bash_login or ~/.profile. An INTERACTIVE NON-LOGIN shell — a new terminal window inside an already logged-in desktop — reads ~/.bashrc and, on most distributions, /etc/bash.bashrc or /etc/bashrc. That is why a variable that works after ssh is missing in a terminal window, and it is the most common real-world symptom of this objective.
Most distributions paper over the split by having ~/.bash_profile source ~/.bashrc, so everything goes in ~/.bashrc and is read in both cases. /etc/profile.d/ is the system-wide equivalent: drop a .sh file there rather than editing /etc/profile, so a package can add settings without fighting over one file. Logout runs ~/.bash_logout.
What you put in them
Variables: NAME=value defines, export makes it inherited. PATH, EDITOR, LANG and PS1 (the prompt string) are the ones set most often. Aliases: alias ll='ls -l' — an alias only expands as the FIRST word of a command, and it is not inherited by scripts or subshells, which is why anything that has to work in a script belongs in a function or a real command instead.
Taking a variable away again is unset NAME, and the exam separates it from the two things that resemble it, because a program can tell all three apart. NAME= leaves the variable there holding an empty string — still exported, so a child still receives it, and anything that merely asks whether the name is set still finds it. export -n NAME removes only the export attribute: children stop seeing it, set still lists it, and it is still a variable of this shell. unset is the one that removes the name entirely, from the shell and from the environment handed to everything it starts. The exception is a variable made readonly, which cannot be unset at all — the only way out of that is a new shell.
Reading one back has three answers and the exam knows it. echo $PATH is the shell doing the work — it expands the variable and echo never sees a variable at all, which is why it prints a set-but-unexported variable that the other two cannot. printenv PATH asks for one variable by name, and printenv with no argument lists the whole environment. env also prints the environment, and its real job is running a command with a modified one — env VAR=x command — so env | grep '^PATH=' is the roundabout spelling of the same question. set is the fourth and is different again: it lists shell variables AND functions, exported or not.
Functions are defined as name() { …; } and behave like commands, taking $1, $2 and so on. Both aliases and functions are shell-local, so they must be defined in a file the shell reads rather than exported. source FILE (or its synonym . FILE) runs a file in the CURRENT shell rather than a child, which is the whole reason it exists: a script that sets a variable in a child shell changes nothing, and sourcing it changes your shell.
/etc/skel is the template for new accounts — whatever is in it is copied into a new user's home directory when the account is created, which is how every new user gets a sensible .bashrc.
Worth carrying in
- ~/.bash_profile
- Login shells. Usually just sources
~/.bashrc. - ~/.bashrc
- Interactive non-login shells. Aliases and functions belong here.
- /etc/profile
- System-wide login. Add files to
/etc/profile.d/rather than editing it. - source file / . file
- Run a file in the current shell so its settings persist.
- alias ll='ls -l'
- Only expands as the first word; not inherited by scripts.
- name() { …; }
- A shell function: takes $1, $2, and works where an alias will not.
- /etc/skel
- Copied into every new user's home directory at account creation.
- echo $PATH
- The shell expands it before
echoruns, so this works on unexported variables too. - printenv
- The environment.
printenv NAMEprints one; with no argument, all of it. - env
- Prints the environment, or runs a command with a changed one:
env VAR=x cmd. - unset NAME
- Removes the variable outright.
NAME=leaves it set and empty;export -nonly stops it being inherited.
What the exam does with this
- Login versus non-login is the whole objective. Know which file each reads and in what order.
sourceruns in the current shell; executing the script runs it in a child and its variables die with it.- Aliases are not available inside scripts. Functions and exported variables are the alternatives.
echo $PATH,printenv PATHandenv | grep '^PATH='all print it — a "choose three" question is usually this one. Onlyechosees a variable that was never exported.
- Objective
- 105. Shells and shell scripting
- Share of the exam
- 15% (the whole objective)
- Questions in this lesson
- 22
- Signed for by a person
- 0
Partly checked. None of the 22 questions here has been read against the cited source by a person. 22 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 Customizing the shell environment
Questions in this lesson
- A user logs in at a text console, and bash starts as an interactive login shell. Which file in that user's home directory is read by bash itself as part of login startup? machine-checked
- You are already logged in to a graphical desktop and open a new terminal window, so bash starts as an interactive shell that is not a login shell. Which per-user file does bash read in that case? machine-checked
- An executable script contains only the line `export EDITOR=vim`. After running it as `./setenv.sh`, `echo $EDITOR` in the calling shell prints nothing. What explains this? machine-checked
- In bash, what is the effect of `export MYVAR`? machine-checked
- At a bash prompt you run `alias ll='ls -l'` and it works for the rest of that session, but a terminal window opened afterwards does not recognise `ll`. Which statement explains this and gives the standard remedy? machine-checked
- The file setenv.sh, in the current directory, sets and exports several variables. You are in an interactive bash shell running with default options (not POSIX mode). Which two invocations leave those variables set in that shell after the file has run? (Choose two.) machine-checked
- Which three commands print the current value of the PATH variable to the terminal? (Choose three.) machine-checked
- On a Debian server you must set a prompt and a couple of aliases for every user, and they must apply to the terminal windows users open inside their desktop session, not only to console logins. Which system-wide file is the right place for those settings? machine-checked
- A user's home directory contains both ~/.bash_profile and ~/.profile. Settings added to ~/.profile have no effect when the user logs in at a console. What explains this? machine-checked
- You want a command to run automatically whenever a user ends a console login session, for example to clear the screen. Which file should contain it? machine-checked
- A user keeps personal scripts in ~/bin. One of them is called grep, and the user wants their own version to be found before /usr/bin/grep, while every other command in the standard directories stays findable by name. Which line, added to a shell startup file, achieves that? machine-checked
- During an audit you find a shell startup file containing export PATH="$PATH:". A user in /tmp types report and an executable file named report in /tmp runs, even though no directory named in PATH holds such a file. Why? machine-checked
- A build script must run once with LANG set to C so that it produces untranslated messages, but the interactive shell's own locale must stay as it is. Which invocation does that? machine-checked
- A variable named PROJECT appears in the output of set, but not in the output of env, and a script started from that shell cannot see it. What is the state of PROJECT? machine-checked
- You want a shortcut mkcd that creates a directory and then changes into it, so that mkcd /srv/app makes and enters /srv/app. Why must this be written as a shell function rather than as an alias? machine-checked
- Your shell has alias rm='rm -i' in effect. For one file you want the external rm to run without the interactive prompt, while leaving the alias defined for later commands. Which command line does that? machine-checked
- Scripts run from cron must pick up a shared file of helper definitions. The definitions are in ~/.bashrc, but the scripts never see them because bash is started non-interactively. Which environment variable, when it names a file, causes a non-interactive bash to read that file at startup? machine-checked
- After adding a company-standard alias to /etc/skel/.bashrc, you check an existing user's account and the alias is absent from their ~/.bashrc. What is the effect of the change you made? machine-checked
- Which TWO statements about bash shell functions are correct? (Choose two.) machine-checked
- Type the bash builtin that, with no options or arguments, prints every shell variable and function of the current shell, including those that have never been exported, and that is also the builtin used with -o and +o to control shell options. machine-checked
- A startup file exported http_proxy, and for the rest of this session the variable must be gone completely: neither set nor env should list it any more, and a program started from this shell must not find it even as an empty value. Which command does that? machine-checked
- Type the name of the shell variable, without a leading dollar sign, whose value bash expands and displays as the primary prompt string before each interactive command. machine-checked
Practise Customizing the shell environment
The rest of objective 105
- Customizing the shell environment — you are here
- Writing simple shell scripts