Complete the code to return the value 5 from the function.
my_func() {
[1] 5
}
my_func
The return command in bash functions sets the exit status (an integer between 0 and 255). Here, return 5 returns the value 5 as the function's exit status.
Complete the code to print the string 'Hello' from the function.
greet() {
[1] "Hello"
}
greet
The echo command prints text to the terminal. Here, echo "Hello" prints the string 'Hello' when the function is called.
Fix the error in the function to correctly return the exit status 3.
check_status() {
[1] 3
}
check_status
status=$?
echo $status
To set the function's exit status, use return. Using echo prints the number but does not set the exit status. The variable $? captures the exit status.
Fill both blanks to create a function that prints 'Done' and returns exit status 0.
finish() {
[1] "Done"
[2] 0
}
finish
status=$?
echo $status
The function uses echo to print 'Done' and return 0 to set the exit status to 0, indicating success.
Fill all three blanks to create a function that prints the argument, returns 1 if argument is 'fail', else returns 0.
result() {
[1] "$1"
if [[ "$1" == "fail" ]]; then
[2] 1
else
[3] 0
fi
}
result "$1"
exit_status=$?
echo $exit_status
The function prints the argument with echo. It uses return 1 if the argument is 'fail', otherwise return 0. This sets the function's exit status correctly.