Creating a Short Math Quiz with Bash Scripting
Tags: bash, scripting
Published: 2021-03-07 04:12:56

_This article explains how to create and run a short math quiz using Bash scripting._

# Step 1: Create a .sh file

In Terminal, navigate to a directory where you'll want to put your Bash script. In that directory, open your preferred text editor and create a `quiz.sh` file. The `.sh` lets your text editor know that you're making a Bash `sh`ell script file. 

# Step 2: Write the Bash script

The math quiz Bash script in its entirety looks like this:
```bash
1    #!/bin/bash
2
3    quiz() {
4      echo "Starting quiz..."
5      random1=$(($RANDOM / 100))
6      random2=$(($RANDOM / 100))
7      result=$(($random1 + $random2))
8  
9      echo "What is $random1 + $random2?"
10     read response
11     if [ "$response" == $result ]
12     then
13       echo "You got it."
14     else 
15       echo "That is incorrect."
16     fi
17   }
18
19   echo "Begin math quiz? (y/n)"
20   read response
21   if [ "$response" == "y" ] || [ "$response" == "Y" ]
22   then 
23     SECONDS=0
24     quiz
25     echo "The quiz took you $SECONDS seconds to complete."
26   else 
27     echo "Exiting..."
28   fi
```

[HERE IS THE CODE WITHOUT COPYABLE LINE NUMBERS](https://github.com/colerau/bash-math-quiz/blob/master/quiz.sh)

# Step 3: Make the script file executable

In the directory that your script file lives in, enter the command `chmod +x yourScriptFile.sh`. In the context of this article, since our script file is called `quiz.sh`, we'll enter `chmod +x quiz.sh`. 

# Step 4: Run the script file

In the script file's directory, enter the command `./yourScriptFile.sh`. In the context of this article, since our script file is called `quiz.sh`, we'll enter in `./quiz.sh`. Alternatively, we can run the script file with `bash quiz.sh`. 

***

# Bash Script Explanation

In reference to the Bash script below...
```bash
1    #!/bin/bash
2
3    quiz() {
4      echo "Starting quiz..."
5      random1=$(($RANDOM / 100))
6      random2=$(($RANDOM / 100))
7      result=$(($random1 + $random2))
8  
9      echo "What is $random1 + $random2?"
10     read response
11     if [ "$response" == $result ]
12     then
13       echo "You got it."
14     else 
15       echo "That is incorrect."
16     fi
17   }
18
19   echo "Begin math quiz? (y/n)"
20   read response
21   if [ "$response" == "y" ] || [ "$response" == "Y" ]
22   then 
23     SECONDS=0
24     quiz
25     echo "The quiz took you $SECONDS seconds to complete."
26   else 
27     echo "Exiting..."
28   fi
```
...we begin with line 19, where we use `echo` to output the string `"Begin math quiz? (y/n)"` to the console. 

_Before we go further, it's essential to note that `#!/bin/bash` is written at the top of the file. This is needed so that your computer knows to use the Bash interpreter for your file's code._

Moving on, line 20 initializes a variable `response` and uses `read` to let the user input its value. In other words, whatever the user types into the console will become the value of `response`. 

Line 21 starts a conditional statement. If the value of response (`$response`) is equal to "y" or "Y", the code after the `then` is executed.

Three things to note: 
* The variable `response` is prepended with a `$` because in Bash, to get the value of a variable, you need to include a `$` before the variable name.

* Second, we're not testing for equality with `-eq` because that is for _integer_ equality tests; here, because we're testing the equality between two _strings_, `==` is used.

* Third, the quotes around the `$response` variables on line 21 are there just to prevent bugs.

On line 24, if the user types in "y" or "Y", the quiz function is called. The quiz function is defined in lines 3 - 17. 

If the user types in something else, the `else` code is executed, which just outputs "Exiting..." to the screen. The `fi` is how you end an if...else conditional code block in Bash. 

To gauge how many seconds it took the user to complete the quiz, we declare the built-in variable `SECONDS` on line 23, give it an initial value of 0, run the quiz function, then when the user is finished with the quiz, we use `$SECONDS` on line 25 to provide the amount of time elapsed. 

### The Code Inside the Quiz Function

If the user typed "y" or "Y", the quiz function code will be run. Inside the quiz function, on line 5, we assign a random integer between 0 and 327 to the `random1` variable using the following code: 
```bash
random1=$(($RANDOM / 100))
``` 
The `$(( ))` allows us to perform an arithmetic expression and assign it to a variable. 

On line 6, we get a second random integer. 

On line 7, we save the sum of the two integers to a variable called `result`. 

From lines 11 - 16, if the user got the question right (if the `response` variable is equal to the `result` variable), `"You got it."` is outputted to the screen. Else, `"That is incorrect."` is displayed to the user.