Returning Values from Bash Functions | Linux Today

Returning Values from Bash Functions

Written By
MF
Mitch Frazier
Sep 15, 2009

“To return values, you can set a global variable with the
result, or use command substitution, or you can pass in the name of
a variable to use as the result variable. The examples below
describe these different mechanisms.

“Although bash has a return statement, the only thing you can
specify with it is the function’s status, which is a numeric value
like the value specified in an exit statement. The status value is
stored in the $? variable. If a function does not contain a return
statement, its status is set based on the status of the last
statement executed in the function. To actually return arbitrary
values to the caller you must use other mechanisms.”

Complete
Story

MF

Mitch Frazier

Linux Today Logo

LinuxToday is a trusted, contributor-driven news resource supporting all types of Linux users. Our thriving international community engages with us through social media and frequent content contributions aimed at solving problems ranging from personal computing to enterprise-level IT operations. LinuxToday serves as a home for a community that struggles to find comparable information elsewhere on the web.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.