Variables - Part I
The shell is a program that takes commands from the input device
(usually, a keyboard) and gives them to the operating system to
perform. On most Linux system including HPC at MSU, sh
works as the
shell. Besides sh
, other shells are available, but here, we will focus
on sh
.
This tutorial assumes you have:
- minimal programming knowledge
- minimal Linux shell knowledge (see Linux Comand Line for Beginners I for an introduction)
Writing a script
Let's create a file first.sh
on the terminal using your favorite editor. If
you rarely use any editor on Linux, this is a good chance to start using one of
them (Linux text
editors). A popular
one for which is already installed on the HPCC is
nano
.
first.sh | |
---|---|
1 2 3 |
|
The first line tells Linux that the file is to be executed by /bin/sh
.
#!
will be explained later. The second line begins with #
. This
special character makes the line as a comment, and it is ignored by the
shell. The only exception is when the first line of the file starts with
#!
.
The third line runs a command echo
, with two parameters/arguments
'Hello' and 'World'. The symbol #
on line 3 makes the rest of the line
a comment.
Now, after exiting the text editor, run chmod u+x first.sh
on the command
line to make the text file executable, then run ./first.sh
.
1 2 3 |
|
Using variables
Next, let's expand on first.sh
by using variables. Create a new script called
var1.sh
with the following content:
var1.sh | |
---|---|
1 2 3 |
|
This assigns the string Hello World
to the variable MY_MESSAGE
then
echo
command prints the value of the variable. Note that you need
the quotes around the string.
To use variables, $
is required in front of variables. If you use
echo MY_MESSAGE
in the above, it will print MY_MESSAGE
instead of
Hello World
. The scope of the variable MY_MESSAGE
is only inside of
the script, and when the script finished the variable is empty (don't
forget to use chmod u+x var1.sh
to make a script executable).
1 2 3 4 5 |
|
In addition, if you use a variable without declaration, it returns empty string. There is no warning or error message.
Exploring variable scope
Let's create a shell script var2.sh
.
var2.sh | |
---|---|
1 2 3 4 |
|
Then run the script. You can use chmod u+x
to make var2.sh
executable and
run it as the previous examples or use the sh
command:
1 2 3 |
|
The first MYVAR
is empty because it is not declared. The second MYVAR
has the value we expected. The scope of the variables in a script is
only inside the script. For example, MYVAR
is only valid inside
var2.sh
and when the script finishes, MYVAR
is empty again.
1 2 3 4 5 6 |
|
Saving variables
You can declare variables with export
command in a shell. Check the
scope of variables.
1 2 3 4 5 6 7 |
|
Extended example
You can use variables in many ways. Here is one example.
var3.sh | |
---|---|
1 2 3 4 5 6 |
|
Let's run the script.
1 2 3 4 5 6 7 8 |
|
Notice that we use curly braces for a file name. If you use $USER_NAME_file
instead of ${USER_NAME}_file
, the shell returns the empty string because
there is no variable called USER_NAME_file
in the script.