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
m (Tweak page sorting and title)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Tutorials]]<nowiki />
+
[[Category:Tutorials|Environment Variables (Linux)]]<nowiki />
{{DISPLAYTITLE:<span style="position:absolute; top:-9999px;">Introduction to Linux in HPC/</span>Environment variables}}<nowiki />
+
{{DISPLAYTITLE:Environment Variables (Linux)}}<nowiki />
 
{{Syllabus Introduction to Linux}}<nowiki />
 
{{Syllabus Introduction to Linux}}<nowiki />
 
__TOC__
 
__TOC__
Line 8: Line 8:
 
=== Video === <!--T:5-->
 
=== Video === <!--T:5-->
  
<youtube width="600" height="400" right>dAcbPeVdv0s</youtube>
+
<youtube width="600" height="340" right>dAcbPeVdv0s</youtube>
  
 
([[Media:HPC.NRW_Introduction_to_Linux_in_HPC_10_Environment_Variables.pdf | Slides as pdf]])
 
([[Media:HPC.NRW_Introduction_to_Linux_in_HPC_10_Environment_Variables.pdf | Slides as pdf]])
Line 15: Line 15:
  
 
{{hidden begin  
 
{{hidden begin  
|title = How do you define an environment variable VARTEST and assign the value 42 to it?
+
|title = How do you define an environment variable <code>VARTEST</code> and assign the value <code>42</code> to it?
 
}}
 
}}
 
<quiz display=simple>
 
<quiz display=simple>
Line 21: Line 21:
 
|type="()"}
 
|type="()"}
 
-  <code>VARTEST=42</code>
 
-  <code>VARTEST=42</code>
|| Explanation: This statement would only define VARTEST as a shell variable and not as an environemnt variable.
+
|| Explanation: This statement would only define <code>VARTEST</code> as a shell variable and not as an environemnt variable.
 
+  <code>export VARTEST=42</code>
 
+  <code>export VARTEST=42</code>
|| Explanation: The keyword <code>export</code> makes VARTEST an environment variable.
+
|| Explanation: The keyword <code>export</code> makes <code>VARTEST</code> an environment variable.
 
-  <code>env VARTEST=42</code>
 
-  <code>env VARTEST=42</code>
 
||  
 
||  
Line 35: Line 35:
 
{
 
{
 
|type="()"}
 
|type="()"}
+  <code>Use uppercase letters</code>
+
Use uppercase letters, i.e., <code>VARTEST2</code>
 
|| Explanation: It is recommended to only use uppercase letters, numbers, and underscores in the names of environment variables.  
 
|| Explanation: It is recommended to only use uppercase letters, numbers, and underscores in the names of environment variables.  
-  <code>Use lowercase letters</code>
+
Use lowercase letters, i.e., <code>vartest2</code>
 
||  
 
||  
<code>There is no naming convention.</code>
+
-  There is no naming convention, i.e., <code>VarTest2</code>
 
||  
 
||  
 
</quiz>
 
</quiz>
Line 54: Line 54:
 
-  An environment variable that was defined is valid and accessible in every shell session on this 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.
+
+  An environment variable is valid in the shell session in which it was defined and is inherited to all programs or shell sessions that are started from this session.
 
|| Explanation: Environment variables are inherited from parent processes to their child processes.
 
|| Explanation: Environment variables are inherited from parent processes to their child processes.
 
</quiz>
 
</quiz>
Line 60: Line 60:
  
 
{{hidden begin  
 
{{hidden begin  
|title = What is the expected output of the following commands? <code>VARTEST="bla"; export VARTEST; VARTEST="blub"; env | grep VARTEST </code>
+
|title = What is the expected output of the following commands: <syntaxhighlight lang="bash">VARTEST="bla";export VARTEST="BLA";VARTEST="blub";env | grep VARTEST</syntaxhighlight>
 
}}
 
}}
 
<quiz display=simple>
 
<quiz display=simple>
Line 75: Line 75:
  
 
{{hidden begin  
 
{{hidden begin  
|title = Assume that the environment variable <code>PATH</code> has the following content <code>PATH="/usr/bin:/usr/local/bin"</code>. There is a program in each directory, i.e., <code>/usr/bin/program</code> and <code>/usr/local/bin/program</code>. Which will be executed if you run <code>program</code> in the shell?
+
|title = Assume that the environment variable <code>PATH</code> has the following content <code>/usr/bin:/usr/local/bin</code>. There is a program in each directory, i.e., <code>/usr/bin/program</code> and <code>/usr/local/bin/program</code>. Which will be executed if you run <code>program</code> in the shell?
 
}}
 
}}
 
<quiz display=simple>
 
<quiz display=simple>
Line 107: Line 107:
  
  
  1. a. Write a script that
+
Run the following steps in a shell:
    b. Prints an environment variable
+
  1. Define an environment variable named <code>TEST_VAR1="test1"</code>.
    c. Saves the output of the date command to a variable
+
2. Define a shell variable names <code>TEST_VAR2="test2"</code>.
    d. Sleeps briefly
+
3. Make sure that <code>TEST_VAR1</code> is in the list of environment variables.
    e. Prints the new and old date and time
+
4. Start a new shell session within the current session.
 +
5. Output the content of the two variables <code>TEST_VAR1</code> and <code>TEST_VAR2</code>.
 +
