Recall & Review
beginner
What is a function in bash scripting?
A function is a named block of code that performs a specific task and can be reused multiple times in a script.
Click to reveal answer
beginner
How do you define a function named
greet in bash?You can define it like this:<br>
greet() {
echo "Hello!"
}Click to reveal answer
beginner
How do you call a function named
greet in bash?Just write the function name without parentheses:<br>
greet
Click to reveal answer
intermediate
Can bash functions accept arguments? How?
Yes, bash functions accept arguments which are accessed inside the function as
$1, $2, etc., representing the first, second argument, and so on.Click to reveal answer
intermediate
What is the purpose of the
return statement in a bash function?The
return statement ends the function and optionally sends an exit status (a number between 0 and 255) back to the caller.Click to reveal answer
How do you start defining a function named
say_hello in bash?✗ Incorrect
In bash, functions are defined with the syntax: function_name() { ... }
Inside a bash function, how do you access the first argument passed to it?
✗ Incorrect
Arguments inside bash functions are accessed as $1, $2, etc. $0 is the script name.
What does the
return statement do inside a bash function?✗ Incorrect
return ends the function and optionally sends an exit status back.Which of these is a correct way to call a bash function named
greet?✗ Incorrect
You call a bash function simply by writing its name: greet
What symbol is used to start the body of a bash function?
✗ Incorrect
The function body starts with a curly brace { and ends with }
Explain how to define and call a function in bash, including how to pass and use arguments.
Think about how you tell a friend to do a task and give them some details.
You got /3 concepts.
Describe the role of the return statement in a bash function and how it differs from outputting text.
Return is like finishing a task and giving a status, echo is like saying something out loud.
You got /3 concepts.