Skip to main content

Command Palette

Search for a command to run...

Creating a Custom Greeting with Bash Scripting

Published
2 min read

Bash scripting is a powerful way to automate tasks in Unix-based systems. In this post, we'll create a simple Bash script that prompts the user for their name and age and then provides a personalized greeting with conditional messages based on their age.

What You’ll Need

  • A Unix/Linux environment

  • A text editor (like nano, vim, or any other)

Step-by-Step Guide

  1. Open your terminal.

  2. Create a new Bash script file. You can name it greeting.sh.

     touch greeting.sh
    
  3. Open the file in your text editor.

     nano greeting.sh
    
  4. Add the following code to the script:

     #!/bin/bash
    
     # Prompt the user for their name
     read -p "Please enter your name: " name
    
     # Prompt the user for their age
     read -p "Please enter your age: " age
    
     # Conditional messages based on age
     if [ "$age" -lt 18 ]; then
         echo "Hello, $name! You're just a teenager!"
     elif [ "$age" -lt 65 ]; then
         echo "Hi, $name! You're in your prime!"
     else
         echo "Greetings, $name! You're a wise individual!"
     fi
    
  5. Save and exit the text editor. If you're using nano, press CTRL + X, then Y, and hit Enter.

  6. Make the script executable:

     chmod +x greeting.sh
    
  7. Run the script:

     ./greeting.sh
    

How It Works

  • The script uses the read command to take user input for their name and age.

  • The if, elif, and else statements check the age and print a corresponding message.

Conclusion

This simple script demonstrates how easy it is to use Bash for basic scripting tasks. You can expand this script by adding more conditions or functionalities, such as validating user input or adding more personalized messages.

Happy scripting!


More from this blog

Amazon S3 Storage Classes

43 posts