Writing Your First Interactive Bash Script (With User Input)

BackerLeader posted 3 min read

Introduction

Bash scripting is one of the most powerful tools in any developer’s or system administrator’s toolbox. But it doesn’t have to be intimidating! In this guide, you'll learn how to write your first interactive Bash script—one that asks for user input, responds to it, and performs simple actions.

By the end of this tutorial, you’ll have a solid foundation to build more complex and useful scripts.


What is a Bash Script?

A Bash script is simply a text file containing a series of commands that the Linux shell can execute. It automates repetitive tasks, reduces errors, and saves you time.


What You'll Learn in This Guide:

  • How to create a basic Bash script
  • How to take user input using read
  • How to use simple if conditions
  • How to make the script interactive

️ Prerequisites

  • ✅ A Linux machine or a Linux VPS
  • ✅ Terminal access
  • ✅ Basic knowledge of using the command line (no coding experience required)

Step 1: Create a New Bash Script

Open your terminal and create a new file called interactive.sh:

nano interactive.sh

At the top of the file, add the shebang line:


 #!/bin/bash

This tells the system to use the Bash interpreter to run the script.


️ Step 2: Add User Input with read

Let’s make the script ask for your name and greet you.

 #!/bin/bash

echo "Hello! What's your name?"
read user_name

echo "Nice to meet you, $user_name!"
  • echo prints text to the screen.
  • read waits for the user to type something and stores it in a variable (user_name in this case).

Step 3: Add Some Interaction

Let’s make it a bit more fun by asking another question.

 #!/bin/bash

echo "Hello! What's your name?"
read user_name

echo "Nice to meet you, $user_name!"
echo "How are you feeling today? (good/bad)"
read feeling

if [ "$feeling" == "good" ]; then
    echo "That's awesome to hear, $user_name!"
elif [ "$feeling" == "bad" ]; then
    echo "Sorry to hear that, $user_name. Hope your day gets better!"
else
    echo "Hmm, I didn't understand that, but I hope you're doing okay!"
fi
  • ✅ The if statement checks what the user typed.
  • ✅ You can respond differently based on different inputs.

Step 4: Make It Executable

Before running the script, you need to give it permission to execute:

chmod +x interactive.sh

Now you can run it:

./interactive.sh

Step 5: Expanding the Script (Bonus)

Here’s an improved version that adds a simple calculator:

#!/bin/bash

echo "Welcome to the Simple Bash Calculator!"
echo "What is your name?"
read user_name

echo "Hello, $user_name! Let's do a quick calculation."
echo "Enter first number:"
read num1

echo "Enter second number:"
read num2

result=$((num1 + num2))

echo "The sum of $num1 and $num2 is: $result"
  • ✅ This script adds two numbers that the user provides.
  • $((...)) is used for arithmetic operations.

Key Takeaways

  • read allows you to accept user input in Bash.
  • Variables store data you can use later.
  • Simple conditions with if make your scripts interactive.
  • Bash is a powerful starting point for automating tasks.

Why Should You Care?

  • ✅ Automate repetitive tasks
  • ✅ Learn the basics of programming logic without complex syntax
  • ✅ Control your servers or homelab devices efficiently

Even small scripts like this can save you time and reduce mistakes.


Next Steps:

If you enjoyed this tutorial, here’s what you can do next:

  1. Try adding more options or calculations.
  2. Learn about loops in Bash to repeat actions.
  3. Use your script to manage files or services on your VPS.

Conclusion

Writing interactive Bash scripts doesn’t have to be scary. With just a few simple commands, you can create tools that make your work easier and more fun. Practice these basics, and you’ll soon be automating your way through everyday Linux tasks like a pro!


If you read this far, tweet to the author to show them you care. Tweet a Thanks

Great intro to Bash scripting—thanks for making it so approachable! I like how you break down the steps clearly for beginners. Have you considered adding examples of loops or functions next? Also, using H1 and H2 headings could improve readability and help users navigate the tutorial better.

More Posts

Performance of Performance Testing: JMeter Script Optimization with VisualVM

bugnificent - Jun 17

Build Your Own DIY Stream Deck with Raspberry Pi Pico W and CircuitPython

Gift Balogun - Jul 14

Versioning Your API the Right Way (REST Best Practices)

Gift Balogun - Aug 11

Keep your customers happy, boost MRR

kodwings - Jun 4

Security Testing for SDETs: Automate Vulnerability Scans with OWASP ZAP

bugnificent - Mar 28
chevron_left