A Simple bash script to check the age of a user.

Sonia UmoruSonia Umoru
2 min read

Introduction

Bash scripting is a powerful way to automate tasks on Linux systems. In this article, I’ll walk you through a simple bash script that asks a user for their age and prints whether they are an adult or not.

Prerequisites

Before running this script, make sure you have the following:

  • A Linux-based system (Ubuntu, Kali, Fedora, etc.) or WSL on Windows

  • Basic understanding of the terminal and how to execute commands

  • A text editor like nano, vim, or VS Code to write the script

  • Bash shell installed (most Linux distros include it by default)

  • Permissions to execute scripts using chmod

The Bash Script.

#!/bin/bash

echo "Enter your age:"
read age

if [ "$age" -ge 18 ]; then
  echo "You are an adult."
else
  echo "You are not an adult."
fi

HOW TO SAVE

  • Press CTRL + O → Hit Enter to confirm

  • Press CTRL + X to exit

How it works in plain terms.

  1. #!/bin/bash: This tells the system to use the Bash shell to run the script.

  2. echo: Prints a message to the terminal.

  3. read age: Takes user input and stores it in a variable called age.

  4. if [ “$age” -ge 18 ]: Checks if the age is 18 or greater using -ge (greater than or equal).

  5. then and else: Based on the result, it prints whether you're an adult or not.

  6. fi: Ends the if block.

How to run the script

Save the code into a file called age.sh

Open your terminal and give it permission to execute.

chmod +x age.sh

Run it.

./age.sh
  • Visual representation

Final Thoughts

Bash scripting can be incredibly powerful, even with simple logic like this. As you get more comfortable, you can add validations, error handling, and even build more complex CLI tools.

0
Subscribe to my newsletter

Read articles from Sonia Umoru directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sonia Umoru
Sonia Umoru