Bash Command to Exit Script in Terminal


Exiting a running Bash Script in Terminal

    If for some reason a bash script is running too long and you want to exit its execution, then you can make press the keyboard keys Control + C.

    Example:
    bash-3.2$ ./script.sh 
    ^C
    
    bash-3.2$ 

    When you press Control + C you will see a ^C character displayed on the Terminal and you are returned back to the bash shell prompt.


    To exit script execution the keyboard shortcut is the same regardless of your operating system - macOS/Ubuntu/Linux/Windows.

    Exit a Bash Script using Control + C

Exiting a Script from within a Bash Script

    On the other hand if you want to exit the script from within it, then you can make use of the exit 0 command. When this command is encountered while execution the script will terminal and return back to the prompt.

    Example:
    #!/bin/bash
    
    sleep 10
    exit 0
    echo "Hello there"

    When you run the above script, it exits at line number 2, and the 3rd echo line is not printed.

    Exit a bash script using exit 0 command

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap