0
0
Bash Scriptingscripting~10 mins

Return values (return and echo) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Return values (return and echo)
Start function
Execute commands
Use echo to output value
Use return to set exit status
Function ends
Caller captures echo output or return status
Use output or status in script
A bash function uses echo to send output text and return to set an exit status code. The caller can capture either.
Execution Sample
Bash Scripting
myfunc() {
  echo "Hello"
  return 3
}
myfunc
status=$?
output=$(myfunc)
Defines a function that echoes text and returns a status; then calls it capturing both.
Execution Table
StepActionEcho OutputReturn StatusNotes
1Call myfuncFunction starts
2Execute echo "Hello"HelloOutput sent to stdout
3Execute return 33Function exits with status 3
4Capture $? after myfunc3Return status stored in status
5Capture output=$(myfunc)Hello0Output captured, return status of subshell is 0
6Check status variable3Status from first call remains 3
💡 Function ends after return; caller captures output and status separately
Variable Tracker
VariableStartAfter Call 1After Call 2Final
statusunset333
outputunsetunsetHelloHello
Key Moments - 2 Insights
Why does the return status after capturing output with $(myfunc) show 0 instead of 3?
Because $(myfunc) runs the function in a subshell, the return status of the subshell is 0 if the command succeeds, not the function's return value. See execution_table row 5.
Can echo send a return status code?
No, echo only sends text output. The return command sets the exit status code. See execution_table rows 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'status' after the first call to myfunc?
A3
B0
CHello
Dunset
💡 Hint
Check execution_table row 4 where status is set after myfunc returns
At which step does the function output 'Hello' to stdout?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 2 for echo output
If we remove the return statement, what will be the return status after calling myfunc?
A3
B1
C0
DUndefined
💡 Hint
By default, bash functions return 0 if no explicit return is given
Concept Snapshot
Bash functions use echo to send output text.
Use return to set an exit status (0-255).
Caller captures output with $(func) and status with $?.
Echo output and return status are separate.
Return ends function immediately.
Subshells affect return status capture.
Full Transcript
In bash scripting, functions can send output using echo and set an exit status using return. When you call a function, echo sends text to standard output, which you can capture using command substitution like output=$(myfunc). The return command sets a numeric exit status accessible via $?. Note that capturing output with $(myfunc) runs the function in a subshell, so the return status of the subshell is usually 0 if the command succeeds, not the function's return value. To get the function's return status, call it normally and check $?. Echo and return serve different purposes: echo for output, return for status. The function ends immediately after return. Understanding these helps you handle function outputs and statuses correctly in bash scripts.