0
0
Bash Scriptingscripting~15 mins

Why functions organize reusable code in Bash Scripting - See It in Action

Choose your learning style9 modes available
Why functions organize reusable code
📖 Scenario: You are working on a small script to greet users by name. Instead of writing the greeting code multiple times, you want to organize it using a function. This makes your script cleaner and lets you reuse the greeting easily.
🎯 Goal: Build a bash script that defines a function to greet a user by name, then calls this function with different names.
📋 What You'll Learn
Create a function named greet that takes one argument (a name).
Inside the function, print the message: Hello, <name>! where <name> is the argument.
Call the greet function twice with the names Alice and Bob.
Print the greetings exactly as specified.
💡 Why This Matters
🌍 Real World
Functions help automate repetitive tasks in scripts, like sending emails or processing files multiple times with different data.
💼 Career
Knowing how to write and use functions in scripts is essential for system administrators, DevOps engineers, and anyone automating workflows.
Progress0 / 4 steps
1
Create a function named greet
Write a bash function named greet that takes one argument called name. Inside the function, print the text Hello, $name! using echo.
Bash Scripting
Need a hint?

Use function_name() { ... } syntax. Use $1 to get the first argument inside the function.

2
Call the greet function with Alice
Call the greet function with the argument Alice to print a greeting for Alice.
Bash Scripting
Need a hint?

Call the function by writing its name followed by the argument: greet Alice.

3
Call the greet function with Bob
Add a second call to the greet function with the argument Bob to print a greeting for Bob.
Bash Scripting
Need a hint?

Call the function again with Bob as the argument.

4
Print the greetings
Run the script so it prints the greetings for Alice and Bob exactly as Hello, Alice! and Hello, Bob!.
Bash Scripting
Need a hint?

Just run the script. The function calls will print the greetings.