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.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. 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
    `

    • bar then nothing at all
    • bar then bar
    • Nothing then bar
    • Nothing then nothing
    Show the answer

    bar then nothing at all

    FOO=bar creates a shell variable, readable by echo (handled by the shell itself). printenv is 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 $HOME prints the … directory.

    Show the answer

    To read a variable's value, put a dollar sign in front of it: echo $HOME prints 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=1 only creates a shell variable; export X or export X=1 makes it visible to launched programs. printenv X only shows exported variables.

  • A tool installed in ~/bin runs with ~/bin/tool, but tool answers “bash: tool: command not found”. What is the cause and the fix?

    • ~/bin is not in PATH: 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

    ~/bin is not in PATH: add it in ~/.bashrc

    With an explicit path, the shell does not consult PATH; with a bare name, it only looks in the directories of PATH. The file is executable, since ~/bin/tool runs. hash -r only forgets commands already found, and sudo does not look in ~/bin either.