Monday, January 20, 2025
HomeQ&AHow can I check the exit status using an 'if' statement in...

How can I check the exit status using an ‘if’ statement in a shell script?

How can I check the exit status using an ‘ if’ statement in a shell script?

In shell scripts, the exit status of a last executed command is stored in the special variable $?. The exit status is 0 for success and on-zero for failure. You can check the exit status in an if statement like so:

See also  What do you think really happens after we die?

if [ $? -eq 0 ]; then echo “Success”

else echo “Failure”

fi: Checks if the previous command succeeded or failed.

To check the exit status using an ‘if’ statement:

Directly check the exit status of a command using this method :

See also  What does “ty” mean in texting?

`$?` variable:

If [ $? -eq 0 ]; then

echo “Success”

else

echo “Failure”

 

 

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x