Difference between revisions of "Introduction to Linux in HPC/Environment variables"

From HPC Wiki
Introduction to Linux in HPC/Environment variables
Jump to: navigation, search
m (quiz 1)
Line 60: Line 60:
{{hidden begin  
{{hidden begin  
|title = What is the expected output of the following commands? <code>VARTEST="bla"; export VARTEST </code>
|title = What is the expected output of the following commands? <code>VARTEST="bla"; export VARTEST; VARTEST="blub"; env | grep VARTEST </code>
<quiz display=simple>
<quiz display=simple>
+  <code>var="value"</code>
-  <code>VARTEST=""</code>
|| Explanation: The space character in bash is important. To assign value to a variable, there must not be any space characters in the assignment. Therefore, A is correct. B results in a bash syntax error. C assigns the value <code>=value</code> to variable <code>var</code>. The first <code>=</code> sign means assignment. The second <code>=</code> sign is part of the value. The two double-quotes around value state that value is a character string.
-  <code>var = "value"</code>
-  <code>var=="value"</code>
-  <code>VARTEST="bla"</code>
+  <code>VARTEST="blub"</code>
|| Explanation: First <code>VARTEST</code> is defined as a regular shell variable, then <code>export VARTEST</code> promotes it to an evironment variable. <code>VARTEST="blub"</code> then sets the value of the variable to <code>"blub</code>. <code>env | grep VARTEST</code> greps the line that contains <code>VARTEST</code>, i.e., the value of the environment variable from the complete list of environement varibles that <code>env</code> outputs.
{{hidden end}}
{{hidden end}}

Revision as of 14:52, 22 November 2020

Title: Introduction to Linux in HPC
Provider: HPC.NRW

Contact: tutorials@hpc.nrw
Type: Multi-part video
Topic Area: HPC Platforms
License: CC-BY-SA

1. Background and History
2. The Command Line
3. Linux Directory Structure
4. Files
5. Text display and search
6. Users and permissions
7. Processes
8. The vim text editor
9. Shell scripting
10. Environment variables
11. System configuration
12. SSH Connections
13. SSH: Graphics and File Transfer
14. Various tips

This part of the Linux tutorials introduces environment variables and explains the difference to shell variables that have been introduced in Introduction_to_Linux_in_HPC/Shell_scripting. A few important use cases for environment variables are discussed such as the OATH variable that determines where the shell searches for executable programs. Environment variables are also used by the so-called environment modules that are the main way to access software installed on an HPC cluster. Environment modules are explained shortly in this tutorial.


( Slides as pdf)


How do you define an environment variable VARTEST and assign the value 42 to it?

export VARTEST=42
env VARTEST=42

What is the naming convention for environment variables?

Use uppercase letters
Use lowercase letters
There is no naming convention.

Which statement is correct for environment variables?

Environment variables are stored in a central database on a computer.
An environment variable that was defined is valid and accessible in every shell session on this computer.
An environment variable is valid in the shell session in which it was defined as well as all programs or shell sessions that are started from this session.

What is the expected output of the following commands? VARTEST="bla"; export VARTEST; VARTEST="blub"; env


Assume that the environment variable PATH has the following content PATH="/usr/bin:/usr/local/bin". There is a program in each directory, i.e., /usr/bin/program and /usr/local/bin/program. Which will be executed if you run program in the shell?

The shell will ask you which one to execute.

How do you add the directory /opt/bin/ to the PATH environment variable and make sure that it is searched last?

export PATH=/opt/bin/:$PATH
export PATH=$PATH:/opt/bin/
export PATH=/opt/bin/

Exercises in Terminal

1. a. Write a script that
   b. Prints an environment variable
   c. Saves the output of the date command to a variable
   d. Sleeps briefly
   e. Prints the new and old date and time
 2. What do different types of quotes (single ' vs. double ") do?
3. create an shell variable MYIDENTITY and export it as below: 
   $ export MYIDENTITY=whoami
   How will you list the shell variable MYIDENTITY?
   Execute the shell variable MYIDENTITY, what is the output?