0
0
Bash Scriptingscripting~5 mins

Return values (return and echo) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the return command do inside a bash function?
It stops the function and sends a numeric exit status (0-255) back to the caller. It does NOT send text output.
Click to reveal answer
beginner
How can you send text output from a bash function?
Use echo inside the function to print text. The caller can capture this output using command substitution.
Click to reveal answer
beginner
What is the difference between return and echo in bash functions?
return sends a numeric status code to indicate success or failure.<br>echo sends text output that can be captured or displayed.
Click to reveal answer
beginner
Can return send strings or complex data in bash?
No. return only sends a number (0-255). To send strings, use echo and capture the output.
Click to reveal answer
beginner
How do you capture the output of a bash function that uses echo?
Use command substitution like result=$(my_function). The variable result will hold the echoed text.
Click to reveal answer
What does return 1 inside a bash function mean?
AThe function prints '1' to the screen
BThe function ends and sends a failure status 1
CThe function sends the string '1' as output
DThe function continues running
How do you get text output from a bash function?
AUse <code>echo</code> inside the function
BUse <code>print</code> command
CUse <code>return</code> with the text
DUse <code>exit</code> with text
What is the valid range of numbers you can use with return in bash?
AAny integer
BOnly 0 or 1
C1 to 100
D0 to 255
How do you store the text output of a bash function into a variable?
Avar=$(my_function)
Bvar=return my_function
Cvar=echo my_function
Dvar=my_function
If a bash function uses return 5, what will $? be after calling it?
AThe text '5'
B0
C5
D1
Explain how return and echo differ in bash functions and when to use each.
Think about what you want to send back: a number or text.
You got /4 concepts.
    Describe how to capture the output of a bash function that prints text using echo.
    Remember how you capture command output in bash.
    You got /3 concepts.