How to return a string value from a Bash function How to return a string value from a Bash function bash bash

How to return a string value from a Bash function


There is no better way I know of. Bash knows only status codes (integers) and strings written to the stdout.


You could have the function take a variable as the first arg and modify the variable with the string you want to return.

#!/bin/bashset -xfunction pass_back_a_string() {    eval "$1='foo bar rab oof'"}return_var=''pass_back_a_string return_varecho $return_var

Prints "foo bar rab oof".

Edit: added quoting in the appropriate place to allow whitespace in string to address @Luca Borrione's comment.

Edit: As a demonstration, see the following program. This is a general-purpose solution: it even allows you to receive a string into a local variable.

#!/bin/bashset -xfunction pass_back_a_string() {    eval "$1='foo bar rab oof'"}return_var=''pass_back_a_string return_varecho $return_varfunction call_a_string_func() {     local lvar=''     pass_back_a_string lvar     echo "lvar='$lvar' locally"}call_a_string_funcecho "lvar='$lvar' globally"

This prints:

+ return_var=+ pass_back_a_string return_var+ eval 'return_var='\''foo bar rab oof'\'''++ return_var='foo bar rab oof'+ echo foo bar rab ooffoo bar rab oof+ call_a_string_func+ local lvar=+ pass_back_a_string lvar+ eval 'lvar='\''foo bar rab oof'\'''++ lvar='foo bar rab oof'+ echo 'lvar='\''foo bar rab oof'\'' locally'lvar='foo bar rab oof' locally+ echo 'lvar='\'''\'' globally'lvar='' globally

Edit: demonstrating that the original variable's value is available in the function, as was incorrectly criticized by @Xichen Li in a comment.

#!/bin/bashset -xfunction pass_back_a_string() {    eval "echo in pass_back_a_string, original $1 is \$$1"    eval "$1='foo bar rab oof'"}return_var='original return_var'pass_back_a_string return_varecho $return_varfunction call_a_string_func() {     local lvar='original lvar'     pass_back_a_string lvar     echo "lvar='$lvar' locally"}call_a_string_funcecho "lvar='$lvar' globally"

This gives output:

+ return_var='original return_var'+ pass_back_a_string return_var+ eval 'echo in pass_back_a_string, original return_var is $return_var'++ echo in pass_back_a_string, original return_var is original return_varin pass_back_a_string, original return_var is original return_var+ eval 'return_var='\''foo bar rab oof'\'''++ return_var='foo bar rab oof'+ echo foo bar rab ooffoo bar rab oof+ call_a_string_func+ local 'lvar=original lvar'+ pass_back_a_string lvar+ eval 'echo in pass_back_a_string, original lvar is $lvar'++ echo in pass_back_a_string, original lvar is original lvarin pass_back_a_string, original lvar is original lvar+ eval 'lvar='\''foo bar rab oof'\'''++ lvar='foo bar rab oof'+ echo 'lvar='\''foo bar rab oof'\'' locally'lvar='foo bar rab oof' locally+ echo 'lvar='\'''\'' globally'lvar='' globally


All answers above ignore what has been stated in the man page of bash.

  • All variables declared inside a function will be shared with the calling environment.
  • All variables declared local will not be shared.

Example code

#!/bin/bashf(){    echo function starts    local WillNotExists="It still does!"    DoesNotExists="It still does!"    echo function ends}echo $DoesNotExists #Should print empty lineecho $WillNotExists #Should print empty linef                   #Call the functionecho $DoesNotExists #Should print It still does!echo $WillNotExists #Should print empty line

And output

$ sh -x ./x.sh+ echo+ echo+ f+ echo function starts function starts+ local 'WillNotExists=It still does!'+ DoesNotExists='It still does!'+ echo function ends function ends+ echo It still 'does!' It still does!+ echo

Also under pdksh and ksh this script does the same!