0
0
Bash Scriptingscripting~15 mins

Function arguments ($1, $2 inside function) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Function Arguments ($1, $2 inside function) in Bash
📖 Scenario: You are writing a small bash script to greet users by their first and last names. You want to practice how to pass arguments to a function and use $1 and $2 inside the function to access those arguments.
🎯 Goal: Build a bash script with a function called greet that takes two arguments: first name and last name. The function should print a greeting message using these arguments.
📋 What You'll Learn
Create a function named greet that accepts two arguments.
Use $1 and $2 inside the function to access the first and second arguments.
Call the function with the exact arguments John and Doe.
Print the greeting message: Hello, John Doe!
💡 Why This Matters
🌍 Real World
Passing arguments to functions is common in bash scripts to reuse code and handle dynamic input, such as user names or file names.
💼 Career
Understanding function arguments in bash is essential for automation tasks, system administration, and writing maintainable shell scripts.
Progress0 / 4 steps
1
Create the greet function
Write a bash function named greet that takes two arguments. Inside the function, do not write anything else yet.
Bash Scripting
Need a hint?

Use the syntax function greet() { } to define the function.

2
Add variables to access arguments inside greet
Inside the greet function, use $1 and $2 to refer to the first and second arguments. Assign them to variables named first_name and last_name respectively.
Bash Scripting
Need a hint?

Use first_name=$1 and last_name=$2 inside the function.

3
Add a greeting message inside the function
Inside the greet function, add a echo statement that prints: Hello, $first_name $last_name!
Bash Scripting
Need a hint?

Use echo "Hello, $first_name $last_name!" to print the greeting.

4
Call the greet function with arguments
Call the greet function with the arguments John and Doe. Then run the script to print the greeting.
Bash Scripting
Need a hint?

Call the function by writing greet John Doe after the function definition.