Bird
Raised Fist0
Bash Scriptingscripting~5 mins

Return values (return and echo) in Bash Scripting

Choose your learning style10 modes available
Introduction
In bash scripts, you use return and echo to send back results from functions. This helps you reuse code and get information from functions.
When you want a function to tell if it succeeded or failed using return codes.
When you want a function to send back some text or data using echo.
When you need to capture the output of a function to use later in your script.
When you want to check the status of a command inside a function.
When you want to print messages from a function to the screen.
Syntax
Bash Scripting
function_name() {
  # Use return to send a number (0-255) as status
  return <number>

  # Use echo to print text output
  echo "some text"
}
The return command only sends back a number between 0 and 255, usually 0 means success and other numbers mean errors.
Echo prints text to the screen or to wherever the output is directed. You can capture this output in a variable.
Examples
This function returns 0 if the number is even, 1 if odd.
Bash Scripting
check_even() {
  if (( $1 % 2 == 0 )); then
    return 0
  else
    return 1
  fi
}
This function prints a greeting message using echo.
Bash Scripting
greet() {
  echo "Hello, $1!"
}
Captures the output of greet function into a variable and prints it.
Bash Scripting
result=$(greet Alice)
echo "$result"
Sample Program
This script defines two functions: one returns a status code to say if a number is even, the other prints a greeting. It then uses them and shows the results.
Bash Scripting
#!/bin/bash

is_even() {
  if (( $1 % 2 == 0 )); then
    return 0
  else
    return 1
  fi
}

say_hello() {
  echo "Hello, $1!"
}

number=4
say_hello "User"

if is_even $number; then
  echo "$number is even."
else
  echo "$number is odd."
fi
OutputSuccess
Important Notes
Return values in bash functions are limited to numbers 0-255 and usually indicate success (0) or failure (non-zero).
Use echo to send text output from functions, which you can capture with command substitution $(...).
Do not confuse return and echo: return is for status codes, echo is for output data.
Summary
Use return to send back a number status from a function.
Use echo to print text output from a function.
Capture echo output with $(...) to use it in your script.

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

  1. 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.
  2. Step 2: Differentiate from echo

    Unlike echo, which prints text output, return only sends a number status, not text.
  3. Final Answer:

    It sends a numeric status code back to the caller. -> Option B
  4. 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

  1. 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.
  2. Step 2: Identify incorrect options

    echo 5 prints text, return(status=5) is invalid syntax, and exit 5 exits the whole script.
  3. Final Answer:

    return 5 -> Option D
  4. 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

  1. Step 1: Analyze function output

    The function prints "Hello" using echo, so "Hello" appears first.
  2. Step 2: Check return status and echo it

    The function returns 3, so $? after calling it is 3, which is printed next.
  3. Final Answer:

    Hello\n3 -> Option C
  4. 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

  1. Step 1: Understand return command rules

    In bash, return must be followed by a numeric status (0-255), not a string.
  2. Step 2: Identify the error in the code

    The code tries to return "Success" (a string), which causes an error.
  3. Final Answer:

    return expects a number, not a string -> Option A
  4. 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

  1. Step 1: Understand capturing function output

    To capture text output from a function that uses echo, use command substitution: $(function_name).
  2. Step 2: Identify correct syntax

    msg=$(greet) runs greet and stores its echo output in msg. Other options misuse return or echo.
  3. Final Answer:

    msg=$(greet) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard