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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:24:16+00:00 2026-05-26T10:24:16+00:00

How can a bash script execute even after encountering a statement to delete itself?

  • 0

How can a bash script execute even after encountering a statement to delete itself?
For eg when I ran test.sh script which conains:

<--some commands-->
rm test.sh
<--some more commands-->

end

The script executes till the end before deleting itself

  • 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-26T10:24:17+00:00Added an answer on May 26, 2026 at 10:24 am

    What actually happens is that bash keeps the file open and rm won’t make that stop.

    So rm calls the libc function “unlink()” which will remove the “link” to the inode from the directory it’s in. This “link” is in fact a filename together with an inode number (you can see inode numbers with ls -i).

    The inode exists for as long as programs have it open.

    You can easily test this claim as follows:

    $ echo read a> ni
    $ bash ni
    

    while in another window:

    $ pgrep -lf bash\ ni
    31662 bash ni
    $ lsof -p 31662|grep ni
    bash    31662 wmertens  255r   REG   14,2         7 12074052 /Users/wmertens/ni
    $ rm ni
    $ lsof -p 31662|grep ni
    bash    31662 wmertens  255r   REG   14,2         7 12074052 /Users/wmertens/ni
    

    The file is still opened even though you can no longer see it in ls.
    So it’s not that bash read the whole file – it’s just not really gone until bash is done with it.

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

Sidebar

Related Questions

How can i, in a bash script, execute a command when the user stops
I have a bash script which generates an SCP command to execute. The relevant
Do lines in a bash script execute sequentially? I can't see any reason why
From a bash script how can i execute a perl script get the output
I have a script which allows to execute Bash processes in the background, i
Can anyone tell me why this bash script works if I cut and paste
How can I determine the name of the Bash script file inside the script
how can one turn off the error messages of a bash script? I want
I want to write a sh/bash script that can determine whether a particular directory
Can you use the bash getopts function twice in the same script? I have

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.