📖 Scenario: You are writing a small Bash script to calculate the sum of two numbers. You want to learn how to get values back from functions using return and echo.
🎯 Goal: Build a Bash script with a function that adds two numbers and returns the result using both return and echo. Then, display the results.
📋 What You'll Learn
Create a function called add_numbers that takes two arguments
Use return to send back the sum modulo 256
Use echo to print the full sum
Call the function and capture both the return value and the echoed output
Print both captured values clearly
💡 Why This Matters
🌍 Real World
In real scripts, functions often need to send back status codes and data. Understanding how <code>return</code> and <code>echo</code> work helps you write better Bash scripts.
💼 Career
Many automation and DevOps tasks use Bash scripts. Knowing how to get values from functions is essential for debugging and building reliable scripts.
Progress0 / 4 steps
1
Create the add_numbers function
Write a Bash function called add_numbers that takes two arguments named num1 and num2. Inside the function, calculate the sum of num1 and num2 and store it in a variable called sum.
Bash Scripting
Hint
Use $1 and $2 to get the function arguments.
2
Add return and echo to the function
Inside the add_numbers function, add a return statement that returns sum % 256. Then, use echo to print the full sum.
Bash Scripting
Hint
Remember return can only return numbers 0-255. Use modulo % 256.
3
Call the function and capture the return value
Call the add_numbers function with arguments 100 and 200. Capture the return value in a variable called ret_val using $? immediately after the call.
Bash Scripting
Hint
Use $? right after the function call to get the return value.
4
Capture the echoed output and print both results
Call add_numbers with 100 and 200 again, but this time capture the echoed output in a variable called echoed_sum using command substitution. Then, print both ret_val and echoed_sum with clear messages.
Bash Scripting
Hint
Use $( ) to capture the echoed output. Use echo to print messages.
Practice
(1/5)
1. In bash scripting, what does the return command inside a function do?
easy
A. It stops the entire script immediately.
B. It sends a numeric status code back to the caller.
C. It prints text output to the terminal.
D. It saves the function output to a file.
Solution
Step 1: Understand the purpose of return in bash functions
The return command sends a numeric exit status (0-255) back to the caller, indicating success or failure.
Step 2: Differentiate from echo
Unlike echo, which prints text output, return only sends a number status, not text.
Final Answer:
It sends a numeric status code back to the caller. -> Option B
Quick Check:
return = numeric status [OK]
Hint: Remember: return sends number status, echo prints text [OK]
Common Mistakes:
Thinking return prints output like echo
Using return to output strings
Confusing return with exit
2. Which of the following is the correct syntax to return a status code 5 from a bash function named check?
easy
A. exit 5
B. echo 5
C. return(status=5)
D. return 5
Solution
Step 1: Recall bash function return syntax
To return a status code from a function, use return followed by the number, e.g., return 5.
Step 2: Identify incorrect options
echo 5 prints text, return(status=5) is invalid syntax, and exit 5 exits the whole script.
Final Answer:
return 5 -> Option D
Quick Check:
Use return number syntax [OK]
Hint: Use 'return' followed by number to send status [OK]
Common Mistakes:
Using echo instead of return for status
Trying to use parentheses in return
Using exit inside function to return status
3. What will be the output of this script?
myfunc() {
echo "Hello"
return 3
}
myfunc
echo $?
medium
A. 3\nHello
B. Hello\n0
C. Hello\n3
D. 3\n3
Solution
Step 1: Analyze function output
The function prints "Hello" using echo, so "Hello" appears first.
Step 2: Check return status and echo it
The function returns 3, so $? after calling it is 3, which is printed next.
Final Answer:
Hello\n3 -> Option C
Quick Check:
echo prints text, return sets status [OK]
Hint: echo prints text first, $? shows return code after function [OK]
Common Mistakes:
Confusing echo output with return value
Expecting return value to print automatically
Ignoring that $? holds last return status
4. Identify the error in this bash function:
myfunc() {
return "Success"
}
myfunc
echo $?
medium
A. return expects a number, not a string
B. echo cannot print return values
C. Function missing parentheses
D. return must be last command in function
Solution
Step 1: Understand return command rules
In bash, return must be followed by a numeric status (0-255), not a string.
Step 2: Identify the error in the code
The code tries to return "Success" (a string), which causes an error.
Final Answer:
return expects a number, not a string -> Option A
Quick Check:
return only accepts numbers [OK]
Hint: return only accepts numbers, not text [OK]
Common Mistakes:
Trying to return strings with return
Confusing echo and return usage
Assuming return prints output
5. How can you capture the text output of a bash function greet that uses echo inside, and store it in a variable msg?
hard
A. msg=$(greet)
B. msg=return greet
C. msg=`return greet`
D. msg=echo greet
Solution
Step 1: Understand capturing function output
To capture text output from a function that uses echo, use command substitution: $(function_name).
Step 2: Identify correct syntax
msg=$(greet) runs greet and stores its echo output in msg. Other options misuse return or echo.
Final Answer:
msg=$(greet) -> Option A
Quick Check:
Use $(function) to capture echo output [OK]
Hint: Use $(function) to capture echo output in variable [OK]
Common Mistakes:
Using return instead of command substitution
Assigning echo command directly to variable
Using backticks incorrectly with return
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.