Questions tagged [shebang]

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

Filter by
Sorted by
Tagged with
540
votes
10answers
222k views

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....
102
votes
5answers
73k views

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]$...
86
votes
8answers
254k views

#!/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?
84
votes
1answer
10k views

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
61
votes
3answers
4k views

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) $ ...
54
votes
6answers
6k views

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 ...
54
votes
7answers
16k views

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 /...
51
votes
3answers
8k views

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.
39
votes
2answers
57k views

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 ...
38
votes
4answers
5k views

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 ...
27
votes
3answers
101k views

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, ...
27
votes
5answers
32k views

Is it recommended to use zsh instead of bash scripts? [closed]

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 ...
26
votes
1answer
3k views

At sign after shebang?

In the Apache httpd project's "support/apxs.in" script, a text surrounded with @ signs comes after #!. That is, the first line of the script is: #!@perlbin@ -w Is this a Perl thing or a UNIX kernel ...
25
votes
4answers
7k views

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 ...
25
votes
3answers
4k views

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?
22
votes
3answers
8k views

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 ...
21
votes
2answers
28k views

What do the arguments '-v' and '-x' mean to bash?

I have seen a couple of shell scripts with the following shebang: #!/bin/bash -x -v However, man bash does not explain what these arguments -x and -v stand for, if they are belong to bash at all. ...
19
votes
5answers
9k views

Why is “shebang” called “shebang”?

Does "shebang" mean "bang she"? Why not "hebang" as "bang he"?
17
votes
6answers
6k views

Choose interpreter after script start e.g. if/else inside hashbang

Is there any way to dynamically choose the interpreter that's executing a script? I have a script that I'm running on two different systems, and the interpreter I want to use is located in different ...
17
votes
3answers
2k views

Why is the behavior of the `#!` syntax unspecified by POSIX?

From the Shell Command Language page of the POSIX specification: If the first line of a file of shell commands starts with the characters "#!", the results are unspecified. Why is the behavior of #...
16
votes
2answers
1k views

How does this shebang that starts with a double hyphen (--) work?

I have found the following kind of shebang in the RosettaCode page: --() { :; }; exec db2 -txf "$0" It works for Db2, and a similar thing for Postgres. However, I do not understand the whole line. ...
15
votes
6answers
4k views

Is it correct to use /bin/sh in the hashbang if the Bourne shell isn't available in a distribution?

Generally, shell scripts contain the following comment at the first line of the script file: #!/bin/sh. According to the researches that I made, this is called "hash bang" and it is conventional ...
13
votes
3answers
3k views

Why doesn't “ps ax” find a running bash script without the “#!” header?

When I run this script, intended to run until killed... # foo.sh while true; do sleep 1; done ...I'm not able to find it using ps ax: >./foo.sh // In a separate shell: >ps ax | grep foo.sh ...
13
votes
2answers
7k views

shell script header for best compatibility [duplicate]

Which one is better: #!/usr/bin/env sh #!/bin/sh empty/no header I used to think the 1st one is the best, anyway i've found on some Linux-based systems (like Android) that pathname is missing, so ...
12
votes
6answers
2k views

Too many shebang (script declaration) lines — any way to reduce their amount?

I have a project comprised of about 20 small .sh files. I name these "small" because generally, no file has more than 20 lines of code. I took a modular approach because thus I'm loyal to the Unix ...
12
votes
4answers
3k views

Distributing a script: Should I use /bin/gawk or /usr/bin/gawk for shebang?

Is gawk in /bin or /usr/bin usually? I would go with #!/usr/bin/env gawk but then I can't use arguments. Right now I'm using #!/bin/gawk -f. The script is very long and contains a lot of single ...
12
votes
2answers
42k views

Why is #!/usr/bin/env bash not working on my system?

I ran into some issues when running some installation scripts where they complained of bad interpreter. So I made a trivial example but I can't figure out what the problem is, see below. #!/usr/bin/...
11
votes
1answer
59k views

No such file or directory but I can see it!

I'm trying to run a python script, on a headless Raspberry PI using winSCP and get the following error message: Command '"./areadetect_movie_21.py"' failed with return code 127 and error message /usr/...
10
votes
3answers
941 views

Why does the “she-bang” begin with a “#!”?

Why does the "she-bang" begin with a #!, like #!/bin/bash? I have always accepted that this how it is done, but is there a reason behind it? Why start with #; isn't that usually a comment? Or is it ...
10
votes
1answer
3k views

shebang line not working with cr-lf

Why are the shebang parts of the following elementary scripts not working: $ cat hello.sh #! /bin/sh echo Hello $ ./hello.sh bash: ./hello.sh: /bin/sh^M: bad interpreter: No such file or directory $ ...
9
votes
3answers
1k views

Can I use a variable content as a shebang? [closed]

I have a shell script in which I want to add a shebang. Given a variable defined as follows: SHEBANG="#!/bin/sh" My question is if I can use that variable in another script like this: $SHEBANG # ...
9
votes
1answer
1k views

