Create a New Shell With Two sh-bangs?

by mike on May 21, 2011

In the header of a script a declaration of what shell will be used is inserted.  So if you were using Bash as the shell you would insert this line in the header of the script:

#!/bin/bash

This line should be the first line and it calls the command interpreter.

What happens if you create a second line with “#!/bin/bash”, will that create a new shell?

When you receive a new shell, the variables that you have set in memory on the first shell do not carry forward.  So to test double entries of “#!/bin/bash” create a variable in the first shell and see it if carries over after tryinng ot invoke a new shell with “#!/bin/bash”.  Try this script to check.

#!/bin/bash
echo "This is the start of a bash shell script"
# variable
myvar=first_shell
echo
#!/bin/bash
echo "Wondering if this starts a new shell?"
echo "The answer is NO, since the variable $myvar remains the same."
exit

The second “#!/bin/bash” is simply interpreted as a comment.

Comments on this entry are closed.

Previous post:

Next post: