Skip to content

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, bash works as the shell. Besides bash, other shells are available, but here, we will focus on bash.

This tutorial assumes you have:

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
#!/bin/bash
# This is a comment!
echo Hello World         # This is a comment, too!

The first line tells Linux that the file is to be executed by /bin/bash. #! 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.

input
1
2
chmod u+x first.sh
./first.sh
output
1
Hello World

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
#!/bin/bash
MY_MESSAGE="Hello World"
echo $MY_MESSAGE

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).

input
1
./var1.sh
output
1
Hello World
input
1
echo $MY_MESSAGE
output
1

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
#!/bin/bash 
echo "MYVAR is: $MYVAR"
MYVAR="hi there"
echo "MYVAR is: $MYVAR"

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:

input
1
bash var2.sh
output
1
2
MYVAR is:
MYVAR is: hi there

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.

input
1
./var2.sh
output
1
2
MYVAR is:
MYVAR is: hi there
input
1
echo $MYVAR
output
1

Saving variables

You can declare variables with export command in a shell. Check the scope of variables.

input
1
2
3
MYVAR="hello there"
export MYVAR
./var2.sh
output
1
2
MYVAR is: hello there
MYVAR is: hi there
input
1
2
$ echo $MYVAR
hello there

Extended example

You can use variables in many ways. Here is one example.

var3.sh
1
2
3
4
5
6
#!/bin/bash
echo "What is your name?"
read USER_NAME
echo "Hello $USER_NAME"
echo "I will create a file called ${USER_NAME}_file"
touch ${USER_NAME}_file

Let's run the script.

input
1
2
chmod u+x var3.sh
./var3.sh
output
1
2
3
4
5
6
What is your name?
ICER
Hello ICER
I will create a file called ICER_file
$ls -l ICER_file
-rw-r--r--  1 choiyj  staff  0 Jan  5 14:08 ICER_file

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.