Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    bash

    Find the exit status of a previous command in Bash

    RyanBy RyanJanuary 14, 20111 Min Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    Most programs will return an exit status of 0 if the program was successful, while a non-zero exit status usually indicates an error. You can find the exit status or exit code of the previously executed command by accessing the “$?” shell variable.

    $ COMMAND
    $ echo $?

    Typically when a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status. If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.

    You can also use the exit status in a conditional.

    #!/bin/bash
    COMMAND
    STATUS=$?
    if [ $STATUS -eq 0 ]; then
    echo "Command Successful"
    else
    echo "Command Failed"
    fi

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleFind the parent PID of a Bash Script
    Next Article Enable or disable boot time services in SUSE

    Related Posts

    How to test the syntax of a Python script

    Safely and atomically change a symlink target

    How to create and debug a core dump file

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.