• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

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.
2 Solutions
("$@" ; echo $? > status ) > /dev/null &
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 $?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now