Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Using your language of choice, golf a quine.

No cheating -- that means that you can't just read the source file and print it. Also, in many languages, an empty file is also a quine: that isn't considered a legit quine either.

Points for:

  • Smallest code (in characters)
  • Most obfuscated/obscure solution
  • Using esoteric/obscure languages
  • Successfully using languages that are difficult to golf in

Also, sorry about the title. I was just going to make it quine but it has to be at least 15 characters.

Per-language scoreboard of true quines1

1 Answers were omitted which involved direct or indirect reading of the source code, which depend on a REPL environment and just print what was entered as well as error quines.

share|improve this question
1  
Do you not mean, "Golf you a quine for greater good!"? And, yes, I'm a 'Grammar Nazi'. –  Mateen Ulhaq May 3 '11 at 2:49
14  
@muntoo it's a play on "Learn you a Haskell for Great Good". –  Rafe Kettler May 3 '11 at 2:52
3  
what about random text generator quines (a probability that the program will print itself) –  Ming-Tang May 14 '11 at 0:01
    
@muntoo Evidently a British English 'Grammar Nazi', judging by the positioning of your quotes and punctuation. –  daviewales Jun 18 '14 at 9:13

73 Answers 73

Fine, here's a 1-character quine in GolfScript:

1
share|improve this answer
10  
Or PowerShell, or PHP :-) –  Joey Jan 28 '11 at 9:54
3  
You didn't go back in time and give the inventor the idea to invent GolfScript, did you? –  Mateen Ulhaq May 3 '11 at 2:38
48  
Technically, 1 is not a quine in GolfScript: it outputs 1\n, where \n denotes a newline. However, the two-char program 1\n is a quine. –  Ilmari Karonen Feb 3 '12 at 8:27
6  
The one-char program \n probably also is? –  Mauris Aug 23 '13 at 14:51
    
No, Ilmari, even that is not a quine. A quine is not just a program which prints its own source, it has to have a specific structure to be a quine. –  Pseudonym Feb 12 '14 at 5:59

MySQL, 167 characters

SELECT REPLACE(@v:='SELECT REPLACE(@v:=\'2\',1+1,REPLACE(REPLACE(@v,\'\\\\\',\'\\\\\\\\\'),\'\\\'\',\'\\\\\\\'\'));',1+1,REPLACE(REPLACE(@v,'\\','\\\\'),'\'','\\\''));

That's right. :-)

I really did write this one myself. It was originally posted at my site.

share|improve this answer

HQ9+ (1 character)

Not quite as short as the 0 char one, but here it is anyway:

Q
share|improve this answer
7  
This one is legit... –  st0le Feb 6 '11 at 11:51
    
What's great about this one is unlike others (like the 1 in golfscript or php) is that you can have any arbitrary HQ9+ program (e.g., H+9+H9) and the quine of that program is always 1 more character than the program itself (in this case H+9+H9Q). –  Casey Apr 23 '11 at 21:17
8  
@Casey that's not entirely true: you can never have an HQ9+ Quine that doesn't involve exactly 1 Q and some sequence of +s (it is a stretch to say that the second program you have there "is a quine of" the first - they are different, and the "Quine" you suggested outputs too much - the lyrics to 99 bottles occur twice in the programs output, but not in the source) –  tobyodavies Apr 24 '11 at 16:31

PostScript, 20 chars

Short and legit. 20 chars including trailing newline.

(dup == =)
dup == =
share|improve this answer

Python 2 (29)

_='_=%r;print _%%_';print _%_

from en.literateprograms.org

share|improve this answer
    
+1, you beat my similar solution so I deleted it. It should be noted that this only works in Python 2. –  nyuszika7h Apr 28 '14 at 12:32

Brainf*ck (755 characters)

This is based off of a technique developed by Erik Bosman (ejbosman at cs.vu.nl). Note that the "ESultanik's Quine!" text is actually necessary for it to be a quine!

