Variables

Now that we have covered Hello World, we will go into variables. But first, let me cover another POSIX utility to use in shell.

We will be making lots of source code files, so it would be best to organize them in directories. You can make a directory with the mkdir utility. Let’s create a directory called “Polyglot”. We pass the string “Polyglot” as an argument to mkdir. Open a terminal, change to your home directory if you are not already there, and enter this:

$ mkdir "Polyglot"

Now that you have made the directory, change your current working directory to it:

$ cd Polyglot

Here, lets make a few more directories. We can pass multiple arguments to mkdir and it will make them all:

$ mkdir "Shell" "Python" "C"

Change to the Shell directory, and lets take a look at variables. I’m going to assume you remember, or will refer back to, how to do the things we covered before.

Declaring Variables

Create a file called variables.sh using touch as before. Now open it with the text editor you are using. Start writing the shell script as before with the shebang and declare a variable called a:

#!/bin/sh

a=1

Here we are declaring a variable which is named a, and initializing it with the value of the number 1. The value should be familiar. Shell uses the equals character to assign the values to varibles. The value on it’s right is assigned to the variable named on it’s left. In the shell syntax there can be no spaces before or after the equals sign. a = 1 will not work.

Think of a variable as a box with a label on the side. In the example above, a is the label, and the number 1 is what we put in the box.

Using the value of a variable

In Shell we use a variable by referring to it by it’s name, but the name must be preceded by a dollar sign like so: $a.

Let us use echo again to print out the value of our variable a. Add to your script as below:

#!/bin/sh

a=1
echo $a

Save your variables.sh file, and as before make it executable with this:

$ chmod u+x variables.sh

Then run it with this:

$ ./variables.sh

You should see the number 1 output, similarly to the output of our hello.sh script from the previous lesson.

Change to your Python directory and create a file there called variables.py. Edit that file and put the following lines in it.

#!/usr/bin/env python3

a = 1
print(a)

Note the differences between the Python syntax and Shell syntax. Again we use the Python specific shebang line. In Python, you can put spaces between the equal sign in the variable assignment. And as before Python has the print function rather than the echo command that the shell uses.

When we reference a variable in Python there is no need to prefix a dollar sign as there is in shell. We pass the exact name of the variable into the print function.

Save this file, and as before, then back in the terminal make it executable and run it as follows:

$ chmod u+x variables.py

$ ./variable.py

The output should be the same as it was for the shell script.

For C we need to specify the type of the variable. With shell and Python, the interpreter will figure out the type of the variable when the program is run. For the current example this is a number, specifically an integer. Integers are whole numbers. They cannot contain a fractional part. Some examples of integers are: 1, 2, 3, 99, -5, and 10011. These numbers are not integers: 1.2, 3.333, or 1/2.

You could think of variable types like different shaped boxes for different things you want to put in them. Shoes might fit in a shoe box, but a violin would not. By that same token, most shoes would not fit in a violin box.

Shell treats most data as text, so all it’s variables are strings. Python will grab the right box at the moment you want to put something in it. So once you assign a value to a variable, it will treat that variable as that type. This takes some time for the computer to handle while running the program, but it is convenient for the programmer. C wants you to tell it exactly what type of thing you will keep in the box when you write the code. In other words you must specify the type of the variable.

An integer in C is specified by the abbreviation int.

The other difference with C is that it’s printf function cannot directly print an integer. It is designed to print format strings. It can take many variables as additional optional arguments, but it should first be given a format string.

To have an integer variable printed in a format string, you need to specify where it will go with the special characters %d. This is called a format specifier. The d here stands for decimal, as in a decimal integer, and the percent sign is treated specially in a format string for this purpose.

We will use a format string like so: "%d\n". As I showed you before, the string is denoted with double quotes, and ends with the characters that make a newline. We have put the decimal integer format specifier before the newline.

So our C program will look like this:

#include <stdio.h>

int main()
{
    int a = 1;
    printf("%d\n", a);
}

Change to your C directory and put the above C code in a new file called variable.c. compile it by entering:

$gcc variable.c -o variable

The output file ‘variable’ should already be executable. Execute it with:

$./variable

As you can see. the printf function is taking two arguments. They are separated with a comma. This is how multiple arguments are passed in to functions in C. The first argument is our new format string, and the second argument is the variable of type int which is named a, and has been given the value 1.

You can save this file