How to return a string value from a Bash function

Tomas F picture Tomas F · Jul 13, 2010 · Viewed 336.3k times · Source

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)

Answer

Philipp picture Philipp · Jul 13, 2010

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