->++>+++>+>+>++>>+>+>+++>>+>+>++>+++>+++>+>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+>++>>>+++>>>>>+++>+>>>>>>>>>>>>>>>>>>>>>>+++>>>>>>>++>+++>+++>+>>+++>>>+++>+>+++>+>++>+++>>>+>+>+>+>++>+++>+>+>>+++>>>>>>>+>+>>>+>+>++>+++>+++>+>>+++>+++>+>+++>+>++>+++>++>>+>+>++>+++>+>+>>+++>>>+++>+>>>++>+++>+++>+>>+++>>>+++>+>+++>+>>+++>>+++>>>+++++++++++++++>+++++++++++++>++++++>+++++++++++++++>++++++++++>+++>+++>++++>++++++++++++++>+++>++++++++++>++++>++++++>++>+++++>+++++++++++++++>++++++++>++++>++++++++++++>+++++++++++++++>>++++>++++++++++++++>+++>+++>++++>++++++>+++>+++++++++>++++>+>++++>++++++++++>++++>++++++++>++>++++++++++>+>+++++++++++++++>+++++++++++++
ESultanik's Quine!
+[[>>+[>]+>+[<]<-]>>[>]<+<+++[<]<<+]>>+[>]+++[++++++++++>++[-<++++++++++++++++>]<.<-<]
share|improve this answer
5  
That's a clever way to do it. –  Peter Olson Jun 22 '11 at 16:48
3  
How does it work? –  proud haskeller Sep 13 '14 at 16:21

PHP, 32

Probably not an optimal solution, but it makes me smile.

HAHA THIS IS TECHNICALLY A QUINE

Give it a shot, works like a charm.

share|improve this answer
1  
+1 just because I laughed. :P –  user1354557 Dec 11 '13 at 23:33
1  
+1 for "give it a shot" :-DD –  Tomas Feb 1 '14 at 14:51
    
Also works with HTML, though that's actually a markup language. –  nyuszika7h Apr 26 '14 at 15:31
    
Give him his 32 points :) –  YoYoYonnY Feb 26 at 21:35

If we're going for short quines:

0 characters.

share|improve this answer
12  
Points for originality, but I'm adding this to the list of "ways to cheat". –  Rafe Kettler Jan 28 '11 at 0:39
25  
@Rafe: it's not very original. This was one of the winners of the 1994 International Obfuscated C Code Contest: the smallest self reproducing program: de.ioccc.org/years-spoiler.html#1994_smr –  R. Martinho Fernandes Jan 28 '11 at 2:18
9  
Can we avoid up-voting silly answers? –  marcog Jan 28 '11 at 11:26
18  
@marcog what is code golf but answering silly questions with rediculous (or rediculously complex) answers ;) –  tobyodavies Jan 28 '11 at 12:01
11  
@toby It's called common sense. If we don't apply it, this site will devolve into a rather stupid one. Up-vote the clever, but still short questions not the ones that required half a brain cell to come up with. –  marcog Jan 28 '11 at 12:09

Unix script (11 chars)

I am not exactly sure if this disqualifies as You can't just read the source file and print it, but it certainly is a nice one. Taken from http://research.swtch.com/zip.

#!/bin/cat
share|improve this answer
3  
+1 well technically "you" don't read the source, cat does –  Tim Seguine Feb 1 '14 at 15:24
    
You don't count the hashbang in code golf score, as technically it's a cat script. –  nyuszika7h Apr 26 '14 at 15:30
    
That raises the question though, is it 0 characters then? –  nyuszika7h Apr 27 '14 at 17:51

java, 530 bytes:

A java-Solution with an original approach:

