Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a string value from a Bash function

I'd like to return a string from a Bash function.

I'll write the example in java to show what I'd like to do:

public String getSomeString() {   return "tadaa"; }  String variable = getSomeString(); 

The example below works in bash, but is there a better way to do this?

function getSomeString {    echo "tadaa" }  VARIABLE=$(getSomeString) 
like image 387
Tomas F Avatar asked Jul 13 '10 11:07

Tomas F


People also ask

Can a bash function return a value?

A bash function can return a value via its exit status after execution. By default, a function returns the exit code from the last executed command inside the function. It will stop the function execution once it is called.

How do I return a value in bash?

When a bash function completes, its return value is the status of the last statement executed in the function, 0 for success and non-zero decimal number in the 1 - 255 range for failure. The return status can be specified by using the return keyword, and it is assigned to the variable $? .

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

Does echo return a string?

The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.


2 Answers

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

#!/bin/bash set -x function pass_back_a_string() {     eval "$1='foo bar rab oof'" }  return_var='' pass_back_a_string return_var echo $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/bash set -x function pass_back_a_string() {     eval "$1='foo bar rab oof'" }  return_var='' pass_back_a_string return_var echo $return_var  function call_a_string_func() {      local lvar=''      pass_back_a_string lvar      echo "lvar='$lvar' locally" }  call_a_string_func echo "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 oof foo 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/bash set -x function 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_var echo $return_var  function call_a_string_func() {      local lvar='original lvar'      pass_back_a_string lvar      echo "lvar='$lvar' locally" }  call_a_string_func echo "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_var in 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 oof foo 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 lvar in 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 
like image 34
bstpierre Avatar answered Oct 16 '22 10:10

bstpierre


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

like image 95
Philipp Avatar answered Oct 16 '22 08:10

Philipp