• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Find exit status code of last executed command on Linux and Unix

April 3, 2014 by Sharad Chhetri 2 Comments

When we execute any command or script,it returns an exit status. The exit status is also called as return code or exit code. It is important to know the exit status of command which we execute.
The method which we are going to describe become more helpful with script also.

Exit status must be an integer in the 0 – 255 range.

The exit status with zero value considered as command or script has run execution.Whereas, the exit status with non-zero value meaning is the command or script is not run successfully.

Exit status code of last executed command

To get the exit status code,run the below given command after running the script/command.

echo $?

See below given Screenshot
In this example I tried to show, how we can use echo $? to check the exit code.

When I used the command ls -lhrt and after this I use echo $? which show the output as 0 (i.e Zero). It means the last command (ls -lhrt) was executed successfully.
Whereas on using wrong command,exit code is non-zero (i.e 127 as shown on terminal)

exit code status

Bash script example with exit code

The below given bash script is created, to check the exit status of command.The script is a reference and can be used in other bash script as per the requirement.

#!/bin/bash
##
## This script is a sample for using "exit $?" command within script
##
echo "Type any command and hit ENTER"
read val
$val > /dev/null 2>&1
### putting 4 seconds delay by using sleep command
if [ $? != 0 ]
then
echo "**************************************************************"
echo "The command IS NOT EXECUTED SUCCESSFULLY"
else
echo "*************************************************************"
echo 'The command is SUCCESSFULLY executed'
fi 

See below given animated example

exit code linux unix

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. error command ‘gcc’ failed with exit status 1 in Ubuntu
  2. How To Find Absolute Full Path Of Command On Linux / Unix : which command
  3. curl command to check the http status
  4. 6 important examples of cd command on Linux and Unix Systems
  5. Linux / Unix : whoami command use and advantage
  6. Head command in Linux / Unix
  7. What is Linux Internal And External Command
  8. Virtual Machine inaccessible status on VirtualBox 4.3
  9. Useful tail command with examples in Linux/Unix
  10. How to know last command run successfully : Linux / Unix

Filed Under: Linux, Linux Commands Tagged With: linux command

Reader Interactions

Comments

  1. Dingo says

    February 12, 2015 at 12:31 pm

    The first command is wrong because it shows “exit $?” but should be “echo $?”.

    Reply
    • sharad chhetri says

      February 13, 2015 at 2:40 am

      Thanks Dingo,
      You are correct. That is typo.

      Regards
      sharad

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

Allow wget and yum in iptable

rsync all files,hidden files,symlinks,hardlinks to remotes Linux Server

How to configure htdigest in nagios server

How to encode and decode the strings with base64

How to increase Password Expire date without resetting the password

error command ‘gcc’ failed with exit status 1 in Ubuntu

Edit the crontab without using crontab -e

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy