Difference between revisions of "Introduction to Linux in HPC/Environment variables"
Introduction to Linux in HPC/Environment variables
Jump to navigation
Jump to search
(Add link to PDF slides) |
m (Tweak page sorting and title) |
||
(13 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:Tutorials]] | + | [[Category:Tutorials|Environment Variables (Linux)]]<nowiki /> |
− | {{ | + | {{DISPLAYTITLE:Environment Variables (Linux)}}<nowiki /> |
− | + | {{Syllabus Introduction to Linux}}<nowiki /> | |
− | |||
__TOC__ | __TOC__ | ||
+ | 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 === <!--T:5--> | === Video === <!--T:5--> | ||
− | <youtube width="600" height=" | + | <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 14: | Line 14: | ||
=== Quiz === <!--T:5--> | === Quiz === <!--T:5--> | ||
+ | {{hidden begin | ||
+ | |title = How do you define an environment variable <code>VARTEST</code> and assign the value <code>42</code> to it? | ||
+ | }} | ||
+ | <quiz display=simple> | ||
+ | { | ||
+ | |type="()"} | ||
+ | - <code>VARTEST=42</code> | ||
+ | || Explanation: This statement would only define <code>VARTEST</code> as a shell variable and not as an environemnt variable. | ||
+ | + <code>export VARTEST=42</code> | ||
+ | || Explanation: The keyword <code>export</code> makes <code>VARTEST</code> an environment variable. | ||
+ | - <code>env VARTEST=42</code> | ||
+ | || | ||
+ | </quiz> | ||
+ | {{hidden end}} | ||
{{hidden begin | {{hidden begin | ||
− | |title = | + | |title = What is the naming convention for environment variables? |
}} | }} | ||
<quiz display=simple> | <quiz display=simple> | ||
{ | { | ||
|type="()"} | |type="()"} | ||
− | + <code> | + | + Use uppercase letters, i.e., <code>VARTEST2</code> |
− | || Explanation: | + | || Explanation: It is recommended to only use uppercase letters, numbers, and underscores in the names of environment variables. |
− | - <code> | + | - Use lowercase letters, i.e., <code>vartest2</code> |
|| | || | ||
− | - <code> | + | - There is no naming convention, i.e., <code>VarTest2</code> |
|| | || | ||
</quiz> | </quiz> | ||
{{hidden end}} | {{hidden end}} | ||
− | === | + | {{hidden begin |
+ | |title = Which statement is correct for environment variables? | ||
+ | }} | ||
+ | <quiz display=simple> | ||
+ | { | ||
+ | |type="()"} | ||
+ | - 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. | ||
+ | || Explanation: Environment variables are inherited from parent processes to their child processes. | ||
+ | </quiz> | ||
+ | {{hidden end}} | ||
+ | {{hidden begin | ||
+ | |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> | ||
+ | { | ||
+ | |type="()"} | ||
+ | - <code>VARTEST=""</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. | ||
+ | </quiz> | ||
+ | {{hidden end}} | ||
− | + | {{hidden begin | |
− | + | |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> | |
− | + | { | |
− | + | |type="()"} | |
− | + | + <code>/usr/bin/program</code> | |
− | + | || Explanation: <code>/usr/bin/program</code> is executed because the shell searches for <code>program</code> starting from the first directory in <code>PATH</code> which is <code>/usr/bin</code> in this case. | |
− | + | - <code>/usr/local/bin/program</code> | |
− | + | || | |
− | + | - The shell will ask you which one to execute. | |
− | + | || | |
+ | </quiz> | ||
+ | {{hidden end}} | ||
− | + | {{hidden begin | |
+ | |title = How do you add the directory <code>/opt/bin/</code> to the <code>PATH</code> environment variable and make sure that it is searched last? | ||
+ | }} | ||
+ | <quiz display=simple> | ||
+ | { | ||
+ | |type="()"} | ||
+ | - <code>export PATH=/opt/bin/:$PATH</code> | ||
+ | || Explanation: In this case, <code>/opt/bin</code> would be searched FIRST, i.e., the path is prepended. | ||
+ | + <code>export PATH=$PATH:/opt/bin/</code> | ||
+ | || Explanation: This is the correct way. | ||
+ | - <code>export PATH=/opt/bin/</code> | ||
+ | || Explanation: This would overwrite all other directories in the <code>PATH</code> variables and no the programs would be found anymore. | ||
+ | </quiz> | ||
+ | {{hidden end}} | ||
− | + | === Exercises in Terminal === <!--T:5--> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Run the following steps in a shell: | |
+ | 1. Define an environment variable named <code>TEST_VAR1="test1"</code>. | ||
+ | 2. Define a shell variable names <code>TEST_VAR2="test2"</code>. | ||
+ | 3. Make sure that <code>TEST_VAR1</code> 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 <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> | ||
|- | |- | ||
− | | | + | | |
− | + | The commands to enter are: | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | export TEST_VAR1="test1" | |
− | + | TEST_VAR2="test2" | |
+ | env | grep TEST_VAR1 | ||
+ | bash | ||
+ | echo $TEST_VAR1 | ||
+ | echo $TEST_VAR2 | ||
+ | export TEST_VAR1="test_new" | ||
+ | exit | ||
+ | echo $TEST_VAR1 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | |} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | |||
− | + | {{Tutorial Navigation | |
− | + | | previous = [[Introduction_to_Linux_in_HPC/Shell_scripting | Shell Scripts ]] | |
− | + | | main = [[Introduction_to_Linux_in_HPC | Overview ]] | |
− | + | | next = [[Introduction_to_Linux_in_HPC/System_configuration_files | System Configuration Files ]] | |
+ | }} |
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 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
Quiz
How do you define an environment variable
VARTEST
and assign the value 42
to it?
What is the naming convention for environment variables?
Which statement is correct for environment variables?
What is the expected output of the following commands:
VARTEST="bla";export VARTEST="BLA";VARTEST="blub";env | grep VARTEST
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?
How do you add the directory
/opt/bin/
to the PATH
environment variable and make sure that it is searched last?
Exercises in Terminal
Run the following steps in a shell: 1. Define an environment variable namedTEST_VAR1="test1"
. 2. Define a shell variable namesTEST_VAR2="test2"
. 3. Make sure thatTEST_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 variablesTEST_VAR1
andTEST_VAR2
. 6. Change content of the environment variable toTEST_VAR1="test_new"
. 7. Exit the shell session and output the content ofTEST_VAR1
.
Answer: |
The commands to enter are: export TEST_VAR1="test1"
TEST_VAR2="test2"
env | grep TEST_VAR1
bash
echo $TEST_VAR1
echo $TEST_VAR2
export TEST_VAR1="test_new"
exit
echo $TEST_VAR1
|