Difference between revisions of "Introduction to Linux in HPC/Environment variables"
Introduction to Linux in HPC/Environment variables
Jump to navigation
Jump to search
m (Intro) |
m (quiz 1) |
||
Line 14: | Line 14: | ||
=== Quiz === <!--T:5--> | === Quiz === <!--T:5--> | ||
+ | {{hidden begin | ||
+ | |title = How do you define an environment variable VARTEST and assign the value 42 to it? | ||
+ | }} | ||
+ | <quiz display=simple> | ||
+ | { | ||
+ | |type="()"} | ||
+ | - <code>VARTEST=42</code> | ||
+ | || Explanation: This statement would only define VARTEST as a shell variable and not as an environemnt variable. | ||
+ | + <code>export VARTEST=42</code> | ||
+ | || Explanation: The keyword <code>export</code> makes VARTEST an environment variable. | ||
+ | - <code>env VARTEST=42</code> | ||
+ | || | ||
+ | </quiz> | ||
+ | {{hidden end}} | ||
+ | |||
+ | {{hidden begin | ||
+ | |title = What is the naming convention for environment variables? | ||
+ | }} | ||
+ | <quiz display=simple> | ||
+ | { | ||
+ | |type="()"} | ||
+ | + <code>Use uppercase letters</code> | ||
+ | || Explanation: It is recommended to only use uppercase letters, numbers, and underscores in the names of environment variables. | ||
+ | - <code>Use lowercase letters</code> | ||
+ | || | ||
+ | - <code>There is no naming convention.</code> | ||
+ | || | ||
+ | </quiz> | ||
+ | {{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 as well as 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 | {{hidden begin | ||
− | |title = | + | |title = What is the expected output of the following commands? <code>VARTEST="bla"; export VARTEST </code> |
}} | }} | ||
<quiz display=simple> | <quiz display=simple> | ||
Line 27: | Line 71: | ||
- <code>var=="value"</code> | - <code>var=="value"</code> | ||
|| | || | ||
+ | </quiz> | ||
+ | {{hidden end}} | ||
+ | |||
+ | {{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? | ||
+ | }} | ||
+ | <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> | </quiz> | ||
{{hidden end}} | {{hidden end}} |
Revision as of 14:46, 22 November 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
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?
How do you add the directory
/opt/bin/
to the PATH
environment variable and make sure that it is searched last?
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
Answer: |
One possible way to write the script is #!/bin/bash
echo "I am $USER, my home directory is $HOME"
olddate=$(date)
sleep 10s
echo "old date: ${olddate}"
echo "new date: $(date)"
Explanation: |
2. What do different types of quotes (single'
vs. double"
) do?
Answer: |
single quote
var=abc
echo '$var'
This script prints the literal string var=abc
echo "$var"
This script prints the value of |
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?
Answer: |
you can list the variable by using the echo command as follows $ echo $MYIDENTITY
whoami
The arguments passed to echo are printed to the standard output. $ $MYIDENTITY
username
The shell variable upon execution runs the command |