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

The Archive Base Latest Questions

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

I accidentally started a bash script with $! instead of #! and got some

  • 0

I accidentally started a bash script with $! instead of #! and got some very weird behavior. I’m trying to figure out what happened.

If you try this script:

$!/bin/bash
echo Hello world!

you will get the following behavior:

$ chmod +x hello
$ ./hello
[nothing happens, get prompt back]
$ exit
exit
Hello world!
$

So it looks like this happened:

  1. A new bash shell spawned.
  2. Upon exit, the rest of the script executed.

What’s up? How is anything at all happening? Without #!, how does the shell know to use bash to interpret the script?

Obviously this is a “satisfy my curiosity” rather than “solve my problem” question. Googling does not yield much, probably because #! and $! in queries don’t make the Google-bot happy.

  • 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-26T05:41:17+00:00Added an answer on May 26, 2026 at 5:41 am

    $something is a parameter (“variable”) expansion, but $! in particular returns a value that isn’t set in your script, so it expands as a zero length string.

    Therefore your script is, you are correct, the equivalent of:

    /bin/bash
    echo Hello world!
    

    The shebang magic number is an old feature of Unix, but the shell is even older. A text file with the execute bit set that the kernel cannot exec (because it’s not actually compiled) is executed by a subshell. That is, the shell deliberately runs another shell and passes the pathname as the parameter. This is how commands written as shell scripts were executed before shebang was invented, and it’s still there in the code.

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

Sidebar

Related Questions

Accidentally I may forget to describe some parameters or exception throwing (or something else)
I accidentally committed too many files to an SVN repository and changed some things
Everyone has accidentally forgotten the WHERE clause on a DELETE query and blasted some
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
A while ago, I was writing some code, but accidentally deleted some good code
While writing a recent application I accidentally started filling an array before I had
I accidentally pushed some changes to the main branch which weren't ready, so I
I'm trying to add a_folder to SVN but I accidentally scheduled my root directory
Finally, I find some article in http://code.google.com/intl/en/web/ajaxcrawling/docs/getting-started.html msnbc use this method. Thanks for all
I have accidentally checked in some files that I meant keeping locally on my

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.