How to return a values from a method which prints its out put [closed]

I have a method which prints its content with ‘printf’. I want the printed contents to be return.

parse_yml() {
   #some logics here
         printf("%s%s is %s", $1, code, $2, $3);
      }

In above code, “printf("%s%s is %s", $1, code, $2, $3);” prints the desired values. But I need to return all printed values concatenated so that the a variable which is outside the method can have final all values into a single variable. Something like below:

allContent=parse_yml. 

I tried with defining a global variable called r, and instead of printf line:

r=$r$vn$2$3 

and outside, when echoed, it was empty.

Any suggestion please how to return all values from that method ?

Answer

I don’t know, whether I understood you right, but in bash, you grab output like this:

allContent=$(parse_yml.) 

An

echo $allContent

should then reveal the same, like

parse_yml. > parse_yml.out
cat parse_yml.out

The filename parse_yml. looks a bit suspicious – no file ending? And usually, you have to either specify the absolute path, or the relative path, which would be

./parse_yml

except, if the current directory is in the PATH.

Leave a Reply

Your email address will not be published. Required fields are marked *