Linux scripting question, return code of last command

Posted on 2011-10-28
Last Modified: 2013-12-10
How do I determine the return code of the last command

I want to know if it was successful or not
Question by:Los Angeles1
    LVL 68

    Accepted Solution

    The variable $? Contains the returncode of the last command.

    LVL 40

    Expert Comment

    just run the below command

    echo $?

    if result is 0 (zero) then last command was successful

    LVL 68

    Expert Comment

    You could do

    if [ $? -eq 0 ]; then
       echo "Command succeeded!"
        # Some more action to celebrate this success ...
          echo "Command failed"
          # More action to bemoan this failure ...

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
    There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now