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

  • SEARCH
  • Home
  • 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 8091043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:55:53+00:00 2026-06-05T19:55:53+00:00

I am writing some BASH script and I want it have some error handling

  • 0

I am writing some BASH script and I want it have some error handling mechanism:

function f()
{
  command1 || { echo "command1 failed"; return 1; }
  command2 || { echo "command2 failed"; return 1; }
  command3 || { echo "command3 failed"; return 1; }
  command4 || { echo "command4 failed"; return 1; }
}

I want to make this repetitive structure more readable by defining some function:

function print_and_return()
{
  echo "$@"
  # some way to exit the caller function
}

so that I can write function f as

function f()
{
  command1 || print_and_return "command1 failed"
  command2 || print_and_return "command2 failed"
  command3 || print_and_return "command3 failed"
  command4 || print_and_return "command4 failed"
}

What’s the best way to achieve this? Thanks.

  • 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-06-05T19:55:55+00:00Added an answer on June 5, 2026 at 7:55 pm

    Maybe you can use set -e, though you have to be careful since it exits the shell:

    function f()
    {
        (
        set -e
        command1
        command2
        command3
        command4
        )
    }
    

    As long as the commands diagnose any problems, this function will stop when the first command fails.

    Test version:

    function f()
    {
        (
        set -e
        true
        echo true
        true
        echo true
        false
        echo false
        true
        echo true
        )
    }
    
    echo calling function
    f
    echo still here
    

    Output:

    calling function
    true
    true
    still here
    

    NB: When I used the sequence:

    echo calling function
    if f
    then echo still here - f passed
    else echo still here - f failed
    fi
    

    Then the function behaved differently under bash 3.2.48 on Mac OS X 10.7.4:

    calling function
    true
    true
    false
    true
    still here - f passed
    

    So, inventive, but not wholly reliable.

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

Sidebar

Related Questions

I'm writing a Bash script that prints some text to the screen: echo Some
I am writing a Bash script to set some environment variables and then start
I am writing a bash script that is supposed to do some confirmation and
I'm writing a bash script to get some podcasts. The problem is that some
I'm writing a script in Bash to test some code. However, it seems silly
I am writing some testing scripts and want to catch all error output and
I want to pass an array parameter to a function in bash, and writing
I want to use PHP writing git hooks but have some problem. I use
I am writing a bash script that calls some apps that run in their
I am writing some BASH shell script that will continuously check a file to

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.