
Shebang Quine - geon
https://github.com/geon/shebang-abuse/blob/master/quine
======
Exuma
Here's my favorite one ever...

[http://aem1k.com/world/](http://aem1k.com/world/)

~~~
takeda
This one is also amazing.

[https://github.com/mame/quine-relay](https://github.com/mame/quine-relay)

~~~
djsumdog
I remember when this one was released and we were passing the link around on
e-mail threads in the office. It's pretty amazing and insane.

------
takeda
Is this technically a quine though?

In any other language, a program that opens and prints file containing its own
source code is not considered a quine.

~~~
geon
Probably not.

------
adrianratnapala
Hmm, when we speak of quining, we ask "How in X programming language, do we
write a program that outputs it's own text".

But in what language is this "program" written?

The best I can guess is that the unix shebang hack/convention is a (very, very
minimal) language all of its own.

~~~
maze-le
I is not 'exactly' a quine, because the tool 'cat' is a program on its own,
and not a shell command. Still, it is an entertaining and unexpected outcome.

~~~
hkjgkjy
I'm curious to know where the line of a "program" goes. I imagine a OS without
file system, but only with functions (like as if emacs was an OS). Would a
function then be called a program?

~~~
maze-le
Well, if your EmacsOS allocates exclusive adress-space for the function-call,
so that it can be executed in a very own execution context, independent of
other function-calls... then you could call it a program.

~~~
khedoros
In that definition, it seems like DOS OSes never had programs.

------
malisper
If anyone's curious, I wrote a blog post a while ago on how to write programs
that obtain their own source code: [http://malisper.me/2016/04/20/writing-
self-referential-progr...](http://malisper.me/2016/04/20/writing-self-
referential-programs/)

------
tehrei
A more philosophical shebang that I like is

    
    
       #!/bin/rm

------
nmc
Very fun.

For those who are puzzled by the term "quine":
[http://meta.codegolf.stackexchange.com/questions/4877/what-c...](http://meta.codegolf.stackexchange.com/questions/4877/what-
counts-as-a-proper-quine)

------
cyphar
I like the fact that #!/proc/self/exe causes the interpreter to be the calling
program (since the interpreter is evaluated in the context of the calling
program, from the kernel).

------
geon
I was thinking of unorthodox uses for the shebang, and realized cat gives you
a very succinct quine.

Any other funny commands? ls -l is cool too.

~~~
gkya

      #!/bin/rm
      #!/sbin/md5
      #!/usr/bin/file
      #!/usr/bin/du -h
    

And yet another quine:

    
    
      #!./quine
      cat $0
    

Edit: I was not sure about #!/sbin/poweroff, and I cannot try now, so removed.

~~~
cyphar
I'm fairly sure `#!./quine` will cause an error because there's a limit of 5
recursive checks to find the interpreter of a script (fs/binfmt_script.c).

~~~
gkya
Ran it, works. I'm on freebsd 10.3 BTW.

~~~
cyphar
On Linux you get ELOOP. How does that resolution even work on FreeBSD? Why
does it resolve to $SHELL (the file resolves to itself).

~~~
gkya
execve(2) on my machine defines ELOOP lie this: _Too many symbolic links were
encountered in translating the pathname._

Refer to sys/kern/imgact_shell.c and ^do_execve in sys/kern/kern_exec.c, I
skimmed but could not find why it ran.

~~~
cyphar
Presumably then, whatever shell you're using decides to just execute the code
as a subshell rather than use execve(2). Can you dtrace it to figure out
whether it actually calls execve(2)?

~~~
gkya
After running dtrace as

    
    
      $ sudo dtrace -n 'syscall::execve*:entry { printf("%s %s", execname, copyinstr(arg0)); }'
    

I had this output

    
    
        0  51617                     execve:entry sh ./quine
        1  51617                     execve:entry sh /bin/sh
        1  51617                     execve:entry sh /bin/cat
    

Altho this is the first time I use dtrace. I can try to run another command
you can ask me to.

My login shell is the Bourne shell. However I tried bash, csh and tcsh too, it
works, so it's probably the system level code that substitutes /bin/sh (maybe
because it's my login shell?) to the interpreter.

~~~
cyphar
I think shells are doing that. Otherwise you wouldn't see the
execve("/bin/sh") syscall entry (it would get silently translated inside the
kernel).

------
CobrastanJorji
Is this technically a quine? It uses its own file as an input. I think that
may technically disqualify it from quineness.

------
JoshTriplett
Here's a quine written as an ld linker script:

    
    
        TARGET(binary)
        OUTPUT_FORMAT(elf32-i386)
        ENTRY(_start)
        SECTIONS
        {
            . = 2M;
            .text 2M: {
                _start = .;
                BYTE(0xb8); LONG(4)         /* mov $4, %eax */
                BYTE(0xbb); LONG(1)         /* mov $1, %ebx */
                BYTE(0xb9); LONG(data);     /* mov data, %ecx */
                BYTE(0xba); LONG(data_len); /* mov data_len, %edx */
                BYTE(0xcd); BYTE(0x80);     /* int $0x80 */
                BYTE(0xb8); LONG(1)         /* mov $1, %eax */
                BYTE(0x31); BYTE(0xdb);     /* xor %ebx, %ebx */
                BYTE(0xcd); BYTE(0x80);     /* int $0x80 */
                data = .;
                *(.data);
                data_len = ABSOLUTE(. - data);
            }
        }
    

Building and running it:

    
    
        /tmp$ ld -T quine.ld quine.ld -o quine
        /tmp$ ./quine | diff -s quine.ld -
        Files quine.ld and - are identical

------
emson
So if anyone is interested here are some "Elixir" quine attempts:
[http://elixirgolf.com/articles/elixir-quine-self-
replicating...](http://elixirgolf.com/articles/elixir-quine-self-replicating-
program/)

------
jessaustin
Haha treating _cat_ as an interpreter just sends the script on stdin.

~~~
tantalor
Actually, sends the name of the script as an argument to cat, not stdin.

------
mijoharas
First argument in bash is the name of the file. So, this is a quine, since the
output of running it is the contents of the script, which is:

#!/bin/cat

[EDIT] as pointed out, I misspoke, and this is false.

~~~
0x0
I don't think bash is involved here?

~~~
Tyr42
I don't think so either. It's something in the implementation of `execv` I
think.

    
    
        #include <unistd.h>                                                             
                                                                                    
        int main() {                                                                    
            execl("cat-quine", "cat-quine", NULL);                                      
            return 0;                                                                   
        }
    

Is sufficient to have this work.

~~~
khc
An entire generation of programmer has grown up not knowing that this is
implemented in the kernel.

~~~
cyphar
I remember being surprised by this and then realising, "wait, of course it's
in the kernel -- that's so much nicer from a usability and sanity standpoint".

