What does the line "#!/bin/sh" mean in a UNIX shell script?
Asked Answered
O

5

177

I was going through some shell script tutorials and found the following sample program:

#!/bin/sh
clear
echo "HELLO WORLD"

Can anyone please tell me what the significance of the comment #!/bin/sh at the start is?

Oil answered 9/9, 2011 at 19:54 Comment(4)
ShebangMemorable
Good explanation on askubuntu: askubuntu.com/questions/141928/…Fetus
Possible duplicate of Why do you need to put #!/bin/bash at the beginning of a script file?Bearwood
Possible duplicate of Why do people write #!/usr/bin/env python on the first line of a Python script?Vange
U
176

It's called a shebang, and tells the parent shell which interpreter should be used to execute the script.

#!/bin/sh <--------- bourne shell compatible script
#!/usr/bin/perl  <-- perl script
#!/usr/bin/php  <--- php script
#!/bin/false <------ do-nothing script, because false returns immediately anyways.

Most scripting languages tend to interpret a line starting with # as comment and will ignore the following !/usr/bin/whatever portion, which might otherwise cause a syntax error in the interpreted language.

Undercroft answered 9/9, 2011 at 19:57 Comment(2)
It's usually the kernel, not the shell, which processes this. The shell simply calls the exec() system call on the file.Regularly
#!/bin/sh <--------- bourne shell compatible script We can't be sure that it's a bourne shell, it can be any shell depending on where /bin/sh soft link is pointing to usually. For instance, With the latest Ubuntu "sh" by default pointing to "dash".Tzong
A
54

When you try to execute a program in unix (one with the executable bit set), the operating system will look at the first few bytes of the file. These form the so-called "magic number", which can be used to decide the format of the program and how to execute it.

#! corresponds to the magic number 0x2321 (look it up in an ascii table). When the system sees that the magic number, it knows that it is dealing with a text script and reads until the next \n (there is a limit, but it escapes me atm). Having identified the interpreter (the first argument after the shebang) it will call the interpreter.

Other files also have magic numbers. Try looking at a bitmap (.BMP) file via less and you will see the first two characters are BM. This magic number denotes that the file is indeed a bitmap.

Aliform answered 10/9, 2011 at 15:59 Comment(0)
L
15

If the file that this script lives in is executable, the hash-bang (#!) tells the operating system what interpreter to use to run the script. In this case it's /bin/sh, for example.

There's a Wikipedia article about it for more information.

Liveryman answered 9/9, 2011 at 19:58 Comment(0)
L
8

The first line tells the shell that if you execute the script directly (./run.sh; as opposed to /bin/sh run.sh), it should use that program (/bin/sh in this case) to interpret it.

You can also use it to pass arguments, commonly -e (exit on error), or use other programs (/bin/awk, /usr/bin/perl, etc).

Locket answered 9/9, 2011 at 19:58 Comment(0)
M
-4

#!/bin/sh or #!/bin/bash has to be first line of the script because if you don't use it on the first line then the system will treat all the commands in that script as different commands. If the first line is #!/bin/sh then it will consider all commands as a one script and it will show the that this file is running in ps command and not the commands inside the file.

./echo.sh

ps -ef |grep echo
trainee   3036  2717  0 16:24 pts/0    00:00:00 /bin/sh ./echo.sh
root      3042  2912  0 16:24 pts/1    00:00:00 grep --color=auto echo
Mnemonic answered 7/1, 2015 at 11:10 Comment(2)
Stack Overflow isn't Twitter. Please take the time to actually spell out words such as "u" and "1st". You're helping write a reference book so proper grammar and spelling is significant. Also, pay attention to proper formatting. Commands that would be issued from the command-line, like ps, and code excerpts like #!/bin/sh should be formatted.Bearwood
I tried to clean up the English here but I'm still not sure what it means. I guess the answer tries to say that commands in the script will be executed as distinct commands and adding a shebang will show the script as a single command in a process listing. This isn't actually true.Regularly

© 2022 - 2024 — McMap. All rights reserved.