Skip to content
Advertisement

Get return value of command run in background with pipe and tee command

I want to get the return value of command run in background with pipe so i have below sample code.

JavaScript

Output:

JavaScript

If i echo the return value from same command then it print correct return value.

But if store it in RETVALUE variable then it shows wrong value.

Advertisement

Answer

The problem is due to the & sign. This puts the RETVALUE assignment into the background, thus this command executes in a different environment than the current script, so the variables in your script do no get updated.

You also don’t need to export the RETVALUE. Also the wait command is not necessary, as bash does not process the next command until it has finished the previous one (unless you use the & to put it in the background)

JavaScript

EDIT: If you need to launch the process in the background, you will be forced to create a new script in order to recover the PIPESTATUS value, due to this variable is volatile. A possible solution is:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement