Bash is powerful! Lets build a simple number guesser game.
Tags: bash, programming, linux, ubuntu
Published: 2022-10-08 20:41:34

## Why Bash? and Why a number guesser?
A number guesser is a less complex project to build and you’ll learn how to work with if, else if, else statements, functions, user input, and the random module (Of course these programming concepts will be done the Bash way!). Wait but what is Bash? Bash is the default login shell for most linux systems. Meaning that to get good at linux it helps to get good at Bash. Bash can be ran straight from the linux terminal and can also be added to a text file and made executable using the linux command line on the spot! Also most (if not all) linux distros support bash so no need to download anything new. Just run it on the fly.

## Alright lets build our game! Here’s the plan!
First we need a way to generate a random number (we’ll use the built in random function) and assign it to a variable that way we can reference the variable and compare it to the user input. Next we need to get user input. Then finally we need to compare the user input to the random number. If the user is guessed right then well end the game and let the user know they won! Else we need to continue the game until the user wins.

##Alright simple enough, Lets start writing the code!
First we append the shebang (which is short for sharp and bang) and follow that with our /bin/bash path. We do this to ensure that our linux operating system executes the bash file properly. It should look like this.

```
#!/bin/bash
```

Next we need a way to read user input through the command line. For this well use the built in read command. Well append that with -p to assign it to a variable (that way we can reference it later). This should look like this.

```bash
read -p "What number would you like to pick?" user_guess
```

So read -p (read command), “what number would you like to pick” (the printed string), then user_guess (the variable name with assigned value).

Next we need to compare the user input to the random number to determine whether or not the game player is right. For this well use if/else statements. If/else statements check if a condition is true and will execute a code block depending on condition. In bash this logical statements looks like this.

```bash
if [conditional] then (do something here) else (do something) fi
```

We also want to print things to the terminal to let the user know whether or not hes correct. For this well use echo. Echo (actually echo lowercase) is another built in command and is used to print strings to the terminal. Echo looks like this.

```bash
echo "this string will be printed to the terminal screen"
```

So combining echo and our if statement, our actual code should look like this.

```bash
if [ ${random_number} == ${user_guess} ]
then
    echo "You guessed right!"
else 
    echo "You guessed wrong"
fi
```

##Awesome! Lets build the final product!
Now that we have the bare bones logic in order, from here i’ll clean up the code a little and make it more readable. I’ll also add some logic to a function and call that function because (in my opinion) it makes everything more readable, But more importantly I will be using recursion to recall the function if the current user guesses wrong. So here’s the finished product!

```bash
echo "Random number guesser!"                                                                       
                                                                                                   
read -p "What is the random number you would like to guess? Please    
         pick a number between 0 and 9" user_guess
                                                                                                   
function number_guess() {                                                                            
   if [ ${random_number} == ${user_guess} ]                                                        
   then                                                                                            
       echo "You guessed right! Congratulations ${random_number} is 
             equal to ${user_guess}"        
   else                                                                                            
        echo "You guessed wrong! Try again."                                                        
        read -p "What is the random number you would like to guess? 
                please pick a number between 0 and 9" user_guess
        number_guess                                                                                
    fi                                                                                              
}                                                                                                   
                                                                                                    
number_guess
```

As you can see building a simple number guessing game isn’t super difficult. It just takes a few lines of code and because we utilized bash this script is super easy to run from the command line.

##Thank you for reading my article!
Wondering what i’m up to? You can check me out here at http://davidesquerra.com/ or here at https://github.com/Davidfree2

If you really enjoyed this article drop a clap down below or even a follow! Let me know what you think and if you want to see more content like this. Drop a question too if there’s something that’s not clear enough. Thank you for reading.