POSIX shell scripts shebang #!/bin/sh vs #!/usr/bin/env sh, any difference?

I recently noticed that many scripts are using /usr/bin/env in their shebang. I have seen that mainly using Bash and Python, but thus far never in conjunction with POSIX sh (ash, dash,...). I wonder ...
9
votes
2answers
23k views

Bash Script Permission denied & Bad Interpreter

I'm on a kali linux 64 bit. I have created a python script which takes 2 arguments to start. I don't want to type out every time the exact same paths or search in the history of the commands I used ...
8
votes
2answers
2k views

Multiple shebangs in a single bash file

I have a script from other person which has a look (note: it's a single file): #!/bin/bash some commands some commands #!/bin/bash some commands some commands #!/bin/bash some commands some ...
7
votes
5answers
37k views

Running python script from Linux Terminal

I have downloaded this script named, pyAES.py and put it in a folder name codes, inside a Desktop directory of my Linux, According to this example, http://brandon.sternefamily.net/2007/06/aes-...
7
votes
3answers
5k views

How to share a GNU sed script between Linux and Mac OS X

I have a GNU sed script I use on Linux; it is installed at /bin/sed and it seems it contains GNUisms. I have collaborators using Mac OS X. They have installed (non-GNU) sed, located at /usr/bin/sed, ...
7
votes
4answers
4k views

Shebang does not set SHELL in cron

I have a script containing: #!/bin/bash printenv When I run it from the command line: env testscript.sh bash testscript.sh sh testscript.sh every time, it outputs SHELL=/bin/bash. However, when it ...
6
votes
1answer
1k views

Perl and Python wrongly interpreted via shebang on Linux

All my Python and Perl scripts are simply NOT iterpreted via shebang. Never. But they work as expected when I explicitly call the binary. I double checked my Perl and Python installations, it is ...
6
votes
1answer
1k views

How should I handle the shebang when writing my own shell?

I'm writing a script to test a shell project to see that my custom shell has correct output. str="HELLO" echo $str echo "*** YOU SHOULD SEE HELLO ABOVE ***" ls * echo "*** YOU SHOULD SEE THE OUTPUT ...
6
votes
1answer
553 views

What is the difference in these two bash environments?

I notice that with bash scripts, some people use a different shebang to the one that I'm used to putting at the top of my own. Can someone simplify the difference between these two? I use the #!/bin/...
6
votes
2answers
3k views

How to control buffering of pipes in bash scripts?

I need my script to be executed with stdbuf -i0 -o0 -e0 otherwise user will wonder why there is no output (becuase of pipe buffer). How can I acheive it? #!/usr/bin/stdbuf -i0 -o0 -e0 /bin/bash ...
5
votes
5answers
3k views

Can I use a shebang to have a file source itself into current bash environment?

I have a growing collection of scripts which should be sourced, not run. At the moment they have the shebang #! /bin/cat but I would prefer the have them be sourced into bash when run, in the same ...
5
votes
2answers
631 views

run perl script with unknown perl location

I'm developing a perl script which expected to be downloaded by Mac users with a very small knowledge of shell, linux etc, let's say office managers and accountants. After the downloading the script ...
5
votes
2answers
914 views

Creating a shebang pointing portably to an interpreter in the folder of a script

I have a JS file (file.js) that I want to have executed as a command line shell script through nodejs (iojs actually); I'm using MINGW Git Bash on Windows. The standard way to do it is to put the ...
4
votes
6answers
4k views

shebang or not shebang

I want to use a program in the shebang, so I create a script named <myscript> with: #!<mypgm> I also want to be able to run <mypgm> directly from the command prompt. <mypgm> ...
4
votes
3answers
264 views

Who runs the interpreter for files that are execute-only?

If the current user only has execute (--x) permissions on a file, under which user does the interpreter (specified by #!/path/to/interpreter at the beginning of the file) run? It couldn't be the ...
4
votes
2answers
3k views

Why am I able to pass arguments to /usr/bin/env in this case?

I read in another answer that I'm not able to pass arguments to the interpreter than I'm giving to /usr/bin/env: Another potential problem is that the #!/usr/bin/env trick doesn't let you pass ...
4
votes
2answers
259 views

Distance of a command from a shebang?

A shebang (#!/bin/sh) is placed on the first line of a bash script, and it's usually followed on the second line by a comment describing what action the script performs. What if, for no particular ...
4
votes
2answers
2k views

Content of /proc/<PID>/cmdline file depends on script shebang?

I have a bash script named test.sh which I start with startproc. In case I use #!/usr/bin/env bash shebang in test.sh, the /proc/<PID>/cmdline file looks following: SERVER:~ # cat /proc/29481/...
3
votes
5answers
8k views

Why is the first line comment of .sh file necessary? [duplicate]

I was told that the first line shebang (#!/bin/bash) is necessary in the shell script file, and the file won't run accurately without this line. But I tested some scripts. They work just fine without ...