import java.math.*;class a{public static void main(String[]a){BigInteger b=new BigInteger("90ygts9hiey66o0uh2kqadro71r14x0ucr5v33k1pe27jqk7mywnd5m54uypfrnt6r8aks1g5e080mua80mgw3bybkp904cxfcf4whcz9ckkecz8kr3huuui5gbr27vpsw9vc0m36tadcg7uxsl8p9hfnphqgksttq1wlolm2c3he9fdd25v0gsqfcx9vl4002dil6a00bh7kqn0301cvq3ghdu7fhwf231r43aes2a6018svioyy0lz1gpm3ma5yrspbh2j85dhwdn5sem4d9nyswvx4wmx25ulwnd3drwatvbn6a4jb000gbh8e2lshp",36);int i=0;for(byte c:b.toByteArray()){++i;if(i==92)System.out.print(b.toString(36));System.out.print((char)c);}}}

in readable form:

import java.math.*;
class a
{
    public static void main (String [] a)
    {
        BigInteger b=new BigInteger ("90ygts9hiey66o0uh2kqadro71r14x0ucr5v33k1pe27jqk7mywnd5m54uypfrnt6r8aks1g5e080mua80mgw3bybkp904cxfcf4whcz9ckkecz8kr3huuui5gbr27vpsw9vc0m36tadcg7uxsl8p9hfnphqgksttq1wlolm2c3he9fdd25v0gsqfcx9vl4002dil6a00bh7kqn0301cvq3ghdu7fhwf231r43aes2a6018svioyy0lz1gpm3ma5yrspbh2j85dhwdn5sem4d9nyswvx4wmx25ulwnd3drwatvbn6a4jb000gbh8e2lshp", 36); 
        int i=0; 
        for (byte c:b.toByteArray ())
        {
            ++i; 
            if (i==92) 
                System.out.print (b.toString (36)); 
            System.out.print ((char) c);
        }
    }
}
share|improve this answer

These are the two shortest Ruby quines from SO:

_="_=%p;puts _%%_";puts _%_

and

puts <<2*2,2
puts <<2*2,2
2

Don't ask me, how the second works...

share|improve this answer
7  
The second one uses heredoc, <<2 starts a string on the next line, and *2 repeats the string –  Ming-Tang Jan 28 '11 at 2:52

Cross-browser JavaScript (41 characters)

It works in the top 5 web browsers (IE >= 8, Mozilla Firefox, Google Chrome, Safari, Opera). Enter it into the developer's console in any one of those:

eval(I="'eval(I='+JSON.stringify(I)+')'")

It's not "cheating" — unlike Chris Jester-Young's single-byte quine, as it could easily be modified to use the alert() function (costing 14 characters):

alert(eval(I="'alert(eval(I='+JSON.stringify(I)+'))'"))

Or converted to a bookmarklet (costing 22 characters):

javascript:eval(I="'javascript:eval(I='+JSON.stringify(I)+')'")
share|improve this answer

Prelude, 5157 4514 2348 1761 1537 664 569 535 423 241 214 bytes

This is rather long... (okay, it's still long ... at least it's beating the shortest known Brainfuck quine now) but it's the first quine I discovered myself (my Lua and Julia submissions are really just translations of standard quine techniques into other languages) and as far as I'm aware no one has written a quine in Prelude so far, so I'm actually quite proud of this. :)