6. Change content of the environment variable to <code>TEST_VAR1="test_new"</code>.
 +
7. Exit the shell session and output the content of <code>TEST_VAR1</code>.
 
   {| role="presentation" class="wikitable mw-collapsible mw-collapsed"
 
   {| role="presentation" class="wikitable mw-collapsible mw-collapsed"
 
     | <strong>Answer:</strong>
 
     | <strong>Answer:</strong>
 
     |-
 
     |-
 
     |  
 
     |  
       One possible way to write the script is
+
       The commands to enter are:
 
       <syntaxhighlight lang="bash">
 
       <syntaxhighlight lang="bash">
       #!/bin/bash
+
       export TEST_VAR1="test1"
 
+
       TEST_VAR2="test2"
       echo "I am $USER, my home directory is $HOME"
+
      env | grep TEST_VAR1
 
+
      bash
       olddate=$(date)
+
       echo $TEST_VAR1
       sleep 10s
+
       echo $TEST_VAR2
       echo "old date: ${olddate}"
+
       export TEST_VAR1="test_new"
       echo "new date: $(date)"
+
      exit
 +
       echo $TEST_VAR1
 
       </syntaxhighlight>
 
       </syntaxhighlight>
 
      Explanation:
 
        <code>#!/bin/bash</code>: the shebang line for bash script
 
        <code>echo "I am $USER, my home directory is $HOME"</code>: the variables <code>$USER</code> and <code>$HOME</code> are your username and home directory, respectively. This line prints a sentence embedded with your username and home directory.
 
        <code>olddate=$(date)</code> assigns the output of the date command to the variable olddate as a string.
 
        <code>sleep 10s</code> puts the terminal into idle for 10 seconds.
 
        <code>echo "old date: ${olddate}"</code> prints the previously saved date in the olddate variable. The form of <code>${olddate}</code> is for using (or referencing) this variable.
 
        <code>echo "new date: $(date)"</code> prints the current date, in which the output of the command date will take the place of <code>$(date)</code>.
 
 
     |}
 
     |}
  
   2. What do different types of quotes (single <code>'</code> vs. double <code>"</code>) do?
+
    
  {| role="presentation" class="wikitable mw-collapsible mw-collapsed"
 
    | <strong>Answer:</strong>
 
    |-
 
    |
 
      single quote <code>'</code> gives literal string, the variable will not be interpreted, e.g.
 
      <syntaxhighlight lang="bash">
 
      var=abc
 
      echo '$var'     
 
      </syntaxhighlight>
 
      This script prints the literal string <code>$var</code> (instead of its value <code>abc</code>) to terminal.
 
      double quote <code>"</code> allows the variable to be interpreted, e.g.
 
      <syntaxhighlight lang="bash">
 
      var=abc
 
      echo "$var"
 
      </syntaxhighlight>
 
      This script prints the value of <code>var</code>, which is <code>abc</code> (instead of the literal string <code>$var</code>) to terminal.
 
    |}
 
 
 
3. create an shell variable MYIDENTITY and export it as below:
 
    <code>$ export MYIDENTITY=whoami</code>
 
    How will you list the shell variable MYIDENTITY?
 
    Execute the shell variable MYIDENTITY, what is the output?
 
  {| role="presentation" class="wikitable mw-collapsible mw-collapsed"
 
    | <strong>Answer:</strong>
 
    |-
 
    |
 
      you can list the variable by using the echo command as follows
 
      <syntaxhighlight lang="bash">
 
        $ echo $MYIDENTITY
 
        whoami
 
      </syntaxhighlight>
 
      The arguments passed to echo are printed to the standard output.
 
      <syntaxhighlight lang="bash">
 
        $ $MYIDENTITY   
 
        username
 
      </syntaxhighlight>
 
      The shell variable upon execution runs the command <code>whoami</code>, which is assigned to it. <code>whoami</code> command prints the user name of the effective user ID
 
    |}
 
 
 
  
 
{{Tutorial Navigation
 
{{Tutorial Navigation

Latest revision as of 16:25, 4 December 2020

Tutorial
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
Syllabus

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.

Video

( Slides as pdf)

Quiz

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

VARTEST=42
export VARTEST=42
env VARTEST=42

What is the naming convention for environment variables?

Use uppercase letters, i.e., VARTEST2
Use lowercase letters, i.e., vartest2
There is no naming convention, i.e., VarTest2

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 and is inherited to all programs or shell sessions that are started from this session.

What is the expected output of the following commands:
VARTEST="bla";export VARTEST="BLA";VARTEST="blub";env | grep VARTEST

VARTEST=""
VARTEST="bla"
VARTEST="blub"

Assume that the environment variable PATH has the following content /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?

/usr/bin/program
/usr/local/bin/program
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

Run the following steps in a shell:
1. Define an environment variable named TEST_VAR1="test1".
2. Define a shell variable names TEST_VAR2="test2".
3. Make sure that TEST_VAR1 is in the list of environment variables.
4. Start a new shell session within the current session.
5. Output the content of the two variables TEST_VAR1 and TEST_VAR2.
6. Change content of the environment variable to TEST_VAR1="test_new".
7. Exit the shell session and output the content of TEST_VAR1.



<< Shell Scripts

Overview

System Configuration Files >>