What is Environment variable?

One of the "name=value" pairs held by a process, used to configure a program's behavior from outside. When a process is started, a copy of the parent's environment variables is inherited by the child. In the shell, only variables exported, as in export X=1, become environment variables and are passed to child processes; a variable that is only assigned is used only inside that shell. Besides PATH (directories searched for commands), HOME (the home directory) and LD_LIBRARY_PATH (where shared libraries are searched), they are also used, for example, to specify the license server of EDA tools. env or printenv lists them.

Japanese name: 環境変数

Environment variable appears in 8 practice questions across 4 topics, including Cloud and Container Basics, Linux and Command-Line Basics, Linkers, Loaders & Executables.

Field: Linux and Command-Line Basics

Try it: questions about Environment variable

What does the OS loader do when a program is started (for example by execve on Linux)?

  1. AIt reads the source code and compiles it on the spot before running it
  2. BIt maps each part into memory according to the program headers, sets up arguments and more on the stack, and starts execution at the entry address
  3. CIt prints every symbol name in the executable before running it
Show answer and explanation

Answer: B. It maps each part into memory according to the program headers, sets up arguments and more on the stack, and starts execution at the entry address

The loader reads the executable's headers and maps each part (segment) described by the program headers into the virtual address space with read, write and execute permissions. It prepares zero-filled memory for BSS and the stack, and pushes the command-line arguments and environment variables onto the stack. It then starts the dynamic linker if the program is dynamically linked, or otherwise begins at the executable's entry address.

An executable script run.sh in the current directory gives "command not found" when run by typing "run.sh", but runs when typed as "./run.sh". Which of the following is the reason?

  1. Arun.sh has no execute permission, and only when ./ is put in front can it be run without permission
  2. BA command name without a slash is searched for only in the directories of the PATH environment variable, which generally does not include the current directory
  3. CThe #!/bin/bash on the first line of run.sh (the shebang) is read only when it is run with ./ in front
Show answer and explanation

Answer: B. A command name without a slash is searched for only in the directories of the PATH environment variable, which generally does not include the current directory

When the shell receives a command name without a slash, it searches in order through the directories listed, separated by colons, in the PATH environment variable (/usr/local/bin:/usr/bin and so on) and runs what it finds. The current directory is generally not put in PATH, so that a malicious program with the same name placed elsewhere is not run by mistake. Writing a path containing a slash, such as ./run.sh, runs that file directly without searching PATH. The #!/bin/bash on the first line (the shebang) specifies the interpreter that runs the script, whichever way it is started.

Environment variable in the explanations

Questions that mention Environment variable (8)

  1. As shown above, the database connection target is not written into the image but passed as an environment variable when the container starts. Which of the following is the most appropriate main advantage of this design?
  2. What does the OS loader do when a program is started (for example by execve on Linux)?
  3. An executable script run.sh in the current directory gives "command not found" when run by typing "run.sh", but runs when typed as "./run.sh". Which of the following is the reason?
  4. When the commands above are run in order, which of the following is the pair of outputs of the two bash -c commands?
  5. The line "30 2 * * 1 /home/alice/backup.sh" was registered in cron. Which of the following is when backup.sh is run?
  6. With CGI, the web server starts the program as a new process every time it receives a request, and the process ends when processing is finished. Which of the following is the main reason that FastCGI and application-server approaches can handle many requests more efficiently than CGI?
  7. A database password is stored in a Kubernetes Secret. Which of the following correctly describes a Secret?
  8. A container image built by a CI/CD pipeline was tested in the staging environment and passed. Which of the following is the most appropriate way to release this image to production?

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Container Kubernetes Shell Program loader Stack Argument Dynamic linker Variable cron Pod

Back to the glossary