InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Telling If a Command Succeeded or Not in bash
Bash

Telling If a Command Succeeded or Not in bash

InfinityCoder February 9, 2017

You need to know whether the command you ran succeeded.

The shell variable $? will be set with a non-zero value if the command fails—provided that the programmer who wrote that command or shell script followed the established convention:

1
2
3
4
5
6
7
8
9
$ somecommand
it works...
$ echo $?
0
$ badcommand
it fails...
$ echo $?
1
$

The exit status of a command is kept in the shell variable referenced with $?. Its value can range from 0 to 255.

When you write a shell script, it’s a good idea to have your script exit with a non-zero value if you encounter an error condition. (Just keep it below 255, or the numbers will wrap around.)

You return an exit status with the exit statement (e.g., exit 1 or exit 0).

But be aware that you only get one shot at reading the exit status:

1
2
3
4
5
6
7
$ badcommand
it fails...
$ echo $?
1
$ echo $?
0
$

Why does the second time give us 0 as a result? Because the second time is reporting on the status of the immediately preceding echo command.

The first time we typed echo $? it returned a 1, which was the return value of badcommand.

But the echo command itself succeeds, therefore the new, most-recent status is success (i.e., a 0 value).

So you only get one chance to check it. Therefore, many shell scripts will immediately assign the status to another shell variable, as in:

1
2
3
4
5
6
7
8
$ badcommand
it fails...
$ STAT=$?
$ echo $STAT
1
$ echo $STAT
1
$

We can keep the value around in the variable $STAT and check its value later on.

Although we’re showing this in command-line examples, the real use of variables like $? comes in writing scripts.

You can usually see if a command worked or not if you are watching it run on your screen.

But in a script, the commands may be running unattended.
One of the great features of bash is that the scripting language is identical to commands as you type them at a prompt in a terminal window.

This makes it much easier to check out syntax and logic as you write your scripts.
The exit status is more often used in scripts, and often in if statements, to take different actions depending on the success or failure of a command.

Here’s a simple example for now, but we will revisit this topic in future recipes:

1
2
3
$ somecommand
...
$ if (( $? )) ; then echo failed ; else echo OK; fi

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding a File Using a List of Possible Locations in bash
You need to execute, source, or read a file, but …

Finding a File Using a List of Possible Locations in bash

Getting Input from a File in bash
You want your shell commands to read data from a …

Getting Input from a File in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap