0
0
Bash Scriptingscripting~15 mins

Function definition in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Function definition
📖 Scenario: You are creating a small bash script to greet users by name. This script will use a function to keep the greeting message reusable and neat.
🎯 Goal: Build a bash script that defines a function called greet which takes a name as input and prints a greeting message. Then call this function with a specific name.
📋 What You'll Learn
Create a function named greet
The function should accept one argument (a name)
The function should print Hello, <name>! where <name> is the argument
Call the greet function with the name Alice
Print the greeting message to the terminal
💡 Why This Matters
🌍 Real World
Functions in bash scripts help automate repetitive tasks like sending messages, processing files, or managing system settings.
💼 Career
Knowing how to write and use functions in bash is essential for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps
1
Create a function named greet
Write a bash function named greet that takes one argument called name. Inside the function, write a comment # Function to greet a user.
Bash Scripting
Need a hint?

Start your function with function greet() { and end with }. Use a comment inside the function.

2
Add code to print a greeting message inside the greet function
Inside the greet function, write a echo command that prints Hello, <name>! where <name> is the first argument passed to the function, accessed as $1.
Bash Scripting
Need a hint?

Use echo "Hello, $1!" to print the greeting with the first argument.

3
Call the greet function with the name Alice
After the function definition, call the greet function with the argument Alice to print the greeting.
Bash Scripting
Need a hint?

Call the function by writing greet Alice after the function.

4
Print the greeting message
Run the script so that it prints the greeting message Hello, Alice! to the terminal.
Bash Scripting
Need a hint?

Make sure you run the script and see the output Hello, Alice! in the terminal.