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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:29:07+00:00 2026-05-18T22:29:07+00:00

In shell scripts set -e is often used to make them more robust by

  • 0

In shell scripts set -e is often used to make them more robust by stopping the script when some of the commands executed from the script exits with non-zero exit code.

It’s usually easy to specify that you don’t care about some of the commands succeeding by adding || true at the end.

The problem appears when you actually care about the return value, but don’t want the script to stop on non-zero return code, for example:

output=$(possibly-failing-command)
if [ 0 == $? -a -n "$output" ]; then
  ...
else
  ...
fi

Here we want to both check the exit code (thus we can’t use || true inside of command substitution expression) and get the output. However, if the command in command substitution fails, the whole script stops due to set -e.

Is there a clean way to prevent the script from stopping here without unsetting -e and setting it back afterwards?

  • 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-18T22:29:07+00:00Added an answer on May 18, 2026 at 10:29 pm

    Yes, inline the process substitution in the if-statement

    #!/bin/bash
    
    set -e
    
    if ! output=$(possibly-failing-command); then
      ...
    else
      ...
    fi
    

    Command Fails

    $ ( set -e; if ! output=$(ls -l blah); then echo "command failed"; else echo "output is -->$output<--"; fi )
    /bin/ls: cannot access blah: No such file or directory
    command failed
    

    Command Works

    $ ( set -e; if ! output=$(ls -l core); then echo "command failed"; else echo "output is: $output"; fi )
    output is: -rw------- 1 siegex users 139264 2010-12-01 02:02 core
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I set up a shell script to execute from the Mac OSX
I'm trying to write a shell script that, when run, will set some environment
I was using curl from shell scripts and was setting -s option to make
Given the dangers of SUID shell scripts , is there a more secure way
I have a shell script that executes a number of commands. How do I
From a shell script, how do I check if a directory contains files? Something
If I'm writing a shell script and I want to source some external (c-)shell
Is there a standard set of return code for Window shell scripts (*.bat files)?
I am going to add some Linux shell scripts to CVS. Can I download
We'd like to ship some helper scripts (shell scripts) as part of an application

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.