Questions tagged [shebang]

Use the tag /shebang if your question is about `#!`-string at the beginning of a script

A Shebang is the character sequence consisting of the characters number sign and exclamation mark (that is, "#!") at the beginning of a script.

Use the tag if your question is about shebang usage.

Further reading

128 questions
654
votes
10 answers

Why is it better to use "#!/usr/bin/env NAME" instead of "#!/path/to/NAME" as my shebang?

I notice that some scripts which I have acquired from others have the shebang #!/path/to/NAME while others (using the same tool, NAME) have the shebang #!/usr/bin/env NAME. Both seem to work properly. In tutorials (on Python, for example), there…
TheGeeko61
  • 6,911
  • 3
  • 17
  • 18
114
votes
5 answers

Does the shebang determine the shell which runs the script?

This may be a silly question, but I ask it still. If I have declared a shebang #!/bin/bash in the beginning of my_shell_script.sh, so do I always have to invoke this script using bash [my@comp]$bash my_shell_script.sh or can I use…
jrara
  • 2,069
  • 8
  • 23
  • 20
106
votes
8 answers

#!/bin/bash - no such file or directory

I've created a bash script but when I try to execute it, I get #!/bin/bash no such file or directory I need to run the command: bash script.sh for it to work. How can I fix this?
Nicolas de Fontenay
  • 4,209
  • 8
  • 21
  • 18
91
votes
1 answer

Why does the following script delete itself?

If you create an executable file with the following contents, and run it, it will delete itself. How does this work? #!/bin/rm
user253751
  • 2,211
  • 2
  • 16
  • 17
86
votes
8 answers

Multiple arguments in shebang

I am wondering whether there is a general way of passing multiple options to an executable via the shebang line (#!). I use NixOS, and the first part of the shebang in any script I write is usually /usr/bin/env. The problem I encounter then is that…
Rastapopoulos
  • 1,569
  • 1
  • 10
  • 21
77
votes
3 answers

Is space allowed between #! and /bin/bash in shebang?

In a shebang, is a space or more allowed between #! and the interpreter? For example, #! /bin/bash. It seems work, but some said that it is incorrect.
Tim
  • 98,580
  • 191
  • 570
  • 977
64
votes
3 answers

Shebang starting with `//`?

I'm confused about following script (hello.go). //usr/bin/env go run $0 $@ ; exit package main import "fmt" func main() { fmt.Printf("hello, world\n") } It can execute. (on MacOS X 10.9.5) $ chmod +x hello.go $ ./hello.go hello, world I…
kawty
  • 743
  • 5
  • 7
53
votes
6 answers

Is there any reason to have a shebang pointing at /bin/sh rather than /bin/bash?

In most shell scripts I've seen (besides ones I haven't written myself), I noticed that the shebang is set to #!/bin/sh. This doesn't really surprise me on older scripts, but it's there on fairly new scripts, too. Is there any reason for preferring…
Jules
  • 2,064
  • 2
  • 24
  • 35
47
votes
2 answers

Use .sh or .bash extension for Bash scripts?

(See Use #!/bin/sh or #!/bin/bash for Ubuntu-OSX compatibility and ease of use & POSIX) If I want my scripts to use the Bash shell, does using the .bash extension actually invoke Bash or does it depend on system config or the first shebang line. If…
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
42
votes
4 answers

What exactly happens when I execute a file in my shell?

So, I thought I had a good understanding of this, but just ran a test (in response to a conversation where I disagreed with someone) and found that my understanding is flawed... In as much detail as possible what exactly happens when I execute a…
Josh
  • 8,311
  • 12
  • 54
  • 73
35
votes
5 answers

Is it recommended to use zsh instead of bash scripts?

Can I assume that enough people have zsh installed to run scripts with a #!/usr/bin/env zsh as shebang? Or will this make my scripts un-runnable on too many systems? Clarification: I’m interested in programs/scripts an end user might want to run…
Profpatsch
  • 1,749
  • 4
  • 20
  • 23
33
votes
3 answers

Run script in a screen

I want to run a bash script in a detached screen. The script calls a program a few times, each of which takes too long to wait. My first thought was to simply open a screen and then call the script, but it appears that I can't detach (by ctrl-a d)…
bongbang
  • 541
  • 1
  • 6
  • 10
30
votes
4 answers

Why no shebang in .bashrc/.bash_profile?

Simple inquiry: I have just realized that I have never seen a shebang on top of a .bashrc script, which leads me to think the system uses the default shell to source it upon login (${SHELL}). I am pondering over reasons why that is the case, i.e.…
amphibient
  • 12,222
  • 18
  • 62
  • 87
29
votes
3 answers

Why not use pathless shebangs?

Is it possible to have a shebang that, instead of specifying a path to an interpreter, it has the name of the interpreter, and lets the shell find it through $PATH? If not, is there a reason why?
Amelio Vazquez-Reina
  • 40,169
  • 77
  • 197
  • 294
29
votes
3 answers

Which shell interpreter runs a script with no shebang?

Suppose the default shell for my account is zsh but I opened the terminal and fired up bash and executed a script named prac002.sh, which shell interpreter would be used to execute the script, zsh or bash? Consider the following…
7_R3X
  • 1,134
  • 2
  • 14
  • 28
1
2 3
8 9