Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    By RahulDecember 27, 20224 Mins Read

    In Bash, it is often necessary to check if a command succeeded or failed. For example, you may want to execute different commands based on the success or failure of a command, or you may want to perform error handling in a script. To check if a command succeeded or failed in Bash, you can examine the exit status of the command. The exit status of a command is a numerical value that indicates the success or failure of the command. A command with an exit status of 0 indicates success, and a command with a non-zero exit status indicates failure.

    Advertisement

    In this article, we will explore different ways to check the exit status of a command in Bash, including using the `$?` special variable, using the `&&` and `||` operators, and using scripts and functions. Understanding how to check the exit status of a command in Bash can be helpful in various scenarios when working with Bash.

    Checking If a Command Succeeded in Bash

    In Bash, you can check if a command succeeded or failed by examining the exit status of the command. The exit status of a command is a numerical value that indicates the success or failure of the command. A command with an exit status of 0 indicates success, and a command with a non-zero exit status indicates failure.

    To check the exit status of a command in Bash, you can use the $? special variable, which stores the exit status of the most recently executed command.

    For example, consider the following command:

    ls -l /etc/ 
    

    This command lists the contents of the `/etc` directory in long format. To check if this command succeeded, you can use the following code:

    1
    2
    3
    4
    5
    6
    ls -l /etc/
    if [ $? -eq 0 ]; then
        echo "Command succeeded"
    else
        echo "Command failed"
    fi

    This code checks the exit status of the ls command by examining the value of the $? special variable. If the value of `$?` is `0`, the command succeeded, and the code prints “Command succeeded”. If the value of `$?` is non-zero, the command failed, and the code prints “Command failed”.

    Check Exit Status of a Function

    You can also use the `$?` special variable to check the exit status of a command in a script or function. For example, consider the following script:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #/usr/bin/env bash
     
    check_command() {
        command
        if [ $? -eq 0 ]; then
            return 0
        else
            return 1
        fi
    }
     
    check_command ls -l /etc/
    if [ $? -eq 0 ]; then
        echo "Command succeeded"
    else
        echo "Command failed"
    fi

    This script defines a function check_command that takes a command as an argument and checks the exit status of the command. The function returns `0` if the command succeeded and `1` if the command failed. The script then calls the check_command function with the ls command and checks the exit status of the function by examining the value of the `$?` special variable. If the value of `$?` is `0`, the command succeeded, and the script prints “Command succeeded”. If the value of `$?` is `1`, the command failed, and the script prints “Command failed”.

    Check Exit Status Using `&&` And `||` in Bash

    In addition to using the `$?` special variable, you can also use the `&&` and `||` operators to check the exit status of a command and execute different commands based on the result. For example, consider the following code:

    1
    ls -l /etc/ && echo "Command succeeded" || echo "Command failed"

    This code checks the exit status of the `ls`‘ command and executes different commands based on the result. If the ls command succeeded, the code prints “Command succeeded”. If the ls command failed, the code prints “Command failed”.

    Conclusion

    In conclusion, checking if a command succeeded or failed in Bash is a useful task that can be accomplished by examining the exit status of the command. The exit status of a command is a numerical value that indicates the success or failure of the command. A command with an exit status of `0` indicates success, and a command with a non-zero exit status indicates failure. To check the exit status of a command in Bash, you can use the $? special variable, which stores the exit status of the most recently executed command.

    You can also use the $? special variable to check the exit status of a command in a script or function or use the `&&` and `||` operators to check the exit status and execute different commands based on the result. Understanding how to check the exit status of a command in Bash can be helpful in various scenarios, such as when you want to execute different commands based on the success or failure of a command or when you want to perform error handling in a script.

    bash exit status script shell
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    How To Customize Shell Prompt (PS1) In Linux

    How To Customize Bash Prompt (PS1) In Linux

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    • sleep Command in Linux with Examples
    • 20 Basic Linux Commands for the Beginners (Recommended)
    • tail Command in Linux with Examples
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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