Force "bad" (not 0) return code of terminal command?

12,347

Solution 1

If you're looking for a system command that always returns a non-zero exit code, then /bin/false seems like it should work for you. From man false:

NAME
       false - do nothing, unsuccessfully

SYNOPSIS
       false [ignored command line arguments]
       false OPTION

DESCRIPTION
       Exit with a status code indicating failure.

Solution 2

You can create a new return code with the command bash -c "exit RETURNCODE", replacing "RETURNCODE" with any number. Note that it will be trimmed to an 8bit unsigned integer (0...255) by (RETURNCODE mod 256)

You can check the return code of the last shell command inside the terminal(!) with executing echo $?. The "$?" variable contains the most recent return code and "echo" prints it to the standard output.

Share:
12,347

Related videos on Youtube

GhostCat
Author by

GhostCat

First of all: things are getting better, but let us all never forget this! I am mainly a contributor to stackoverflow.com. Beyond that, I participate on MSO, MSE, and various other SE communities, mostly around software/IT topics. My primary goal on stackoverflow.com is to help other folks solving their problems. Sometimes I am very direct; so please understand that I am not rude on intention - just honest. If you disagree with something I say: simply speak up; I appreciate any (constructive) feedback! On stackoverflow.com, I proudly hold a few gold badges, like: java tag: Awarded Sep 23 '16 Illuminator: Awarded Jul 8 '17 Legendary: Award Sept 7 '17 Personal background I studied computer science in the late 90ies at the "Universität of Karlsruhe"; nowadays known as KIT. After some years with smaller companies, I am working as software engineer at a global IT player for more than 15 years by now. I started programming in Java more or less shortly after Java 1.02 became available. Java is still my main love, but there were hot affairs with C, C++, perl, python, and other, less known languages. I am very much convinced that the only reasonable way to do "enterprise java" programming is to focus on "clean code" and "tdd/unit testing".

Updated on September 18, 2022

Comments

  • GhostCat
    GhostCat over 1 year

    I have a framework written in python, and for testing purposes I basically want to do a subprocess (aka shell call) ... that should simply come back with a RC != 0. I tried to invoke some non-existing executable; or to run "exit 1"; but those are for some reason translated to a FileNotFoundError.

    So, what else could I do to trigger a return code != 0 (in a "reliable" way; meaning the command should not suddenly return 0 at a future point in time).

    I thought to "search" for a binary called exit, but well:

    > /usr/bin/env exit
    /usr/bin/env: exit: No such file or directory
    
  • fviktor
    fviktor almost 6 years
    No, it does not. At least on Ubuntu 16.04.4 running bash the following command line gives 1 instead of 5: /bin/false 5; echo $?
  • steeldriver
    steeldriver almost 6 years
    @fviktor it exits with non-zero status - as the synopsis says, command line arguments (such as 5) are ignored
  • fviktor
    fviktor almost 6 years
    You're right. Just no way to control the actual non-zero exit code.
  • Matt Browne
    Matt Browne over 4 years
    +1. Note that if you're writing this in a shell script, you can just do exit RETURNCODE (without the bash -c part)