# Golf you a quine for great good!

Using your language of choice, golf a quine.

A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.

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.

No error quines -- there is already a separate challenge for error quines.

Points for:

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

The following Stack Snippet can be used to get a quick view of the current score in each language, and thus to know which languages have existing answers and what sort of target you have to beat:

var QUESTION_ID=69;
var OVERRIDE_USER=98;

getAnswers();var SCORE_REG=(function(){var headerTag=String.raw h\d
var score=String.raw \-?\d+\.?\d*
var normalText=String.raw [^\n<>]*
var strikethrough=String.raw <s>${normalText}</s>|<strike>${normalText}</strike>|<del>${normalText}</del> var noDigitText=String.raw [^\n\d<>]* var htmlTag=String.raw <[^\n<>]+> return new RegExp(String.raw <${headerTag}>+String.raw \s*([^\n,]*[^\s,]),.*?+String.raw (${score})+String.raw (?=+String.raw ${noDigitText}+String.raw (?:(?:${strikethrough}|${htmlTag})${noDigitText})*+String.raw </${headerTag}>+String.raw ))})();var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}
body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)
if(languages.hasOwnProperty(lang))
langs.push(languages[lang]);langs.sort(function(a,b){if(a.uniq>b.uniq)return 1;if(a.uniq<b.uniq)return-1;return 0});for(var i=0;i<langs.length;++i)
{var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

• Do you not mean, "Golf you a quine for greater good!"? May 3 '11 at 2:49
• @muntoo it's a play on "Learn you a Haskell for Great Good". May 3 '11 at 2:52
• Did anybody notice that this is question 69? Oct 24 '20 at 22:47

# Perl 5, 21 bytes

say<<""x2
say<<""x2



Try it online!

• Woah, not sure how I missed this. This is awesome! Aug 19 at 8:13

# Pushy, 9 bytes

95 34
_"



Although writing functional programs in Pushy is sometimes difficult, the quine is relatively simple:

95   % Push 95 to the stack (ASCII code for _ )
34   % Push 34 to the stack (ASCII code for " )
_    % Print representation of the stack: 95 34
"    % Print stack converted to string: _"


Notice that, although Pushy ignores newlines, it is needed here because the default separator for printing is \n - and there needs to be a trailing newline, hence making it 9 bytes

Alternatively, an 11-byte solution that does not require a newline:

78 95 34N_"


Works similarly to the one above, but N sets the separator an empty string.

# 2DFuck, 963 853 bytes

!x^x>>x>x>x>>x>>>x>x>x>x>>x>>>x>>>x>x>x>>>>x>x>x>x>>>>x>x>>x>x>x>>>x>x>x>>x>>>>x>>>x>x>x>>x>x>>x>x>>x>>x>>>>>x>>>x>>x>x>x>>x>>>x>x>x>x>>x>>>x>>>x>x>x>>>x>x>x>x>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>x>>x>>>x>>>x>x>x>>>x>x>>x>x>x>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>>>x>x>x>>>x>x>x>>x>>>>x>x>>x>x>x>>x>>x>x>x>>x>>>x>x>x>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>>x>>>x>>>>>x>>>x>x>>x>x>>x>>>x>>>x>x>x>>x>x>>x>x>>x>>>>>x>x>x>x>>>x>x>x>x>>x>>>>x>x>x>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>>x>>>x>>>>>x>>>>x>x>x>>x>>>>x>x>x>>x>>>x>>x>x>x>>x>>>>>x>x>x>x>>x>>>>>x>>>>x>>>x>x>x>>>>x>x>x>x>>>x>x>>x>x>>x>>>x>x>>x>x>x>v[<r!x]..!.!....!.<^x[r[!.!....!...]v.r!.!.vr^!....!.>r^]![r.v<r^]


Try it online!

Just a plain binary encoding beats out the huffman style encoding.

### Explanation

!x^           Leave a marker at the start of the data string
x>>x>x>...    Push a binary string where 'x>' is 1 and '>' is 0
v[<r!x]       Move to the start of the data string
[             Loop over the data string
r[!.!....!...]     If the current bit is a 1, print 'x'
v.r!.!.vr^!....!.  Print a '^' if the current bit is the first bit, else '>'
>r^                Move to the next bit
]
![            Loop over the data string in reverse
r.v<r^        Print each bit of the data string
]


And my previous longer but more interesting answer below:

# 2DFuck, 963 bytes

!xv>x>x>>>x>x>>x>x>x>>x>x>>>>>x>>>x>>x>x>>>x>x>>>x>x>>>>x>x>x>>>x>x>>>x>>>>>x>>x>x>x>>x>x>x>>>x>>>x>>>x>x>x>x>>>x>>x>>x>x>x>x>x>>>x>x>>x>x>x>>>x>>>>>x>x>x>>x>x>x>>>x>>>x>>>x>x>>>x>>>x>>>x>x>x>x>>x>x>>>x>x>>x>x>x>>>x>>>>>x>x>x>>x>x>x>>>x>>>x>>x>x>x>>x>x>x>>x>>x>>>x>x>>>x>>>x>>>x>x>x>x>>x>x>>>>x>x>x>x>x>>>x>>>>>x>x>x>>x>>x>x>x>x>x>>>x>>>x>>x>x>x>>>x>>>>>x>x>x>>>x>>>x>>>x>x>>>x>>>x>>>x>x>x>x>>x>x>>>x>x>x>x>>>x>>>>>x>x>x>>x>>x>>x>x>x>x>x>>>x>>>x>x>x>>x>x>x>>x>x>x>>x>>>x>x>>>>>>>>x>>>x>x>>x>x>x>>>x>x>x>x>>x>x>>>>>x>>>>>x>x>x>>x>>x>>x>>x>>x>x>x>>x>>x>x>x>>>>x>>x>>x>x>x>>x>>x>>x>>x>x>x>>x>x>x>>x>>>x>x>>>>>>>>x>>>x>x>x>x>>>x>x>x>x>>>x>>>x>>>x>x>>x>x>x>>x>>x>x>x>>x>x>x>>x>>x>>x>x>x>>x>>x>>x>>x>x>x>>x>>x>>x>>x>x>x>>x>x>x>>x>x>x>>x>>x>>x>>x>x>x>>x>x>x>>x>>x>x>x>x>>>>>x>x>>>^x!..!.!....!.!.!....!....!...!.!..!.![<r!]![vr[!.!....!...]..!.....!.>^r!].![vr[!.!.!.<r!...!.>r!]r![<r[<r.>r..<r!..!.>r!.]r![<r[.!.!..<r.!.>r.!]r![<r[<r.!.>r.!]!...r![<r.!..>r].]]]<<^r.!]


Try it online!

I'm glad I got this below 1000 bytes. I think it still could be shorter though, maybe through encoding multiple or partial characters rather than one character per binary string. In particular encoding .. and/or !. as tokens but while that may make the data string shorter, the increases to the decoder may not be worth it. Here's the helper program to generate the program.

### Explanation

!xv                Leave a marker at the start of the data string
>x>x>>>x>...       Create the binary data string where 'x>' is 1 and '>' is 0
Here we encode the characters of the program as
'.' => 10
'!' => 11
'<' => 010
'r' => 011
'[' => 0010
']' => 0011
'>' => 00010
'^' => 00011
'v' => 00000
'x' => 00001

^x                 Leave a marker at the end of the data string
!..!.!....!.       Print '!xv'
!.!....!...
.!...!.!..!.

![<r!]             Move to the start of the data string
![                 Loop over each bit of the data string
vr[!.!....!...]    Print an 'x' if the bit is 1
..!.....!.         Print a '>'
>^r!]

.!                 Print a zero bit for use in the first character
[                  Loop over the data string in reverse
vr[                If the current bit is a 1, print
!.!.!.<r!...!.>r!  '.' if the next bit is a 0, otherwise '!'
]
r![                Otherwise
<r[                If the next bit is a 1, print
<r.>r..<r!..!.>r!. '<' if the next bit is a 0, otherwise 'r'
]
r![                Otherwise
<r[                If the next bit is a 1, print
.!.!..<r.!.>r.!    '[' if the next bit is a 0, otherwise ']'
]
r![                Otherwise
<r[<r.!.>r.!]!...  Print '>','^','v' or 'x' based on the next two bits
r![<r.!..>r].
]
]
]
<<^r            Move to the next bit in the data string
.!              And print a zero bit for the next character
]


# R, 44 41 bytes

crossed out 44 is still regular 44 ;(

-3 bytes thanks to Dominic van Essen

Works on R Version 3.6.2 and greater (thanks to Giuseppe for figuring out the versions).

+ <-
function(x)cat(dump("+",""),1)
+ 1


Try it online!

This is 3 6 bytes shorter than the previous shortest quine by JAD.

Note that TIO currently uses R version 3.5.2, and thus adds some spaces and newlines to the output, but my local install of R version 3.6.3 does not, and gives the exact correct output.

• IIRC TIO calls R by using Rscript as seen here, not sure if that's the difference. Aug 4 '20 at 15:48
• Nice! And you can even shed 2 more bytes to 42 bytes (also works on R installation but not properly on TIO or RScript). Aug 4 '20 at 17:51
• @DominicvanEssen Neat, thanks! Aug 4 '20 at 21:46
• Some digging in R news shows that since TIO's R install (3.5.2), there's been a change to dump as of version 3.6.2: see this edition of R News. I have 3.6.1 on my machine and it prints the function() and its body on separate lines. Aug 4 '20 at 22:03
• @Giuseppe Thanks for working that out! Aug 5 '20 at 5:39

# TypeScript, 209 197 bytes

type Q<X extends string[]=['','$',Q<['${X[0]}','${X[1]}','${X[2]}']>]>=type Q<X extends string[]=['${X[0]}','${X[1]}',Q<['${X[1]}{X[0]}','${X[1]}{X[1]}','${X[1]}{X[2]}']>]>=${X[0]}${X[2]}${X[0]}


Try it online!

# Jq -rn, 18 bytes

"|@json+."|@json+.

"|@json+."         # the string
|        # through the filter
@json   # json encoded
+  # concatenated with
. # itself


# Vyxal, 10 8 bytes

:qp:qp


Try it Online!

-2 thanks to a stone arachnid

## Explained

:qp:qp
:qp    # the string ":qp"
:q  # surrounded in backticks
p # and prepended to itself

• 8 bytes: Try it Online! May 26 at 3:43
• Does this count?
– user
May 30 at 16:13
• @user no, it doesn't May 30 at 22:30

# Knight, 22 bytes

E=s"O+'E=s'++A34sA34"



Includes trailing newline. Try it online!

Finally figured out how to do an EVAL quine, after definitely not adapting the Ruby quine example on Wikipedia.

It is pretty standard. Create a string containing code to print itself, and eval it. A34 is used to generate a double quote.

I am keeping my original idea here as well.

# Knight, no eval, 56 45 bytes

O S S=p"O S S=p7F=qA34 7F+q p"7F=qA34 7F+q p



Try it online!

Updated for the new ASCII function.

Includes trailing newline.

Removing the quine string and expanding the keywords/variable names, this is basically the printf quine:

OUTPUT
: SUBSTITUTE(
# insert closing quote for quine_str
: SUBSTITUTE(
: (= quine_str "...")
: 7
: 0 # we use FALSE to save a space
: (= quote ASCII(34))
)
# insert opening quote + quine string
: 7
: 0
: (+ quote quine_str)
: )


## Unix Executable, 10 bytes

#!/bin/cat


When run with something such as ./file, it will actually call /bin/cat ./file (because of the shebang), which prints the file's contents.

main=putStrLn$"s="++show s++s  ## Bash, 67/51 chars f () { printf "%s\n${!1} $1" "$(local -f ${!1})" } f FUNCNAME  And 51 chars: trap -- 'printf "%s\n:" "$(trap -p DEBUG)"' DEBUG
:


# Zozotez Lisp: 73

((\ (x) (c x (c (c (" ") (c x))))) (" (\ (x) (c x (c (c (" ") (c x)))))))


This requires one of the REPLs. For one bootstrap expression quine I need it to print: 81

((\ (x) (p (c x (c (c (" ") (c x)))))) (" (\ (x) (p (c x (c (c (" ") (c x))))))))


# Extended BrainFuck: 68

This uses mostly Brainfuck code except for the store string procedure.

>~"~!<<.[<]>.>+.-<[.>]<[<]>>+.->[.>]"<<.[<]>.>+.-<[.>]<[<]>>+.->[.>]


A 94 byte version that uses more EBF features:

{c|"{q$q.$p(-)}:q:p$q 34+$p|'{c|'&q&c&q|'}'(-)&c"}{q$q.$p(-)}:q:p$q 34+$p|'{c|'&q&c&q|'}'(-)&c


# Node.js REPL (22)

console.log(RegExp.$1)  • ...this abuses the fact that (I guess) the Node REPL internally executes a regex on the received line? I want to upvote because of the cleverness, but on the other hand I don't want to since it relies on extracting its own source code as a string... Aug 22 '14 at 15:38 • You need to specify the Node version this works in, because running in 7.5.0, this prints a single newline. Jul 16 '17 at 5:21 MATLAB, 95 characters There might be shorter ones, but I'm happy that it works at all. function d=g();d='gvodujpo!e>h)*<e>(<e>\e)2;29*.2-e-e)29;foe*.2^<';d=[d(1:18)-1,d,d(18:end)-1];  Cleaner approach, exact same length: function d=g();d='function d=g();d=[d(1:17),39,d,39,d(15:end)];';d=[d(1:17),39,d,39,d(15:end)];  ## Burlesque - 1 byte Technically, this is a quine: 1  Technically, this is also a quine: {1 2 3}  Pretty much any literal is a quine. This is also a quine which doesn't use a simple literal: ,#Q2 SH ~- ",#Q" \/ .+ sh  The comma is not necessary if you launch burlesque in no-stdin mode. # AppleScript, 2 Bytes 1  It's a little cheaty, but it is following the restrictions of a quine. If we don't count the trailing newline, then this solution becomes 1 byte - 1. Whenever AppleScript has a final executed line of code, it prints the result of the last operation (whatever it is) to the command line. text  Any class name has the same effect. # DUP, 51 bytes 0"0.[34,0[$;$][,1+]#]$!%%!"0.[34,0[$;$][,1+]#]$!%%!  Try it here. Well, DUP quines are possible, just really, really, long. I'll have to golf some more. # Python 2 and 3 - 32 bytes s='s=%r;print(s%%s)';print(s%s)  # reticular, 9 bytes "'34'coo;  This captures the string '34'coo;, then wraps around. After that, the number 34 is pushed the stack, converted to a character with c, finally being outputted with o. The next o outputs the captured string, and ; terminates the program. Other quines: "'34'c~O; "6@P2*c~O;  # Julia, 36 bytes (~=:@printf "(~=:%s)|>eval" ~)|>eval  Try it online! ### Background Unlike many other languages, Julia's eval doesn't work as expected with a string; for example, eval("print(42)") just returns the string print(42). To actually executed print(42) with eval;, we have to pass an Expr to eval. This can be done by invoking parse on a string (e.g., eval(parse("print(42)"))) or by passing an Expr literal to eval (e.g., eval(:(print(42))). Now, while : is a unary operator and :print works fine on its own, :print(42) does not, as it is parsed as (:print)(42), making all parentheses in :(print(42)) mandatory. However, if we use the macro @printf instead, the parsing rules change, and :@printf(42) works as intended. Also, macro calls also do not require parentheses, and :@printf 42 saves one byte over :(print(42)). ### How it works :@printf "(~=:%s)|>eval" ~ constructs the Expr that calls @printf with the specified format string and additional argument ~. Here, ~ is simply a variable reference; the name is arbitrary. ~=<Expr> saves the generated Expr in the variable ~, which will be accessible when the Expr is evaluated. Finally, (<assigment>)|>eval calls eval with the return value of the assignment, i.e., the Expr that was assigned to ~. # Jelly, 6 bytes There are two proper and payload capable "built-in" quines of 6-bytes: “ØV”ṘV - takes no input “ØV” - make the string “ØV” Ṙ - print and yield left (prints “ØV”, yields “ØV”) V - eval Jelly code (the code ØV yields the string “ṘV”) - implicit return of the string “ṘV”, so the final output is “ØV”ṘV  and “Øv”Ṙv - as above, except: v evals with an input, which in this case is empty; and Øv yields “Ṙv”  A payload may be placed directly after the leading open quote in either. ## Straw, 10 bytes (non-competing) (:%>>):%>>  • Link to language is dead. Also, why is this non-competing? Sep 18 '20 at 1:35 # PowerShell, 41 37 Bytes: function q{"function q{$function:q};q"};q

filter q{"filter q{$function:q};q"};q  Thanks to TimmyD for saving 4 bytes • What interpreter / compiler does this work in? In this interpreter, this submission doesn't work (it outputs function q{End: { "function q{$function:q};q" }};q). Oct 6 '16 at 18:00
• @Loovjo Most online PowerShell interpreters use an open-source PoSH that's roughly equivalent to PowerShell v0.5 and lacking many features. The above works fine in an actual install on Windows. Oct 7 '16 at 13:08
• At least in v4 on Windows 8.1, you can shave a few bytes using filter as follows -- filter q{"filter q{$function:q};q"};q for 37. Oct 7 '16 at 13:09 • $MyInvocation.MyCommand.ScriptBlock is 2 bytes shorter but almost definitely cheating? Feb 22 '17 at 22:38

# Cheddar, 56 bytes

Try this one online!

let q='let q=%s%s%s;print IO.sprintf(q,@"39,q,@"39)';print IO.sprintf(q,@"39,q,@"39)


See the explanation below, except mentally replace % with IO.sprintf.

Well darn. @ETHProductions came up with this solution before me..

This is the shortest I could come up with... Maybe some abuse of functional operators could help me.

let q='let q=%s;print q%@"39+q+@"39';print q%@"39+q+@"39


Try it online! You can guess what the output is.

This code can be divided into two parts: the string and the output. The string part:

let q='let q=%s;print q%@"39+q+@"39';


is simply a formatting template.

The output part:

;print q%@"39+q+@"39


formats the string. @"39 is char 39, or '.

# Java 8, 94 bytes

()->{String s="()->{String s=%c%s%1\$c;System.out.printf(s,34,s);}";System.out.printf(s,34,s);}


This is a lambda expression which prints its own source code to STDOUT. It uses a similar tactic to other Java quines here, but the lambda really helps cut down bytes.

If we wanted to be really cheeky and cut down two bytes, we could declare the lambda as x->, where x is an empty string, as according to meta, "taking no input" means you can assume empty input, and in function submissions input is given as a parameter.

## Python 3, 38 bytes

There are already a lot of Python quines, but as far as I can see this one hasn't been posted yet. Technically it is a statement that evaluates to a string representation of itself, but other submissions do similar things.

'.__repr__()[:-1]*2'.__repr__()[:-1]*2


This works in a similar way to many quines in 2D langauges with "edge-wrap", where "string mode" is entered, the whole program is pushed to the stack, then string mode is executed and the program runs, printing the string mode character (usually ") followed by the contents of the stack (i.e. the program's source) then exiting.

A breakdown of the statement is as follows:

'.__repr__()[:-1]*2'                   # A string containing the body of the program.
# .__repr__()[:-1]*2
.__repr__()        # The same string, but enclosed in single quote marks.
# '.__repr__()[:-1]*2'
[:-1]   # A splice that crops off the last character.
# '.__repr__()[:-1]*2
*2 # Repeat the string.
# '.__repr__()[:-1]*2'.__repr__()[:-1]*2


The reason I have used .__repr__() instead of repr(string) is because the quine relies on code following and not preceding the string. This is also why this is a statement and not a program; the print() function requires code before the string, which is not possible with this quine layout.

As you may have noticed, there's a much golfier statement that evaluates to this statement:

"'.__repr__()[:-1]*2"*2


But this isn't a quine, because it doesn't evaluate to itself.

• Unfortunately, I don't think this is valid, since it's an expression (and therefore a snippet) rather than a full program. Jul 30 '17 at 2:33

# D, 85 bytes

enum c=q{import std.stdio;void main(){write("enum c=q{"~c~"};mixin(c);");}};mixin(c);


>91>60>93>62>91>105>54>50>99>111>100>111>62>93>60>91>60>93>62>91>99>111>62>93[<]>[i62codo>]<[<]>[co>]


Try it online!

My first thought for writing a Threead quine was to store the entire data section as one large number, in order to get a good compression ratio. This doesn't work because a) % appears to be broken, and b) Threead doesn't support bignum arithmetic.

Instead, I wrote this solution, which works along the same lines as a brainfuck quine, storing the characters of the code section of the program as individual tape elements. Then we just have to scan the list once in order to print it as data, and again to print it as code.

Although Threead allows for three threads, and requires their use when performing binary operations, this style of quine uses only unary operations and thus there was no point in using more than one thread, so I just did everything inside the first.

## Explanation

>91>60>…>62>93            ASCII character codes of the rest of the program
[<]>                      Return the pointer to the start of the data
[                         While the current data cell is nonzero:
i62                        Place 62 (ASCII code of >) on a temporary tape cell
co                         Output it as a character (i.e. >)
d                          Delete the temporary tape cell
o                          Output the current data element as an integer
>]                        then continue the loop with the next data cell
<[<]>                     Return the pointer to the start of the data
[                         While the current data cell is nonzero:
co                         Output it as a character
>]                        then continue the loop with the next data cell

• Nice. Basically the same as mine but with >` at the beginning. I like it Jan 16 '17 at 23:01
• I hope you don't mind that I used that trick in my new version :) Jan 16 '17 at 23:23
• @Riley: That's OK, we're pretty much all cooperating to improve the quine at this point.
– user62131
Jan 16 '17 at 23:25