News for hackers. Stuff that matters.
Re:Shebang Quine by Exuma on Friday May 20, 2016 @11:01AM (#11738353)
Here's my favorite one ever...

http://aem1k.com/world/

Re:Shebang Quine by takeda on Friday May 20, 2016 @11:35AM (#11738662)
This one is also amazing.

https://github.com/mame/quine-relay

Re:Shebang Quine by djsumdog on Friday May 20, 2016 @12:17PM (#11739058)
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.
Re:Shebang Quine by B1FF_PSUVM on Sunday May 22, 2016 @05:11AM (#11747889)
Hmm ... 100 langs circuit, impressive ... thumbnail of code ... %&$#!!

https://github.com/mame/quine-relay/blob/master/thumbnail.pn...

(Attention to detail, they haz it.)

Re:Shebang Quine by knodi123 on Friday May 20, 2016 @11:05AM (#11738396)
wow, that's incredible. there's, like, 4 different techniques going on in there that I don't understand- and I would have said I understood javascript pretty well.
Re:Shebang Quine by glaberficken on Friday May 20, 2016 @11:51AM (#11738819)
Wow, can someone explain what is going on in here?
Re:Shebang Quine by saganus on Friday May 20, 2016 @12:12PM (#11739019)
This is extremely cool, however the source code does look a little different than the output (the output only has '#' chars, but the source has letters and other chars besides #). Does that still count as a quine? (don't really care though... this is very cool).

Any idea how this works? It's just to much for my mind to bear I think.

Re:Shebang Quine by stouset on Friday May 20, 2016 @12:16PM (#11739057)
I'm not sure what you're talking about. The output is the entire source with the central globe rotated by a user-selectable number of degrees, including 0. The output isn't just the globe in the center.

Edit: Ah, nvm. It looks like this is a Javascript port of a famous Ruby quine from 2010, the Qlobe[1].

[1]: http://d.hatena.ne.jp/ku-ma-me/20100905/p1

Re:Shebang Quine by saganus on Friday May 20, 2016 @02:58PM (#11740502)
Well, not really. I checked the source and you can see here:

https://jsfiddle.net/f17yr7qh/

For example, line 9 of the source:

~(32sin(o)/* , (W#####Xx######.P^ T % */sin(.5+y/7))\

You can see that there are more chars than just '#' inside the "globe", like '(W' and 'Xx' and '.P^'

I even changed those inside the globe and the effect is non obvious. Sometimes it stops working sometimes it works fine with other chars. No idea why.

Re:Shebang Quine by takeda on Friday May 20, 2016 @11:30AM (#11738623)
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.

Re:Shebang Quine by geon on Friday May 20, 2016 @11:41AM (#11738737)
Probably not.
Re:Shebang Quine by vortico on Friday May 20, 2016 @11:56PM (#11743134)
It could be called a "boring quine" whereas the better ones could be called "pure quines". I'd still call it a quine though.
Re:Shebang Quine by adrianratnapala on Friday May 20, 2016 @11:02AM (#11738362)
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.

Re:Shebang Quine by maze-le on Friday May 20, 2016 @11:54AM (#11738846)
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.
Re:Shebang Quine by profmonocle on Friday May 20, 2016 @01:08PM (#11739553)
My favorite pseudo-quine is just an empty file. Works in Bash, PHP, Node, Perl, Python, Ruby, and many others I'm sure.
Re:Shebang Quine by hkjgkjy on Friday May 20, 2016 @12:43PM (#11739321)
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?
Re:Shebang Quine by jannotti on Friday May 20, 2016 @01:08PM (#11739550)
That's not the issue, really. The issue is whether the program has access to itself as input. This one does, so it's not a quine. But it's cool.
Re:Shebang Quine by maze-le on Friday May 20, 2016 @12:59PM (#11739460)
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.
Re:Shebang Quine by khedoros on Friday May 20, 2016 @07:51PM (#11742435)
In that definition, it seems like DOS OSes never had programs.
Re:Shebang Quine by stormbrew on Friday May 20, 2016 @11:57AM (#11738880)
Yeah, I would call it a quine of the binary loader, basically.
Re:Shebang Quine by malisper on Friday May 20, 2016 @02:03PM (#11740057)
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...
Re:Shebang Quine by tehrei on Friday May 20, 2016 @11:21AM (#11738534)
A more philosophical shebang that I like is

   #!/bin/rm
Re:Shebang Quine by nmc on Friday May 20, 2016 @10:43AM (#11738169)
Very fun.

For those who are puzzled by the term "quine": http://meta.codegolf.stackexchange.com/questions/4877/what-c...

Re:Shebang Quine by cyphar on Friday May 20, 2016 @12:22PM (#11739120)
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).
Re:Shebang Quine by geon on Friday May 20, 2016 @10:39AM (#11738128)
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.

Re:Shebang Quine by philh on Friday May 20, 2016 @10:53AM (#11738254)
Ages ago I wrote a simple command that let you do shell scripting in the shebang line. http://reasonableapproximation.net/2010/07/27/silly-things-t...

I'm not sure I've used it since, but I still like the xgraph thing.

Re:Shebang Quine by jmiserez on Friday May 20, 2016 @01:27PM (#11739720)
Very interesting and a good read. I was not aware that you could pass an argument in the shebang line.
Re:Shebang Quine by hk__2 on Friday May 20, 2016 @04:05PM (#11741039)
It’s often used to have your shell scripts fail fast by using `#! /usr/bin/bash -e`; which you could also do by using `set -e` at the beginning of the script but that’s less elegant.
Re:Shebang Quine by kylek on Friday May 20, 2016 @03:54PM (#11740936)
This is pretty great little read, much appreciated
Re:Shebang Quine by gkya on Friday May 20, 2016 @11:37AM (#11738695)

  #!/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.
Re:Shebang Quine by cyphar on Friday May 20, 2016 @12:17PM (#11739066)
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).
Re:Shebang Quine by gkya on Friday May 20, 2016 @12:44PM (#11739323)
Ran it, works. I'm on freebsd 10.3 BTW.
Re:Shebang Quine by cyphar on Friday May 20, 2016 @01:22PM (#11739680)
On Linux you get ELOOP. How does that resolution even work on FreeBSD? Why does it resolve to $SHELL (the file resolves to itself).
Re:Shebang Quine by usmannk on Friday May 20, 2016 @01:51PM (#11739947)
Yeah, I don't see that working unless the shebang is changed to `#!/bin/sh`?
Re:Shebang Quine by gkya on Friday May 20, 2016 @02:10PM (#11740104)
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.

Re:Shebang Quine by cyphar on Friday May 20, 2016 @09:14PM (#11742681)
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)?
Re:Shebang Quine by gkya on Friday May 20, 2016 @09:48PM (#11742784)
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.

Re:Shebang Quine by cyphar on Saturday May 21, 2016 @11:28AM (#11744976)
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).
Re:Shebang Quine by djsumdog on Friday May 20, 2016 @02:07PM (#11740088)
Huh...yea, I never though about how the first argument to a program is always itself.
Re:Shebang Quine by Null-Set on Friday May 20, 2016 @12:51PM (#11739388)
rm, with self cleaning action!
Re:Shebang Quine by CobrastanJorji on Friday May 20, 2016 @02:18PM (#11740184)
Is this technically a quine? It uses its own file as an input. I think that may technically disqualify it from quineness.
Re:Shebang Quine by JoshTriplett on Friday May 20, 2016 @05:24PM (#11741647)
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
Re:Shebang Quine by emson on Friday May 20, 2016 @10:47AM (#11738210)
So if anyone is interested here are some "Elixir" quine attempts: http://elixirgolf.com/articles/elixir-quine-self-replicating...
Re:Shebang Quine by jessaustin on Friday May 20, 2016 @10:32AM (#11738065)
Haha treating cat as an interpreter just sends the script on stdin.
Re:Shebang Quine by tantalor on Friday May 20, 2016 @10:56AM (#11738297)
Actually, sends the name of the script as an argument to cat, not stdin.
Re:Shebang Quine by mijoharas on Friday May 20, 2016 @10:32AM (#11738066)
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.

Re:Shebang Quine by 0x0 on Friday May 20, 2016 @10:40AM (#11738136)
I don't think bash is involved here?
Re:Shebang Quine by Tyr42 on Friday May 20, 2016 @10:55AM (#11738295)
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.
Re:Shebang Quine by khc on Friday May 20, 2016 @11:36AM (#11738674)
An entire generation of programmer has grown up not knowing that this is implemented in the kernel.
Re:Shebang Quine by cyphar on Friday May 20, 2016 @12:19PM (#11739085)
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".
Re:Shebang Quine by cyphar on Friday May 20, 2016 @12:19PM (#11739092)
It's implemented in the kernel. Look at fs/binfmt_script.c in the Linux kernel source tree for an example.
Re:Shebang Quine by geon on Friday May 20, 2016 @12:57PM (#11739436)
Re:Shebang Quine by kovek on Friday May 20, 2016 @10:52AM (#11738248)
More precisely:

  - The shell finds opens up the file you want to execute

  - It gets the first line of the file

  - It figures out what program to execute

  - It calls fork()

  - In the new process, it executes the program (using exec()) and passes the file as the input to the program.
Re:Shebang Quine by Tyr42 on Friday May 20, 2016 @10:56AM (#11738304)
The shell is not involved in looking at the first line of the program, that's what happens in `exec`.