Monday 22 August 2016

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)

Answer



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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...