1(  #^    #^^   (#^+!
6 9+ 05+5+^#^+! #^ ^+!
   (((1- )#^##)^^+   )7-!)1433545514232323344949145353495314235494040404232323013334492349532333344953493435343584233475234501333449530423232349495323232323495323230494)

That large number of digits is just an encoding of the core code, which is why the quine is so long.

The digits encoding the quine have been generated with this CJam script.

This requires a standard-compliant interpreter, which prints characters (using the values as character codes). So if you're using the Python interpreter you'll need to set NUMERIC_OUTPUT = False.

Explanation

First, a few words about Prelude: each line in Prelude is a separate "voice" which manipulates its own stack. These stacks are initialised to an infinite number of 0s. The program is executed column by column, where all commands are executed "simultaneously" based on the previous stack states. Digits are pushed onto the stack individually, so 42 will push a 4, then a 2. There's no way to push larger numbers directly, you'll have to add them up. Values can be copied from adjacent stacks with v and ^. Brainfuck-style loops can be introduced with parentheses. See the link in the headline for more information.

Here is the basic idea of the quine: first we push loads of digits onto the stack which encode the core of the quine. Said core then takes those digits,decodes them to print itself and then prints the digits as they appear in the code (and the trailing )).

This is slightly complicated by the fact that I had to split the core over multiple lines. Originally I had the encoding at the start, but then needed to pad the other lines with the same number of spaces. This is why the initial scores were all so large. Now I've put the encoding at the end, but this means that I first need to skip the core, then push the digits, and jump back to the start and do the printing.

The Encoding

If I avoid using v in the core, all characters that can appear in a Prelude program have character code less than 100, and greater than 9 (the newline is 10). This means I can encode each character with exactly two decimal digits, where I can also be sure that the first digit is never 0. This is exactly the encoding I'm using. However, since we're working with a stack, the digits are pushed in reverse. So if you look at the end of the encoding:

...5323230494

Reverse and split into pairs, and then look up the character codes:

49 40 32 32 35
 1  (        #

where 32 is corresponds to spaces. The core simply combines these two digits into one number again, and then prints them.

The Core

So let's look at how these numbers are actually processed. First, it's important to note that matching parentheses don't have to be on the same line in Prelude. There can only be one parenthesis per column, so there is no ambiguity in which parentheses belong together. In particular, the vertical position of the closing parenthesis is always irrelevant - the stack which is checked to determine whether the loop terminates will always be the one where the matching ( appeared.

We want to run the code exactly twice - the first time, we skip the core and push all the numbers at the end, the second time we run the core. In fact, after we've run the core, we'll push all those numbers again, but since the loop terminates afterwards, this is irrelevant. This gives the following skeleton:

1(  #
6 9+
   (                     )14... encoding ...94)

Ignore the middle voice for now. First, we push a 1 onto the first voice - if we don't do this, we'd never enter the loop. Then we enter the main loop. Now, the third voice contains another loop. On the first pass, this loop will be skipped because the third stack is empty/contains only 0s. So we jump straight to the encoding and push all those digits onto the stack. The 1 we pushed onto the first stack is still there, so the loop repeats.

This time, there is a 4 on the third stack, so we do enter loop on the third voice. As soon as we're inside the loop, we discard the 1 on the first voice. The loop on the third voice is designed such that the stack is empty again at the end, so it only runs once. Then we push all the digits on the third stack again, but now the 1 on the first stack has been discarded, so the main loop ends and the program terminates.

Now regarding that 6 9+. That's just a golfing trick to put a number on the second voice which I'll need later in core. We push a 6 and in the main loop add a 9 to it. However, before we enter the core loop, this 9+ will be encountered twice, so we'll actually have 24 on the second stack by then.

Next, let's look at the first loop in the actual core:

 ^    #^^
 05+5+^#^+!
((1- )#^##)

This loop continues while the stack of digits hasn't been emptied. It processes two digits at a time, which is why the 0s in the encoding (which are all in second digit of each pair) will not terminate this loop. The purpose of this loop is to decode the encoding, print the character, and at the same time shift the stack of digits to the first voice. This latter part is a bit, tricky: while I want to reverse the order of the digits (so I can print them in the right order), I don't want to reverse the order of each pair (because then those trailing 0s could terminate the second loop).

So first, I copy the most significant digit (MSD) to the first voice, and push a 0 onto the second voice. Then the inner loop runs until the MSD is decremented down to 0. In each of those iterations, the 5+5+ adds 10 to the second voice. Now the tricky part:

#^^
^#^
#^#

This... uh... well, basically it copies the least significant digit (LSD) onto the first voice, while moving the MSD back through the other voices until its on top of the LSD. The last ^ on the second voice also copies the LSD onto that stack. Lastly, +! adds the LSD to ten times to MSD and prints the character, while the additional # on the third voice gets rid of the LSD there.

At this point, we've printed all of the core, the first voice holds all the digits in reverse order (without reversing each pair), the second voice only holds the 24 from earlier and the third voice is empty. Now the second and third voice do this:

 #
^^+

This copies the 24 onto the third voice twice and adds them together to give 48. This is the character code of 0, which we'll use as an offset to print all the digits. The # just discards the 24 (which isn't even necessary, but it doesn't cost any bytes). Note that the + is actually inside the following loop. However, this doesn't pose a problem, since the stack is initialised to an infinite number of 0s, so applying + to a single number is essentially a no-op. Moving it inside the loop saves one column of code.

(#^+!
 ^ ^+!
     )

This is the second loop of the core, which now prints back all the digits. The only catch is the we now need to reverse the order of each pair before printing. So first we shift the MSD to the second voice. Then copy the 48 onto the first voice, add it to the LSD and print it. And we also copy the 48 from the first onto the second voice, add it to the MSD and then print that.

At this point, the first two voices are empty, and the third voice still holds a 48. All that's left to do is to print the trailing ) which, conveniently, is within reach of 48. We simply subtract 7 off, to get 41 and print that. This empties the third voice, so we cleanly exit the core and are done.

share|improve this answer

Javascript (36 char)

(function a(){alert("("+a+")()")})()

This is, AFAICT, the shortest javascript quine posted so far.

share|improve this answer
    
That... is impressive. You should explain how it works for me 8-| –  TehShrike Sep 27 '11 at 19:18
3  
@TehShrike Hint: you can view the contents a function by coercing it to a string. For example, if you have a function a, you can access its contents by calling a.toString. –  Peter Olson Sep 27 '11 at 19:22
6  
To be pedantic, though, this is only a quine if your JavaScript implementation stringifies the function a exactly the same way as it's been written above. However, the output of this code is likely to be a quine on any JavaScript implementation. –  Ilmari Karonen Feb 3 '12 at 18:55
    
@IlmariKaronen not exactly. If an other implementation chooses to add a space between the arguments and the body, the output is not a quine in an implementation that doesn't add a space between the arguments and body. –  Johannes Kuhn Dec 21 '13 at 20:59
    
@JohannesKuhn: I meant that, for any given JavaScript implementation, the output of running the above code on that implementation is likely to be a quine on that implementation. Sorry for the imprecise language. –  Ilmari Karonen Dec 21 '13 at 21:22

Fob (135)

If Fob, a language of my own creation some time ago, I present a rather interesting 135 byte quine:

$$#<&$::#<&$:#<&#<&$:#<=#<&$&//%<//<.&%<<%.%<&>/////%<<%.<&.%<.%/////<&.%<<&/.%%<&>%</%<////<&.%<<%/<&.%%<&>/%//<&.%<</&.%%%<&>>/>>#<=
share|improve this answer

A Classic - Lisp - 78

((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x))))

A beautiful snippet, but give credit where credit is due.

share|improve this answer
3  
Actually this code returns itself instead of printing itself. Running it in an interpreter with read-eval-print loop will of course print the returned list, but the printing is not part of the code itself. The C equivalent of this would be a C code which outputs its executable instead of its source code. Which would certainly also be a quite interesting problem, although heavily system-dependent. –  celtschk Feb 3 '12 at 16:13

JavaScript 2688!!

Any awards for the most abysmal quine? My first go, it builds a little html interface which lets your run it again to build another interface which lets you... you get it, try it here

var a = [47, 42, 32, 90, 108, 97, 116, 97, 110, 39, 115, 32,
        115, 101, 108, 102, 32, 114, 117, 110, 110, 105, 110,
        103, 32, 113, 117, 105, 110, 101, 32, 42, 47, 10,
        118, 97, 114, 32, 116, 97, 59, 10, 10, 102, 117,
        110, 99, 116, 105, 111, 110, 32, 102, 119, 114, 105,
        116, 101, 40, 97, 114, 114, 41, 32, 123, 10, 9,
        118, 97, 114, 32, 107, 32, 61, 32, 39, 39, 59,
        10, 9, 118, 97, 114, 32, 98, 117, 116, 32, 61,
        32, 100, 111, 99, 117, 109, 101, 110, 116, 46, 99,
        114, 101, 97, 116, 101, 69, 108, 101, 109, 101, 110,
        116, 40, 39, 73, 78, 80, 85, 84, 39, 41, 59,
        10, 9, 118, 97, 114, 32, 98, 111, 32, 61, 32,
        100, 111, 99, 117, 109, 101, 110, 116, 46, 103, 101,
        116, 69, 108, 101, 109, 101, 110, 116, 115, 66, 121,
        84, 97, 103, 78, 97, 109, 101, 40, 39, 66, 79,
        68, 89, 39, 41, 91, 48, 93, 10, 9, 116, 97,
        46, 114, 111, 119, 115, 32, 61, 32, 39, 57, 39,
        59, 10, 9, 116, 97, 46, 99, 111, 108, 115, 32,
        61, 32, 39, 57, 48, 39, 59, 10, 9, 102, 111,
        114, 40, 118, 97, 114, 32, 122, 61, 45, 49, 59,
        32, 43, 43, 122, 60, 97, 114, 114, 46, 108, 101,
        110, 103, 116, 104, 59, 41, 32, 123, 10, 9, 9,
        107, 43, 61, 83, 116, 114, 105, 110, 103, 46, 102,
        114, 111, 109, 67, 104, 97, 114, 67, 111, 100, 101,
        40, 97, 114, 114, 91, 122, 93, 41, 59, 10, 9,
        125, 10, 9, 116, 97, 46, 118, 97, 108, 117, 101,
        32, 43, 61, 32, 107, 59, 10, 9, 98, 111, 46,
        97, 112, 112, 101, 110, 100, 67, 104, 105, 108, 100,
        40, 116, 97, 41, 59, 10, 9, 98, 117, 116, 46,
        116, 121, 112, 101, 32, 61, 32, 39, 98, 117, 116,
        116, 111, 110, 39, 59, 10, 9, 98, 117, 116, 46,
        118, 97, 108, 117, 101, 32, 61, 32, 39, 82, 117,
        110, 33, 39, 10, 9, 98, 117, 116, 46, 111, 110,
        99, 108, 105, 99, 107, 32, 61, 32, 119, 105, 110,
        100, 111, 119, 46, 111, 110, 108, 111, 97, 100, 59,
        10, 9, 98, 111, 46, 97, 112, 112, 101, 110, 100,
        67, 104, 105, 108, 100, 40, 98, 117, 116, 41, 59,
        10, 125, 10, 10, 119, 105, 110, 100, 111, 119, 46,
        111, 110, 108, 111, 97, 100, 32, 61, 32, 102, 117,
        110, 99, 116, 105, 111, 110, 40, 41, 32, 123, 10,
        9, 116, 97, 32, 61, 32, 100, 111, 99, 117, 109,
        101, 110, 116, 46, 99, 114, 101, 97, 116, 101, 69,
        108, 101, 109, 101, 110, 116, 40, 39, 84, 69, 88,
        84, 65, 82, 69, 65, 39, 41, 59, 10, 9, 116,
        97, 46, 118, 97, 108, 117, 101, 32, 43, 61, 32,
        39, 118, 97, 114, 32, 97, 32, 61, 32, 91, 39,
        59, 10, 9, 102, 111, 114, 40, 118, 97, 114, 32,
        122, 32, 61, 32, 48, 59, 32, 122, 60, 97, 46,
        108, 101, 110, 103, 116, 104, 59, 32, 122, 43, 43,
        41, 32, 123, 10, 9, 9, 116, 97, 46, 118, 97,
        108, 117, 101, 32, 43, 61, 32, 97, 91, 122, 93,
        43, 40, 40, 33, 122, 124, 124, 40, 122, 37, 49,
        49, 41, 41, 63, 39, 44, 32, 39, 58, 39, 44,
        32, 92, 110, 92, 116, 92, 116, 39, 41, 59, 10,
        9, 125, 10, 9, 116, 97, 46, 118, 97, 108, 117,
        101, 32, 43, 61, 32, 39, 93, 59, 92, 110, 92,
        110, 39, 59, 10, 9, 102, 119, 114, 105, 116, 101,
        40, 97, 41, 59, 10, 125, 10, 10];

/* Zlatan's self running quine */
var ta;

function fwrite(arr) {
    var k = "";
    var but = document.createElement("INPUT");
    var bo = document.getElementsByTagName("BODY")[0]
    ta.rows = "9";
    ta.cols = "90";
    for(var z=-1; ++z<arr.length;) {
        k+=String.fromCharCode(arr[z]);
    }
    ta.value += k;
    bo.appendChild(ta);
    but.type = "button";
    but.value = "Run!"
    but.onclick = window.onload;
    bo.appendChild(but);
}

window.onload = function() {
    ta = document.createElement("TEXTAREA");
    ta.value += "var a = [";
    for(var z = 0; z<a.length; z++) {
        ta.value += a[z]+((!z||(z%11))?", ":", \n\t\t");
    }
    ta.value += "];\n\n";
    fwrite(a);
}
share|improve this answer

><> (Fish) - 8 chars

Prints itself but throws an error

"r0:g>o<

13 For no error (old Fish)

"r0:g!;>?o?|;

15 if you think g is cheating

"r1b3*+!;>?o?|;
share|improve this answer
    
Last two ones don't work for me. They output rg>? and r3!?|, respectively. They seem to skip two characters every time... –  tomsmeding Apr 22 '13 at 5:55
    
@tomsmeding I think the interpreter changed some point after this answer, hence the (old fish) in parentheses. Though I honestly can't remember it was 2 years ago. I know they worked when i posted my answer. –  cthom06 Apr 22 '13 at 13:19

Haskell (50 characters)

main=putStr$q++show q;q="main=putStr$q++show q;q="
share|improve this answer

dc - 16 characters

[91PP6120568P]dx
share|improve this answer
    
There's this for 10: 6581840dnP –  Maria Tidal Tug Feb 26 at 22:22

JavaScript:

<script>alert(document.querySelector("script").outerHTML)</script>

It technically doesn't read its own file.

I think this is shorter and more “obfuscated”, though:

(_=$=>alert('(_='+_+')()'))()
share|improve this answer
5  
It reads its own source, though. –  Joey May 12 '11 at 21:15
    
` Uncaught SyntaxError: Unexpected token =>` in Chrome –  Nakilon Jan 14 at 9:36
    
@Nakilon: Use Firefox. –  minitech Jan 14 at 16:17

JavaScript, 31 characters

function f(){alert(f+"f()")}f()

Is this seriously the shortest JavaScript quine here?

share|improve this answer
    
75, without recursion: !function (x){alert('!'+x+'('+x+')')}(function (x){alert('!'+x+'('+x+')')}) –  sdleihssirhc Oct 27 '14 at 5:12

Perl, 30 28 chars

printf+(q(printf+(q(%s))x2))x2

I first posted this one years ago to the Fun With Perl mailing list, and I've been quite fond of it ever since.

You can save two characters if you use qw instead of q:

printf+qw(printf+qw(%s)x2)x2
share|improve this answer
1  
This is the shortest Perl quine I'm aware of (which doesn't read $0, that is). –  primo Jun 20 '13 at 12:43

HTML + CSS 118 78 77 75 characters

<style>*{display:inline;font-family:monospace}style:before{content:'<style>

This language isn't good for quining, but it works. Invalid HTML and CSS, but it doesn't really matter.

share|improve this answer
    
} isn't required –  xem Dec 21 '13 at 19:02
    
@xem: Removed '}. This abuses CSS error handling rules, but considering it's code golf, it's fine. –  xfix Dec 21 '13 at 19:13

Python 3, 54

I have never seen this one before, so here's my fair creation. It is longer than the classical one but a true one-liner without ;

print(str.format(*['print(str.format(*[{!r}]*2))']*2))
share|improve this answer
1  
btw, this is 2 bytes shorter in Python 2, where you don't need the parentheses after print. –  flornquake Sep 15 '14 at 9:40

Java 6 - 138 110 106

Since the question says "golf you a quine", I took Steve P's quine and golfed it:

enum Q{X;{String s="enum Q{X;{String s=%c%s%1$c;System.out.printf(s,34,s);}}";System.out.printf(s,34,s);}}

With credits to Trixie Wolf and Volune.
Note: you need to ignore stderr (e.g. 2>/dev/null)

For great good (and justice)!

share|improve this answer
    
I can't get this to work. Did you actually try to compile it? I think you need a System.exit() gimmick or it will fail to run properly. I'll add an answer here with my implementation later if I don't hear back from you soon. –  Trixie Wolf Aug 16 '14 at 4:00
    
Actually: given the "ignore stderr" comment obv. you did get it to work. I'm very curious how, though. –  Trixie Wolf Aug 16 '14 at 4:19
    
@TrixieWolf It works fine here, there is absolutely no compile error. Did you think I would post it without trying it first? :p Anyway, you can only run it with java 6 (or 5), newer versions check for the main method first. –  aditsu Aug 16 '14 at 7:52
    
I'd like to suggest this improvement: enum Q{X;{String s="enum Q{X;{String s=%c%s%1$c;System.out.printf(s,34,s);}}";System.out.printf(s,34,s);}} –  Volune Aug 17 '14 at 11:47
    
@aditsu Ah, that makes perfect sense. I'm busy today, but tomorrow I will check to see if mine still functions correctly (I tested it recently but I'll bet it was on J6). It might still work due to the exit() trick. –  Trixie Wolf Aug 17 '14 at 21:35

