Understanding function input and output

It's important to note from the discussion on return values that you cannot return strings or arrays from functions. The return keyword is not used in the same way as in structured programming languages, such as PHP or JavaScript. It is strictly for integers, and it's intended to describe the function's success or failure, and not any product of its work.

Instead, for values calculated by a function, you should have the function print the output that you want, and use it in suitable ways in scripts. Using our home example function, the following code uses command substitution to include the function's output in a message of its own:

bash$ home() { printf '%s\n' "$HOME" ; } bash$ printf '%s\n' "Your ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.