How to capture a return code from calling a command in shell script


I have written a shell script to execute a command as

"$@" > /dev/null &

Now, this command will run in the background. It will run for a while before it will terminate.
This shell script will continue checking the progress and will terminate the process if it is need to.

Now the question is... if the command execute successfully, how do I check the return code from the command? What I need to do to get this information inside this shell script?

Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

("$@" ; echo $? > status ) > /dev/null &

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If you run a command / script in the background, then you can get it's process id from


To get the exit status, you can do

wait $!
echo $?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.