7 ms·
Shebang Quine
- jessaustin 10y agoHaha treating cat as an interpreter just sends the script on stdin.
- tantalor 10y agoActually, sends the name of the script as an argument to cat, not stdin.
- mijoharas 10y agoFirst 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 10y agoI don't think bash is involved here?
- kovek 10y agoMore 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.
- Tyr42 10y agoThe shell is not involved in looking at the first line of the program, that's what happens in `exec`.
- Tyr42 10y agoI 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 10y agoAn entire generation of programmer has grown up not knowing that this is implemented in the kernel.
- cyphar 10y agoI 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".
- cyphar 10y agoIt's implemented in the kernel. Look at fs/binfmt_script.c in the Linux kernel source tree for an example.
- geon 10y agoSomeone linked to it on reddit: https://www.reddit.com/r/programming/comments/4k7gvh/shebang_quine/d3csbiu https://www.reddit.com/r/programming/comments/4k7gvh/shebang...
- geon 10y agoI 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.
- philh 10y agoAges ago I wrote a simple command that let you do shell scripting in the shebang line. http://reasonableapproximation.net/2010/07/27/silly-things-to-do-with-shebang-lines.html 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.
- jmiserez 10y agoVery interesting and a good read. I was not aware that you could pass an argument in the shebang line.
- hk__2 10y agoIt’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.
- kylek 10y agoThis is pretty great little read, much appreciated
- gkya 10y ago#!/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 10y agoI'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).
- nmc 10y agoVery fun. For those who are puzzled by the term "quine": http://meta.codegolf.stackexchange.com/questions/4877/what-counts-as-a-proper-quine http://meta.codegolf.stackexchange.com/questions/4877/what-c...
- emson 10y agoSo if anyone is interested here are some "Elixir" quine attempts: http://elixirgolf.com/articles/elixir-quine-self-replicating-program/ http://elixirgolf.com/articles/elixir-quine-self-replicating...
- Exuma 10y agoHere's my favorite one ever... http://aem1k.com/world/ http://aem1k.com/world/
- knodi123 10y agowow, 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.
- takeda 10y agoThis one is also amazing. https://github.com/mame/quine-relay https://github.com/mame/quine-relay
- djsumdog 10y agoI 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.
- B1FF_PSUVM 10y agoHmm ... 100 langs circuit, impressive ... thumbnail of code ... %&$#!! https://github.com/mame/quine-relay/blob/master/thumbnail.png https://github.com/mame/quine-relay/blob/master/thumbnail.pn... (Attention to detail, they haz it.)
- glaberficken 10y agoWow, can someone explain what is going on in here?
- saganus 10y agoThis 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.
- adrianratnapala 10y agoHmm, 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 10y agoI 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 10y agoI'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 10y agoWell, 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 10y agoIn that definition, it seems like DOS OSes never had programs.
- jannotti 10y agoThat'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.
- profmonocle 10y agoMy favorite pseudo-quine is just an empty file. Works in Bash, PHP, Node, Perl, Python, Ruby, and many others I'm sure.
- tehrei 10y agoA more philosophical shebang that I like is #!/bin/rm
- takeda 10y agoIs 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.
- deleted 10y ago[deleted]
- cyphar 10y agoI 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).
- malisper 10y agoIf 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-programs/ http://malisper.me/2016/04/20/writing-self-referential-progr...
- CobrastanJorji 10y agoIs this technically a quine? It uses its own file as an input. I think that may technically disqualify it from quineness.
- JoshTriplett 10y agoHere'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