T-SQL 24

This statment reproduces itself in the EVENTINFO column of the output:

dbcc inputbuffer(@@spid)

Explanation:

  • dbcc inputbuffer() - Displays the last statement sent from the client with the specified process id to the current instance of Microsoft SQL Server
  • @@spid - Retrieves the current process id

tested with SQL Server 2008 R2 and 2012; probably working with other versions as well

Online demo: http://www.sqlfiddle.com/#!3/d41d8/2230

share|improve this answer

Mathematica, 68 characters

Print[#<>ToString[#,InputForm]]&@"Print[#<>ToString[#,InputForm]]&@"
share|improve this answer

PHP - 54 characters (no cheating)

<?printf($p='<?printf($p=%c%s%c,39,$p,39);',39,$p,39);

(finally even shorter)

share|improve this answer

Shell echo-sed quine:

echo sed -eh -es/[\\\(\\\\\\\\\\\)\\\&\\\|]/\\\\\\\\\\\&/g -es/^/echo\\ / -es/$/\\\|/ -eG|
sed -eh -es/[\(\\\\\)\&\|]/\\\\\&/g -es/^/echo\ / -es/$/\|/ -eG

I wanted to write a sed quine, but sed can only work on its input stream, not generate output spontaneously, so this is an echo-sed quine. This 154-character quine uses command-line sed, which automatically makes it hard to read, and uses three different sed commands, as well as two sequences of eleven backslashes in a row. This quine works in bash, ksh, and sh, but not csh or tcsh.

EDIT:

A blatant, and amusing, cheat: echo $BASH_COMMAND

Another, unreasonably silly, cheat: export PROMPT_COMMAND='echo $BASH_COMMAND';$PROMPT_COMMAND

share|improve this answer

protected by Peter Taylor Jun 20 '13 at 15:16

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.