Environment variables
Shell or environment variable, export, env and printenv, PATH, HOME, USER, PWD, SHELL, LANG, inheritance by child processes, ~/.bashrc and which.
- 26 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Environment variable: what is it?
Show the answer
A name bound to a text value that is copied into every process launched from the shell that exported it.
Programs read their context there:
HOME,USER,LANG,EDITOR. A copy changed in a child never goes back up to its parent.What do these commands show?
```bash
FOO=bar
echo $FOO
printenv FOO`barthen nothing at allbarthenbar- Nothing then
bar - Nothing then nothing
Show the answer
barthen nothing at allFOO=barcreates a shell variable, readable byecho(handled by the shell itself).printenvis a separate program: it only sees the environment, so only exported variables. It exits without printing anything.To read a variable's value, put a … in front of it:
echo $HOMEprints the … directory.Show the answer
To read a variable's value, put a dollar sign in front of it:
echo $HOMEprints the home directory.To write, there is no dollar sign:
NOM=Lea, with no space around the equals sign.export: what is it?Show the answer
The command that moves a shell variable into the environment, so that processes launched afterwards inherit it.
X=1only creates a shell variable;export Xorexport X=1makes it visible to launched programs.printenv Xonly shows exported variables.A tool installed in
~/binruns with~/bin/tool, buttoolanswers “bash: tool: command not found”. What is the cause and the fix?~/binis not inPATH: add it in~/.bashrc- The file is not executable:
chmod +x ~/bin/tool - The shell keeps an old result in memory:
hash -r - Root's rights are missing: run
sudo tool
Show the answer
~/binis not inPATH: add it in~/.bashrcWith an explicit path, the shell does not consult
PATH; with a bare name, it only looks in the directories ofPATH. The file is executable, since~/bin/toolruns.hash -ronly forgets commands already found, andsudodoes not look in~/bineither.