Reputation: 2949
I have read qns regarding return output from a function in Stack Overflow. All the post says to use echo
#!/bin/bash
function myown()
{
echo "i dont need this in retval"
echo "Need this alone in retVal"
}
retVal=$(myown)
echo $retVal
o/p: i dont need this in retval Need this alone in retVal
expected: Need this alone in retVal
Is there a way to flush the previous output in echo. Or I need to parse all the echoed output to get my return value ? Is there simple way to do this ? Because I may have echos that are useful to debug and echo to return a value.
Upvotes: 1
Views: 4022
Reputation: 64308
Echo output to stderr for debugging:
#!/bin/bash
function myown()
{
echo "i dont need this in retval" >&2
echo "Need this alone in retVal"
}
retVal=$(myown)
echo "result: $retVal"
When you run the script, you will see
i dont need this in retval result: Need this alone in retVal
Upvotes: 5