Interactive Bash Games

Writing command-line games is one of the most fun ways to learn Bash scripting. It teaches you how to read user inputs, construct game loops, manage conditionals, and write to standard output.

We built an interactive Bash Emulator right on this page. Below, you can play standard command-line games (like Guess the Number and Tic-Tac-Toe) directly in your browser, or cat their source code to see how they are constructed!


In-Browser Terminal Emulator

Try running ls to list the files, cat guess_number.sh to read the script source, or run a game by typing:


Game Source Code Reference

Here is the actual Bash source code that is virtualized and running in the terminal emulator above. You can copy and run these scripts directly on your local Linux machine!

1. Guess the Number (guess_number.sh)

#!/bin/bash
# guess_number.sh - Guess the Number Game in Bash

secret=$(( (RANDOM % 100) + 1 ))
attempts=0

echo "-------------------------------------------------"
echo "SH-GAME: Guess the Number script initiated."
echo "I've chosen a random number between 1 and 100."
echo "Type 'exit' to quit."
echo "-------------------------------------------------"

while true; do
    read -p "Number (1-100) > " guess
    
    if [[ "$guess" == "exit" ]]; then
        echo "Script terminated."
        exit 0
    fi
    
    # Check if input is a number
    if ! [[ "$guess" =~ ^[0-9]+$ ]]; then
        echo "Error: Please enter a valid number."
        continue
    fi
    
    attempts=$((attempts + 1))
    
    if [[ $guess -eq $secret ]]; then
        echo "[SUCCESS] Correct! You guessed it in $attempts attempts!"
        exit 0
    elif [[ $guess -lt $secret ]]; then
        echo "Attempt $attempts: $guess is too low. Try higher!"
    else
        echo "Attempt $attempts: $guess is too high. Try lower!"
    fi
done

2. Tic-Tac-Toe (tic_tac_toe.sh)

#!/bin/bash
# tic_tac_toe.sh - Play Tic-Tac-Toe against CPU

board=(" " " " " " " " " " " " " " " " " ")

print_board() {
    echo "   ${board[0]} | ${board[1]} | ${board[2]}"
    echo "  -----------"
    echo "   ${board[3]} | ${board[4]} | ${board[5]}"
    echo "  -----------"
    echo "   ${board[6]} | ${board[7]} | ${board[8]}"
}

check_win() {
    local p=$1
    # Check rows, columns, and diagonals
    if [[ ${board[0]} == $p && ${board[1]} == $p && ${board[2]} == $p ]] ||
       [[ ${board[3]} == $p && ${board[4]} == $p && ${board[5]} == $p ]] ||
       [[ ${board[6]} == $p && ${board[7]} == $p && ${board[8]} == $p ]] ||
       [[ ${board[0]} == $p && ${board[3]} == $p && ${board[6]} == $p ]] ||
       [[ ${board[1]} == $p && ${board[4]} == $p && ${board[7]} == $p ]] ||
       [[ ${board[2]} == $p && ${board[5]} == $p && ${board[8]} == $p ]] ||
       [[ ${board[0]} == $p && ${board[4]} == $p && ${board[8]} == $p ]] ||
       [[ ${board[2]} == $p && ${board[4]} == $p && ${board[6]} == $p ]]; then
        return 0
    fi
    return 1
}

echo "-------------------------------------------------"
echo "SH-GAME: Tic-Tac-Toe script initiated."
echo "Positions are mapped 1-9 starting from top-left."
echo "You are 'X', CPU is 'O'. Type 'exit' to quit."
echo "-------------------------------------------------"
print_board

while true; do
    read -p "Your Move (1-9) > " move
    
    if [[ "$move" == "exit" ]]; then
        echo "Script terminated."
        exit 0
    fi
    
    idx=$((move - 1))
    if ! [[ "$move" =~ ^[1-9]$ ]] || [[ "${board[$idx]}" != " " ]]; then
        echo "Invalid move. Choose an empty position 1-9."
        continue
    fi
    
    board[$idx]="X"
    print_board
    
    if check_win "X"; then
        echo "[WIN] Congratulations! You beat the CPU!"
        exit 0
    fi
    
    # Check draw
    if [[ ! " ${board[@]} " =~ " " ]]; then
        echo "[DRAW] Tie game!"
        exit 0
    fi
    
    echo "CPU is calculating..."
    for i in {0..8}; do
        if [[ "${board[$i]}" == " " ]]; then
            board[$i]="O"
            echo "CPU played position $((i + 1)):"
            print_board
            break
        fi
    done
    
    if check_win "O"; then
        echo "[DEFEAT] The CPU won. Try again!"
        exit 0
    fi
done