Languages
[Edit]
EN

Bash - return string from function

5 points
Created by:
Khloe-Kaiser
578

In this short article, we would like to show how to return string / text from a function in Bash.

Quick solution:

  • use echo "$my_variable" to print a string inside a function (return works only with numbers),
  • use my_variable="$(my_function)" to call the function, and retrieve the string.

 

Practical example:

 

Alternative solution

Edit

You can use the global variables approach, which requires keeping some order in the source code.

or:

Alternative titles

  1. Bash - return text from function
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join