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 1113733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:54:04+00:00 2026-05-17T02:54:04+00:00

I am a noob in shell-scripting. I want to print a message and exit

  • 0

I am a noob in shell-scripting. I want to print a message and exit my script if a command fails. I’ve tried:

my_command && (echo 'my_command failed; exit)

but it does not work. It keeps executing the instructions following this line in the script. I’m using Ubuntu and bash.

  • 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-17T02:54:05+00:00Added an answer on May 17, 2026 at 2:54 am

    Try:

    my_command || { echo 'my_command failed' ; exit 1; }
    

    Four changes:

    • Change && to ||
    • Use { } in place of ( )
    • Introduce ; after exit and
    • spaces after { and before }

    Since you want to print the message and exit only when the command fails ( exits with non-zero value) you need a || not an &&.

    cmd1 && cmd2
    

    will run cmd2 when cmd1 succeeds(exit value 0). Where as

    cmd1 || cmd2
    

    will run cmd2 when cmd1 fails(exit value non-zero).

    Using ( ) makes the command inside them run in a sub-shell and calling a exit from there causes you to exit the sub-shell and not your original shell, hence execution continues in your original shell.

    To overcome this use { }

    The last two changes are required by bash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to get a shell(bash) script to insert a row into a
I'm a complete Python and Django noob so any help is appreciated. This is
There are so many goodies that come with a modern Unix shell environment that
Please forgive the noob question. I've been developing this little app (with Unity) for
Im a 15 year old noob to java. I am trying to make a
Noob-ish question: I'm looking for a lightweight but decent php way to search all
noob here, I'm trying to transfer a file using rsync from windows to linux.
noob question here. I have a has_many :through relationship between items and tags. Finding
I'm on a Mac running 10.6.4 Snow Leopard, and apparently ruby comes ready to
I am trying to develop a very basic console application that will retrieve a

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.