Bash: What is the effect of "#!/bin/sh" in a bash script with curl

I make a complex and long line command to successful login in a site. If I execute it in Console it work. But if I copy and paste the same line in a bash script it not work.

I tried a lot of thing, but accidentally discovery that if I NOT use the line

#!/bin/sh

it work! Why this happens in my mac OSX Lion? What this config line do in a bash script?

Answers


A bash script that is run via /bin/sh runs in sh compatibility mode, which means that many bash-specific features (herestrings, process substitution, etc.) will not work.

sh-4.2$ cat < <(echo 123)
sh: syntax error near unexpected token `<'

If you want to be able to use full bash syntax, use #!/bin/bash as your shebang line.


"#!/bin/sh" is a common idiom to insure that the correct interpreter is used to run the script. Here, "sh" is the "Bourne Shell". A good, standard "least common denominator" for shell scripts.

In your case, however, "#!/bin/sh" seems to be the wrong interpreter.

Here's a bit more info:

http://www.unix.com/answers-frequently-asked-questions/7077-what-does-usr-bin-ksh-mean.html

Originally, we only had one shell on unix. When you asked to run a command, the shell would attempt to invoke one of the exec() system calls on it. It the command was an executable, the exec would succeed and the command would run. If the exec() failed, the shell would not give up, instead it would try to interpet the command file as if it were a shell script.

Then unix got more shells and the situation became confused. Most folks would write scripts in one shell and type commands in another. And each shell had differing rules for feeding scripts to an interpreter.

This is when the "#! /" trick was invented. The idea was to let the kernel's exec() system calls succeed with shell scripts. When the kernel tries to exec() a file, it looks at the first 4 bytes which represent an integer called a magic number. This tells the kernel if it should try to run the file or not. So "#! /" was added to magic numbers that the kernel knows and it was extended to actually be able to run shell scripts by itself. But some people could not type "#! /", they kept leaving the space out. So the kernel was exended a bit again to allow "#!/" to work as a special 3 byte magic number.

So #! /usr/bin/ksh and #!/usr/bin/ksh now mean the same thing. I always use the former since at least some kernels might still exist that don't understand the latter.

And note that the first line is a signal to the kernel, and not to the shell. What happens now is that when shells try to run scripts via exec() they just succeed. And we never stumble on their various fallback schemes.


The very first line of the script can be used to select which script interpreter to use.

With

#!/bin/bash

You are telling the shell to invoke /bin/bash interpreter to execute your script. Assure that there are not spaces or empty lines before #!/bin/bash or it will not work.


Need Your Help

What is the benefit of using sqlite3_data_count() over sqlite3_column_count() in the SQLite C API?

c api sqlite

After reading the docs, it seems the the function sqlite3_column_count does all the same, but doesn't have the restrictions that sqlite3_data_count has.

classic asp syntax highliting inside javascript in Visual web developer ee 2008

asp-classic visual-studio-express visual-web-developer

How can visual web developer Express Edition highlight the classic asp-Tags (&lt;% asp code %>) insight the Javascript code?