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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:50:49+00:00 2026-06-17T03:50:49+00:00

Take this script #!/bin/sh fd () { echo Hello world exit } trap fd

  • 0

Take this script

#!/bin/sh

fd ()
{
  echo Hello world
  exit
}

trap fd EXIT INT

for g in {1..5}
do
  echo foo
  sleep 1
done

I would like fd to fire once, either from Control-C or if the script exits normally. However if you hit Control-C it will run twice. How can I fix this?

  • 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-17T03:50:50+00:00Added an answer on June 17, 2026 at 3:50 am

    Do cascading traps. exit 127 will run the EXIT trap and set the exit code to 127, so you can say

    #!/bin/sh
    
    fd () {
      echo Hello world
      # No explicit exit here!
    }
    
    trap fd EXIT
    trap 'exit 127' INT
    

    I remember learning this from other people’s scripts after struggling with various workarounds to your problem for several years. After that, I have noticed that some tutorials do explain this technique. But it is not documented clearly in e.g. the Bash manual page IMHO. (Or it wasn’t when I needed it. Maybe some things don’t change in 15 years … 🙂

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

Sidebar

Related Questions

In Unix, how would one do this? #!/bin/sh x=echo Hello | grep '^[A-Z]' I
I am writing a small python script like this: #!/usr/bin/env python from sqlite3 import
I have problem with python multithreaded Queues. I have this script, where producer take
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {
I would like to use Perl to take a previously generated SPSS syntax file
#!/bin/sh #My script echo Are you sure you want to reorganize your files? echo
Take this example: {-# OPTIONS_HADDOCK ignore-exports #-} module HaddockTest (e1) where -- * Exported
Take this Lorem Ipsum text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla
Take this query: SELECT * FROM MyTable WHERE MyColumn = 'SomeValue' ORDER BY SomeFakeQualifier.MyColumn
Take this example code (ignore it being horribly inefficient for the moment) let listToString

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.