85

Is a space or more allowed between #! (shebang) and the interpreter?

For example, #! /bin/bash. It seems to work, but some said that it is incorrect.

3
  • 2
    Read in-ulm.de/~mascheck/various/shebang/#blankrequired Commented Apr 15, 2016 at 16:37
  • So a space is not necessary? I've always seen at least one... Commented Apr 16, 2016 at 8:45
  • 11
    @Bakuriu Where? I've almost never seen one... Commented Apr 16, 2016 at 19:05

3 Answers 3

90

Yes, this is allowed.

The Wikipedia article about the shebang includes a 1980 email from Dennis Ritchie, when he was introducing kernel support for the shebang (as part of a wider package called interpreter directives) into Version 8 Unix (emphasis mine):

The system has been changed so that if a file being executed begins with the magic characters #!, the rest of the line is understood to be the name of an interpreter for the executed file. […]

To take advantage of this wonderful opportunity, put

#! /bin/sh 

at the left margin of the first line of your shell scripts.

Blanks after ! are OK.

So spaces after the shebang have been around for quite a while, and indeed, Dennis Ritchie’s example is using them.

Note that early versions of Unix had a limit of 16 bytes in this interpreter line, so you couldn’t have many spaces there. Modern kernels usually have a higher limit¹.


¹ On Linux, it was initially 1023 bytes, then lowered to 127 bytes in 0.12 in 1992, then raised to 255 in 5.1 in 2019 (see commit); that shebang line will be truncated if longer though not if it's the path of the interpreter that would be truncated in which case the exec fails with ENOEXEC instead.

2
  • 3
    Note that the character limit still exists on linux at least - and is 127. See stackoverflow.com/a/10813634/6857772 Commented Mar 15, 2024 at 14:57
  • @JamesSharpe on Linux it was raised to 255 in 2019. See edit. Commented Oct 3 at 11:07
48

Yes, blanks are allowed after the #!. There was even a (mistaken) thought that some systems might require it, but it has always just been optional.

For further reading try here

0
9

FYI, some versions of systemd fail to parse shebangs with a space. With a service like this:

[Service] Type=oneshot ExecStart=/root/foo.sh 

... and a shebang like this:

#! /bin/bash 

... you may get an error like this:

systemd[32834]: foo.service: Failed at step EXEC spawning /root/foo.sh: Exec format error 

Removing the space from the shebang fixes the error. So look out for that one.

5
  • 5
    Ah. yet another reason for me to be grumpy about systemd; Just what I needed… Thanks for the heads-up. Commented Jan 6, 2021 at 22:13
  • 6
    FWIW, I can't reproduce with systemd 245.4-4ubuntu3.6 (on Ubuntu 20.04). Commented Jun 21, 2021 at 15:37
  • 4
    The only reference to shebang I find in the source code of systemd is in ./test/test-functions about a _shebang_regex='(#! *)(/[^ ]+).*' (from someone who doesn't know that bash regexps are not anchored apparently) which explicitly allows those spaces (but is not otherwise related to executing services). Commented Jun 21, 2021 at 15:54
  • 6
    I'm not sure why systemd would want to parse shebangs by itself. Also, I'd expect a "no such file or directory" error if systemd tried to execute " /bin/bash" instead of "/bin/bash". It all suggests systemd is fine but your issue was elsewhere. Maybe you had a UTF-8 BOM at the start of your #! /bin/bash script? Commented Jun 21, 2021 at 15:56
  • 1
    I just see the obvious system call too, execve("/root/test.sh", ["/root/test.sh"], [/* 6 vars */]) = 0 So, no, this doesn't seem to be true. (tested on a way older systemd, though) Commented Jun 21, 2021 at 20:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.