0
0
Bash Scriptingscripting~5 mins

Function definition in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Adef say_hello():
Bfunction say_hello[] {
Cfunction say_hello()
Dsay_hello() {
Inside a bash function, how do you access the first argument passed to it?
A$0
B$first
C$1
D$arg1
What does the return statement do inside a bash function?
APrints a value to the screen
BEnds the function and sets an exit status
CCalls another function
DDefines a new variable
Which of these is a correct way to call a bash function named greet?
Agreet
Bgreet()
Ccall greet
Drun greet
What symbol is used to start the body of a bash function?
A{
B[
C(
D<
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.