Introduction to Linux in HPC/Environment variables
Introduction to Linux in HPC/Environment variables /
Jump to navigation
Jump to search
Revision as of 14:52, 22 November 2020 by Robert-schade-e757@uni-paderborn.de (talk | contribs)
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; 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?
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:
#!/bin/bash
: the shebang line for bash script
echo "I am $USER, my home directory is $HOME"
: the variables $USER
and $HOME
are your username and home directory, respectively. This line prints a sentence embedded with your username and home directory.
olddate=$(date)
assigns the output of the date command to the variable olddate as a string.
sleep 10s
puts the terminal into idle for 10 seconds.
echo "old date: ${olddate}"
prints the previously saved date in the olddate variable. The form of ${olddate}
is for using (or referencing) this variable.
echo "new date: $(date)"
prints the current date, in which the output of the command date will take the place of $(date)
.
2. What do different types of quotes (single '
vs. double "
) do?
Answer:
single quote '
gives literal string, the variable will not be interpreted, e.g.
var=abc
echo '$var'
This script prints the literal string $var
(instead of its value abc
) to terminal.
double quote "
allows the variable to be interpreted, e.g.
var=abc
echo "$var"
This script prints the value of var
, which is abc
(instead of the literal string $var
) to terminal.
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 whoami
, which is assigned to it. whoami
command prints the user name of the effective user ID