Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:55:50+00:00 2026-05-28T04:55:50+00:00

I have seen both exit and exec used in bash script to stop script

  • 0

I have seen both “exit” and “exec” used in bash script to stop script execution if an error has occurred. For example:

if [ ! -f file ]; then
echo "no file"
exit 1
fi

and:

if [ ! -f file ]; then
exec echo "no file"
fi

What is the best practise here and why? Wider discussion/explanations regarding “exec” and “exit” are welcome as well 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:55:51+00:00Added an answer on May 28, 2026 at 4:55 am

    exit just exits the shell, returning the specified numeric exit code (or 0 if omitted) to the parent process. It is equivalent to the C library routine also called exit, or to returning from main.

    exec replaces the shell with a new executable image (still running in the same process), which will in due course exit and return some code or other to the parent process. It is roughly equivalent to the C library routine execvp.

    Your first example is almost, but not quite, the correct way to stop a script if an error has occurred. It should read

    if [ ! -f file ]; then
        echo "no file" >&2
        exit 1
    fi
    

    The >&2, which is missing from your example, causes the error message to go to stderr, where it belongs, instead of stdout, where it does not belong.

    Your second example is wrong. In addition to the error message going to the wrong place, exec echo will stop the script (because /bin/echo will do its thing and then exit) but it will return exit code 0 to the parent process. Exit code 0 means success. Programs running in the Unix environment must always make sure to return a nonzero exit code when they have failed.

    The proper use of exec is in shell scripts that do some set-up work and then invoke a long-lived program written in another language, and don’t have anything to do afterward, so there’s no point keeping the shell process hanging around. For example:

    #! /bin/sh
    PATH=/special/directory/for/foo:$PATH
    export PATH
    exec foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen and used javascript code that both has and omits the ';'
In css, I have seen both div.selector and selector being used to define styling
There are two progid's. I've seen both used. Anyone have any insight as to
I've seen both approaches used but have never heard that one way is preferred
I have been looking through some configuration files and I've seen both being used
I have seen member functions programed both inside of the class they belong to
I have read several post on both matters but I haven't seen anyone comparing
I have seen simple example Ajax source codes in many online tutorials. What I
In jQuery, I have seen both the following ways of defining a jQuery function:
I have seen Nutch and Heritrix way of crawling. They both have the concept

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.