bash how to return string from function

Answer

You can return string from function in many ways, but you can not use command "return" to return string:

return "Hello..."

Return statement can return only a integer value.

First option uses passing argument to the function. To assign to the first argument use in function "$1":

eval "$1='Hello...'"

Then call the function "my_function":

my_var=''
my_function my_var
echo $my_var

Output: Hello...

Other way is use global variable which you modify within function.

You can also use command echo to write string value and use command substitution to get it:

hello() {
var='Hello friend.'
echo "$var"
}
	
greeting=$(hello)
echo $greeting

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.