110
\$\begingroup\$

One of the most common standard tasks (especially when showcasing esoteric programming languages) is to implement a "cat program": read all of STDIN and print it to STDOUT. While this is named after the Unix shell utility cat it is of course much less powerful than the real thing, which is normally used to print (and concatenate) several files read from disc.

Task

You should write a full program which reads the contents of the standard input stream and writes them verbatim to the standard output stream. If and only if your language does not support standard input and/or output streams (as understood in most languages), you may instead take these terms to mean their closest equivalent in your language (e.g. JavaScript's prompt and alert). These are the only admissible forms of I/O, as any other interface would largely change the nature of the task and make answers much less comparable.

The output should contain exactly the input and nothing else. The only exception to this rule is constant output of your language's interpreter that cannot be suppressed, such as a greeting, ANSI color codes or indentation. This also applies to trailing newlines. If the input does not contain a trailing newline, the output shouldn't include one either! (The only exception being if your language absolutely always prints a trailing newline after execution.)

Output to the standard error stream is ignored, so long as the standard output stream contains the expected output. In particular, this means your program can terminate with an error upon hitting the end of the stream (EOF), provided that doesn't pollute the standard output stream. If you do this, I encourage you to add an error-free version to your answer as well (for reference).

As this is intended as a challenge within each language and not between languages, there are a few language specific rules:

  • If it is at all possible in your language to distinguish null bytes in the standard input stream from the EOF, your program must support null bytes like any other bytes (that is, they have to be written to the standard output stream as well).
  • If it is at all possible in your language to support an arbitrary infinite input stream (i.e. if you can start printing bytes to the output before you hit EOF in the input), your program has to work correctly in this case. As an example yes | tr -d \\n | ./my_cat should print an infinite stream of ys. It is up to you how often you print and flush the standard output stream, but it must be guaranteed to happen after a finite amount of time, regardless of the stream (this means, in particular, that you cannot wait for a specific character like a linefeed before printing).

Please add a note to your answer about the exact behaviour regarding null-bytes, infinite streams, and extraneous output.

Additional rules

  • This is not about finding the language with the shortest solution for this (there are some where the empty program does the trick) - this is about finding the shortest solution in every language. Therefore, no answer will be marked as accepted.

  • Submissions in most languages will be scored in bytes in an appropriate preexisting encoding, usually (but not necessarily) UTF-8.

    Some languages, like Folders, are a bit tricky to score. If in doubt, please ask on Meta.

  • Feel free to use a language (or language version) even if it's newer than this challenge. Languages specifically written to submit a 0-byte answer to this challenge are fair game but not particularly interesting.

    Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language.

    Also note that languages do have to fulfil our usual criteria for programming languages.

  • If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainfuck derivatives like Headsecks or Unary), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.

  • Unless they have been overruled earlier, all standard rules apply, including the http://meta.codegolf.stackexchange.com/q/1061.

As a side note, please don't downvote boring (but valid) answers in languages where there is not much to golf; these are still useful to this question as it tries to compile a catalogue as complete as possible. However, do primarily upvote answers in languages where the author actually had to put effort into golfing the code.

Catalogue

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution 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>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table><script>var QUESTION_ID = 62230; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 8478; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "//api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "//api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(42), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) 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); } }</script>

\$\endgroup\$
14
  • 78
    \$\begingroup\$ Bash, 3 bytes: cat \$\endgroup\$
    – TheDoctor
    Oct 31, 2015 at 19:02
  • 4
    \$\begingroup\$ @TheDoctor I guess this would fall into the "don't use a builtin which does exactly what is needed" rule. \$\endgroup\$ Oct 31, 2015 at 19:46
  • 14
    \$\begingroup\$ @PaŭloEbermann There is no such rule, and the corresponding standard loophole is no longer accepted. (In fact, there is already a sh answer using cat which also contains a shorter solution using dd.) \$\endgroup\$ Oct 31, 2015 at 20:25
  • 1
    \$\begingroup\$ If only it used standard methods of input and output: ///, 0 bytes. \$\endgroup\$
    – sporkl
    Mar 31, 2017 at 19:33
  • 1
    \$\begingroup\$ @SparklePony Except, you'd have to escape slashes and backslashes. \$\endgroup\$ Mar 31, 2017 at 20:35

328 Answers 328

1
3 4
5
6 7
11
2
\$\begingroup\$

I like frog, 11 bytes

like i
like

like i takes the input

like prints all cells that were written to at any time by the program.

\$\endgroup\$
2
\$\begingroup\$

Turing Machine Language, 7148 bytes

0 * * l 2 ;
1 _ _ l 2 ;
2 _ ( r 3 ;
3 _ _ r 4 ;
4 _ _ l 3) ;
4 * * r 2 ;
3) _ ) l 5 ;
3 * * r 3 ;
5 ( ( * 0 ;
5 * * l 5 ;
6 _ _ l 7 ;
7 _ ] r 8 ;
8 ) ) l 9 ;
8 * * r 8 ;
9 0 @ l c0 ;
9 1 @ l c1 ;
9 2 @ l c2 ;
9 3 @ l c3 ;
9 4 @ l c4 ;
9 5 @ l c5 ;
9 6 @ l c6 ;
9 7 @ l c7 ;
9 8 @ l c8 ;
9 9 @ l c9 ;
9 a @ l a ;
9 A @ l A ;
9 b @ l b ;
9 B @ l B ;
9 c @ l c ;
9 C @ l C ;
9 d @ l d ;
9 D @ l D ;
9 e @ l e ;
9 E @ l E ;
9 f @ l f ;
9 F @ l F ;
9 g @ l g ;
9 G @ l G ;
9 h @ l h ;
9 H @ l H ;
9 i @ l i ;
9 I @ l I ;
9 j @ l j ;
9 J @ l J ;
9 k @ l k ;
9 K @ l K ;
9 l @ l l ;
9 L @ l L ;
9 m @ l m ;
9 M @ l M ;
9 n @ l n ;
9 N @ l N ;
9 o @ l o ;
9 O @ l O ;
9 p @ l p ;
9 P @ l P ;
9 q @ l q ;
9 Q @ l Q ;
9 r @ l r ;
9 R @ l R ;
9 s @ l s ;
9 S @ l S ;
9 t @ l t ;
9 T @ l T ;
9 u @ l u ;
9 U @ l U ;
9 v @ l v ;
9 V @ l V ;
9 w @ l w ;
9 W @ l W ;
9 x @ l x ;
9 X @ l X ;
9 y @ l y ;
9 Y @ l Y ;
9 z @ l z ;
9 Z @ l Z ;
c0 ] ] l c0a ;
c0 * * l c0 ;
c0a _ 0 r @0 ;
c0a * * l c0a ;
@0 @ 0 l nC ;
@0 * * r @0 ;
c1 ] ] l c1a ;
c1 * * l c1 ;
c1a _ 1 r @1 ;
c1a * * l c1a ;
@1 @ 1 l nC ;
@1 * * r @1 ;
c2 ] ] l c2a ;
c2 * * l c2 ;
c2a _ 2 r @2 ;
c2a * * l c2a ;
@2 @ 2 l nC ;
@2 * * r @2 ;
c3 ] ] l c3a ;
c3 * * l c3 ;
c3a _ 3 r @3 ;
c3a * * l c3a ;
@3 @ 3 l nC ;
@3 * * r @3 ;
c4 ] ] l c4a ;
c4 * * l c4 ;
c4a _ 4 r @4 ;
c4a * * l c4a ;
@4 @ 4 l nC ;
@4 * * r @4 ;
c5 ] ] l c5a ;
c5 * * l c5 ;
c5a _ 5 r @5 ;
c5a * * l c5a ;
@5 @ 5 l nC ;
@5 * * r @5 ;
c6 ] ] l c6a ;
c6 * * l c6 ;
c6a _ 6 r @6 ;
c6a * * l c6a ;
@6 @ 6 l nC ;
@6 * * r @6 ;
c7 ] ] l c7a ;
c7 * * l c7 ;
c7a _ 7 r @7 ;
c7a * * l c7a ;
@7 @ 7 l nC ;
@7 * * r @7 ;
c8 ] ] l c8a ;
c8 * * l c8 ;
c8a _ 8 r @8 ;
c8a * * l c8a ;
@8 @ 8 l nC ;
@8 * * r @8 ;
c9 ] ] l c9a ;
c9 * * l c9 ;
c9a _ 9 r @9 ;
c9a * * l c9a ;
@9 @ 9 l nC ;
@9 * * r @9 ;
a ] ] l aa ;
a * * l a ;
aa _ a r @a ;
aa * * l aa ;
@a @ a l nC ;
@a * * r @a ;
A ] ] l Aa ;
A * * l A ;
Aa _ A r @A ;
Aa * * l Aa ;
@A @ A l nC ;
@A * * r @A ;
b ] ] l ba ;
b * * l b ;
ba _ b r @b ;
ba * * l ba ;
@b @ b l nC ;
@b * * r @b ;
B ] ] l Ba ;
B * * l B ;
Ba _ B r @B ;
Ba * * l Ba ;
@B @ B l nC ;
@B * * r @B ;
c ] ] l ca ;
c * * l c ;
ca _ c r @c ;
ca * * l ca ;
@c @ c l nC ;
@c * * r @c ;
C ] ] l Ca ;
C * * l C ;
Ca _ C r @C ;
Ca * * l Ca ;
@C @ C l nC ;
@C * * r @C ;
d ] ] l da ;
d * * l d ;
da _ d r @d ;
da * * l da ;
@d @ d l nC ;
@d * * r @d ;
D ] ] l Da ;
D * * l D ;
Da _ D r @D ;
Da * * l Da ;
@D @ D l nC ;
@D * * r @D ;
e ] ] l ea ;
e * * l e ;
ea _ e r @e ;
ea * * l ea ;
@e @ e l nC ;
@e * * r @e ;
E ] ] l Ea ;
E * * l E ;
Ea _ E r @E ;
Ea * * l Ea ;
@E @ E l nC ;
@E * * r @E ;
f ] ] l fa ;
f * * l f ;
fa _ f r @f ;
fa * * l fa ;
@f @ f l nC ;
@f * * r @f ;
F ] ] l Fa ;
F * * l F ;
Fa _ F r @F ;
Fa * * l Fa ;
@F @ F l nC ;
@F * * r @F ;
g ] ] l ga ;
g * * l g ;
ga _ g r @g ;
ga * * l ga ;
@g @ g l nC ;
@g * * r @g ;
G ] ] l Ga ;
G * * l G ;
Ga _ G r @G ;
Ga * * l Ga ;
@G @ G l nC ;
@G * * r @G ;
h ] ] l ha ;
h * * l h ;
ha _ h r @h ;
ha * * l ha ;
@h @ h l nC ;
@h * * r @h ;
H ] ] l Ha ;
H * * l H ;
Ha _ H r @H ;
Ha * * l Ha ;
@H @ H l nC ;
@H * * r @H ;
i ] ] l ia ;
i * * l i ;
ia _ i r @i ;
ia * * l ia ;
@i @ i l nC ;
@i * * r @i ;
I ] ] l Ia ;
I * * l I ;
Ia _ I r @I ;
Ia * * l Ia ;
@I @ I l nC ;
@I * * r @I ;
j ] ] l ja ;
j * * l j ;
ja _ j r @j ;
ja * * l ja ;
@j @ j l nC ;
@j * * r @j ;
J ] ] l Ja ;
J * * l J ;
Ja _ J r @J ;
Ja * * l Ja ;
@J @ J l nC ;
@J * * r @J ;
k ] ] l ka ;
k * * l k ;
ka _ k r @k ;
ka * * l ka ;
@k @ k l nC ;
@k * * r @k ;
K ] ] l Ka ;
K * * l K ;
Ka _ K r @K ;
Ka * * l Ka ;
@K @ K l nC ;
@K * * r @K ;
l ] ] l la ;
l * * l l ;
la _ l r @l ;
la * * l la ;
@l @ l l nC ;
@l * * r @l ;
L ] ] l La ;
L * * l L ;
La _ L r @L ;
La * * l La ;
@L @ L l nC ;
@L * * r @L ;
m ] ] l ma ;
m * * l m ;
ma _ m r @m ;
ma * * l ma ;
@m @ m l nC ;
@m * * r @m ;
M ] ] l Ma ;
M * * l M ;
Ma _ M r @M ;
Ma * * l Ma ;
@M @ M l nC ;
@M * * r @M ;
n ] ] l na ;
n * * l n ;
na _ n r @n ;
na * * l na ;
@n @ n l nC ;
@n * * r @n ;
N ] ] l Na ;
N * * l N ;
Na _ N r @N ;
Na * * l Na ;
@N @ N l nC ;
@N * * r @N ;
o ] ] l oa ;
o * * l o ;
oa _ o r @o ;
oa * * l oa ;
@o @ o l nC ;
@o * * r @o ;
O ] ] l Oa ;
O * * l O ;
Oa _ O r @O ;
Oa * * l Oa ;
@O @ O l nC ;
@O * * r @O ;
p ] ] l pa ;
p * * l p ;
pa _ p r @p ;
pa * * l pa ;
@p @ p l nC ;
@p * * r @p ;
P ] ] l Pa ;
P * * l P ;
Pa _ P r @P ;
Pa * * l Pa ;
@P @ P l nC ;
@P * * r @P ;
q ] ] l qa ;
q * * l q ;
qa _ q r @q ;
qa * * l qa ;
@q @ q l nC ;
@q * * r @q ;
Q ] ] l Qa ;
Q * * l Q ;
Qa _ Q r @Q ;
Qa * * l Qa ;
@Q @ Q l nC ;
@Q * * r @Q ;
r ] ] l ra ;
r * * l r ;
ra _ r r @r ;
ra * * l ra ;
@r @ r l nC ;
@r * * r @r ;
R ] ] l Ra ;
R * * l R ;
Ra _ R r @R ;
Ra * * l Ra ;
@R @ R l nC ;
@R * * r @R ;
s ] ] l sa ;
s * * l s ;
sa _ s r @s ;
sa * * l sa ;
@s @ s l nC ;
@s * * r @s ;
S ] ] l Sa ;
S * * l S ;
Sa _ S r @S ;
Sa * * l Sa ;
@S @ S l nC ;
@S * * r @S ;
t ] ] l ta ;
t * * l t ;
ta _ t r @t ;
ta * * l ta ;
@t @ t l nC ;
@t * * r @t ;
T ] ] l Ta ;
T * * l T ;
Ta _ T r @T ;
Ta * * l Ta ;
@T @ T l nC ;
@T * * r @T ;
u ] ] l ua ;
u * * l u ;
ua _ u r @u ;
ua * * l ua ;
@u @ u l nC ;
@u * * r @u ;
U ] ] l Ua ;
U * * l U ;
Ua _ U r @U ;
Ua * * l Ua ;
@U @ U l nC ;
@U * * r @U ;
v ] ] l va ;
v * * l v ;
va _ v r @v ;
va * * l va ;
@v @ v l nC ;
@v * * r @v ;
V ] ] l Va ;
V * * l V ;
Va _ V r @V ;
Va * * l Va ;
@V @ V l nC ;
@V * * r @V ;
w ] ] l wa ;
w * * l w ;
wa _ w r @w ;
wa * * l wa ;
@w @ w l nC ;
@w * * r @w ;
W ] ] l Wa ;
W * * l W ;
Wa _ W r @W ;
Wa * * l Wa ;
@W @ W l nC ;
@W * * r @W ;
x ] ] l xa ;
x * * l x ;
xa _ x r @x ;
xa * * l xa ;
@x @ x l nC ;
@x * * r @x ;
X ] ] l Xa ;
X * * l X ;
Xa _ X r @X ;
Xa * * l Xa ;
@X @ X l nC ;
@X * * r @X ;
y ] ] l ya ;
y * * l y ;
ya _ y r @y ;
ya * * l ya ;
@y @ y l nC ;
@y * * r @y ;
Y ] ] l Ya ;
Y * * l Y ;
Ya _ Y r @Y ;
Ya * * l Ya ;
@Y @ Y l nC ;
@Y * * r @Y ;
z ] ] l za ;
z * * l z ;
za _ z r @z ;
za * * l za ;
@z @ z l nC ;
@z * * r @z ;
Z ] ] l Za ;
Z * * l Z ;
Za _ Z r @Z ;
Za * * l Za ;
@Z @ Z l nC ;
@Z * * r @Z ;
Sp ] ] l Sp1 ;
Sp * * l Sp ;
Sp1 _ ~ r @Sp ;
Sp1 * * l Sp1 ;
@Sp @ _ l nC ;
@Sp * * r @Sp ;
nC _ @ l Sp ;
nC 0 0 * 9 ;
nC 1 1 * 9 ;
nC 2 2 * 9 ;
nC 3 3 * 9 ;
nC 4 4 * 9 ;
nC 5 5 * 9 ;
nC 6 6 * 9 ;
nC 7 7 * 9 ;
nC 8 8 * 9 ;
nC 9 9 * 9 ;
nC - - * 9 ;
nC a a * 9 ;
nC A A * 9 ;
nC b b * 9 ;
nC B B * 9 ;
nC c c * 9 ;
nC C C * 9 ;
nC d d * 9 ;
nC D D * 9 ;
nC e e * 9 ;
nC E E * 9 ;
nC f f * 9 ;
nC F F * 9 ;
nC g g * 9 ;
nC G G * 9 ;
nC h h * 9 ;
nC H H * 9 ;
nC i i * 9 ;
nC I I * 9 ;
nC j j * 9 ;
nC J J * 9 ;
nC k k * 9 ;
nC K K * 9 ;
nC l l * 9 ;
nC L L * 9 ;
nC m m * 9 ;
nC M M * 9 ;
nC n n * 9 ;
nC N N * 9 ;
nC o o * 9 ;
nC O O * 9 ;
nC p p * 9 ;
nC P P * 9 ;
nC q q * 9 ;
nC Q Q * 9 ;
nC r r * 9 ;
nC R R * 9 ;
nC s s * 9 ;
nC S S * 9 ;
nC t t * 9 ;
nC T T * 9 ;
nC u u * 9 ;
nC U U * 9 ;
nC v v * 9 ;
nC V V * 9 ;
nC w w * 9 ;
nC W W * 9 ;
nC x x * 9 ;
nC X X * 9 ;
nC y y * 9 ;
nC Y Y * 9 ;
nC z z * 9 ;
nC Z Z * 9 ;
nC ( ( * fC ;
fC ] ] l fC1 ;
fC * * l fC ;
fC1 _ [ * cl ;
fC1 * * l fC1 ;
cl [ _ r cl ;
cl ] _ r cl ;
cl ~ _ r cl ;
cl ( _ r clO ;
clO ) _ * halt-accept ;
clO * _ r clO ;
cl ) ) * halt-accept ;
cl * * r cl ;

Try it online!

Only handles alphanumerics at the moment. But can be extended rather trivially to handle any characters of most UTF sets.

\$\endgroup\$
2
\$\begingroup\$

ARM Thumb-2 machine code (Linux, no libc), 18 bytes

This version exits quite impolitely by crashing with Bad system call.

However, the program itself doesn't print anything to stderr, the shell does. 😏

Machine code:

4669 2201 2703 1ef8 df00 4347 f087 0707
e7f9

Commented assembly:

        .syntax unified
        .arch armv6t2
        .thumb
        .globl _start
        .thumb_func
        // cat program (exits with SIGSYS)
        // read = syscall 3, write = syscall 4
_start:
        // use sp as a pointer for read/write
        mov     r1, sp
        // one byte
        movs    r2, #1
        // start with read (3)
        movs    r7, #3
.Lloop:
        // read (3) - 3 = 0 = stdin, write (4) - 3 = 1 = stdout
        subs    r0, r7, #3
        // syscall
        svc     #0
        // We multiply the syscall by r0.
        // On an error, r0 will be zero or negative, setting r7 to that
        muls    r7, r0
        // If r0 was 1, flip between read (3) and write (4) with xor.
        // Otherwise, any negative or zero value will set this to a
        // bad syscall, raising a SIGSYS.
        // Yes, ARM doesn't have syscall 7.
        eor.w   r7, r7, #7
        // loop indefinitely
        b       .Lloop

If read and write return 1, the syscall will be multiplied by 1, doing nothing, and the eor.w will flip it between read (3) and write (4) by XORing 7.

If read or write returns zero, the multiply will set r7 to 0 XOR 7, so it will try to run syscall 7, which, for some reason, doesn't exist on ARM. Therefore it crashes with a bad system call.

If it returns negative, well, there obviously aren't any negative syscalls, and it also crashes with a bad system call.

Otherwise, it is a fairly standard read/write loop, aside from using syscall arithmetic to swap between stdin and stdout.

No errors, 22 bytes

Machine code:

4669 2201 2703 1ef8 df00 2801 bf14 2701
f087 0707 e7f7

Commented assembly:

        .syntax unified
        .arch armv6t2
        .thumb
        .globl _start
        .thumb_func
        // cat program (clean exit)
        // AFAIK, there is nothing that is smaller than 22 bytes that
        // exits nicely.
        // exit = syscall 1, read = syscall 3, write = syscall 4
_start:
        // use sp as a pointer for read/write
        mov     r1, sp
        // one byte
        movs    r2, #1
        // start with read (3)
        movs    r7, #3
.Lloop:
        // read (3) - 3 = 0 = stdin, write (4) - 3 = 1 = stdout
        // exit doesn't really matter
        subs    r0, r7, #3
        // syscall
        svc     #0
        // On an error, r0 will not be 1.
        cmp     r0, #1
        ite     ne
        // If we failed, set the syscall to exit (1).
        movne   r7, #1
        // Otherwise, flip between read (3) and write (4) with xor.
        eoreq.w r7, r7, #7
        // loop indefinitely
        b       .Lloop

This is roughly the same thing, only it wastes 4 bytes setting the syscall to exit when the syscall fails.

I had an epic fail when writing this.

I thought "oh, I can switch between read and write by XORing 1".

And so I tried it, and it didn't work.

I ran echo "test" | ./a.out aaaaaand my phone froze.

3 XOR 1 == 2. Syscall 2 is fork. I accidentally fork bombed my phone because I can't do math. 😂

ARM Thumb-2 Linux ELF executable, 52 bytes

We can actually pack the first version into an ELF executable quite nicely using the muppet labs method, for a full program at 52 bytes:

xxd -g1 :

00000000: 7f 45 4c 46 01 00 00 00 00 00 00 00 00 00 01 22  .ELF..........."
00000010: 02 00 28 00 1b 00 01 22 1b 00 01 22 04 00 00 00  ..(...."..."....
00000020: 03 27 f8 1e 69 46 00 df 47 43 20 00 01 00 87 f0  .'..iF..GC .....
00000030: 07 07 f6 e7                                      ....

Assembly with comments (le ugly hack intensifies):

// Adaptation of
//    https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
// for an ARM cat program.
// $ arm-none-eabi-as -march=armv6t2 -mthumb cat-arm.s -o cat-arm.o
// $ arm-none-eabi-ld -Ttext 0x22010000 cat-arm.o -o cat-arm.elf
// $ arm-none-eabi-objcopy -O binary cat-arm.elf cat-arm

        .text              // A lie.
        .arch   armv6t2    // Set architecture version
        .thumb             // Needs to be Thumb to fit. ARM is too thicc.
        .syntax unified
        .org    0          // Start at offset 0
Elf32_Ehdr:
        .byte   0x7F,'E','L','F'              // e_ident
Elf32_Phdr:
        .word   1                                             // p_type
        .word   0                                             // p_offset
        // squeeze an instruction into the start offset
        // 0x2201 -> movs r2, #1
        .word   0x22010000 // same as -Ttext                  // p_vaddr
        .short  2          // ET_EXEC        // e_type        // p_paddr
        .short  40         // EM_ARM         // e_machine
        .word   _start + 2                   // e_version     // p_filesz
        .thumb_func
        .globl _start
_start:
        // start halfway into this word, it will be an instruction
        // movs r2, #1     // len = 1
        .word  _start + 2                    // e_entry       // p_memsz
        // 0004 0000 -> just messing with r4 and r0, harmless
        .word   4          // Elf32_Phdr     // e_phoff       // p_flags
        // start with r7 == write (3)
        movs    r7, #3     // write          // e_shoff       // p_align
.Lloop:
        subs    r0, r7, #3 // fd (0 or 1)
        mov     r1, sp     // buffer is sp   // e_flags
        swi     #0         // do syscall
        // We multiply the syscall by r0.
        // On an error, r0 will be zero or negative.
        muls    r7, r0                       // e_ehsize
        // finish the ELF header. This is basically harmless.
        // movs r0, r4
        .short  0x20                        // e_phentsize
        // movs r1, r0
        .short  1                           // e_phnum
        // If r0 was 1, flip between read (3) and write (4) with xor.
        // Otherwise, any negative or zero value will set this to a
        // bad syscall, raising a SIGSYS.
        // Yes, ARM doesn't have syscall 7.
        eor.w   r7, r7, #7
        // loop indefinitely
        b       .Lloop

One cool thing is that we don't need to branch over any header fields: 00xx is a simple movs, which we can deal with easily.

I actually start halfway into e_entry itself, using a custom p_vaddr which acts as an instruction, and ignoring the next two instructions.

I don't have a way to make an interactive demo yet until someone adds QEMU to TIO.

\$\endgroup\$
2
\$\begingroup\$

ThumbGolf, 8 bytes

de10 de18 d0fc 4770

Assembly

        // Include ThumbGolf wrapper macros
        .include "thumbgolf.inc"

        // Who said you had to start at the top?
.Lloop:
        // Print the byte in r0 to stdout
        putc    r0 // udf #0020
        // Fallthrough

        .globl main
        .thumb_func
        // Entry point is here
main:
        // Read a byte from stdin to r0.
        // This sets the Z flag on success,
        // allowing us to easily detect EOF.
        getc    r0 // udf #0030
        // If successful, go to the top to print
        beq     .Lloop
        // EOF or error, return
        // this actually returns zero as a plus
        bx      lr

ThumbGolf is a brand new, very WIP golfing extension to Thumb-2 I am making out of boredom.

It takes what I see as a RISC approach to a golfing language, giving you the tools to write the algorithm instead of giving you the algorithm itself.

I decided to make its initial debut with the classic cat program.

It doesn't have an online interpreter yet, but you can compile it yourself, and run it on ARM Linux (including Termux) or with qemu-arm.

\$\endgroup\$
2
\$\begingroup\$

Grok, 9 bytes

:j
}lYW
q

Non-terminating 4 byte version:

:j
wl
\$\endgroup\$
2
\$\begingroup\$

Alice, 8 4 bytes

-4 bytes thanks to Martin Ender

It's my first time trying out Alice, and it's a pretty fun language to use so far.

Unlike Sp3000's answer, this one doesnt error out on EOF, at the cost of some bytes.

It also uses Ordinal mode. It's not the golfiest answer, but variety is fun.

\ o
@i

Try it online!

Explanation

\      - Enter ordinal mode, the IP now moves south-east.
 i     - Push STDIN to the stack as string
  o    - Print it
   i   - Grab STDIN again, but it reads nothing cause only EOF remains.
    \  - Go back to cardinal mode, the IP now moves south.
     @ - Terminate
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Thanks for checking out Alice! :) Even with an Ordinal mode solution, you can save a couple of bytes like this: tio.run/##S8zJTE79/z9GIZ/LIfP//8TUzPxSAA \$\endgroup\$ Jun 4, 2021 at 13:07
  • \$\begingroup\$ Thanks! I'm loving the language so far, I love puzzling around with the mirrors and modes. \$\endgroup\$ Jun 4, 2021 at 13:12
2
\$\begingroup\$

brainbox, 6 bytes

-],.]!

Try it Online!

This program makes use of the fact that if a ] doesn't have a pointer to jump back to, it jumps back to before the beginning of the program, or coordinates (0,0). This sets the memory cell to 0 before each character is inputted, then gets the input, outputs it, and checks if it is 0. If it is not, it goes back to the beginning, and if it is, the program terminates.

You can pipe the input, or use console input, with Esc acting as EOF.

Non-terminating, 2 bytes

,.

Since the pointer wraps when it hits the edge of the program, this will input then output over and over forever.

\$\endgroup\$
2
\$\begingroup\$

INTERCAL (C-INTERCAL), 303 236 229 bytes

  1. Forgot one more PLEASEDO to replace with PLEASE.
  2. Also .2<-.4~#255 to .2<-.4.
DO,1<-#1DOCOMEFROM#3DOWRITEIN,1(1)DO.1<-,1SUB#1DO.2<-.4DO(1000)NEXTDO.4<-.3PLEASE.3<-!3~#15'$!3~#240'DO.3<-!3~#15'$!3~#240'DO.2<-!3~#15'$!3~#240'PLEASE.1<-.5DO(1010)NEXTDO.5<-.2DO,1SUB#1<-.3(3)PLEASEREADOUT,1PLEASECOMEFROM.1~#256

Try it online!

Previous

Seeing original code, I noticed that program should end when .1 (,1SUB#1) has #256, so I erased some NEXT and RESUME statements to replace with (1)DO.1<-... and PLEASECOMEFROM.1~#256. It works, even the expression .1~#256 evaluates to zero when not reached to EOF. Additionally, replacing .1 with ',1SUB#1' worked; this is opposed to CLC-INTERCAL. This is really a golfing tip.

Also I almost forgot that every variable is initialized with zero.

DO,1<-#1DOCOMEFROM#3DOWRITEIN,1(1)DO.1<-,1SUB#1DO.2<-.4DO(1000)NEXTDO.4<-.3~#255PLEASEDO.3<-!3~#15'$!3~#240'DO.3<-!3~#15'$!3~#240'DO.2<-!3~#15'$!3~#240'PLEASE.1<-.5DO(1010)NEXTDO.5<-.2DO,1SUB#1<-.3(3)PLEASEREADOUT,1PLEASECOMEFROM.1~#256

Try it online!

Prev.: 303 bytes.

Yesterday I showed you making a short cat program in CLC-INTERCAL is easy; but it's not really in C-INTERCAL, due to its I/O system.

The following program originates Louis Howell's; I reduced the code for this golf problem. This seemed to be the shortest one that satisfies politeness check.

DO,1<-#1DO.4<-#0DO.5<-#0DOCOMEFROM#3DOWRITEIN,1DO.1<-,1SUB#1DO(1)NEXTDO_(2)PLEASERESUME'?.1$#256'~'#256$#256'(1)DO(2)NEXTDOFORGET#1PLEASE.2<-.4DO(1000)NEXTDO.4<-.3~#255PLEASEDO.3<-!3~#15'$!3~#240'DO.3<-!3~#15'$!3~#240'DO.2<-!3~#15'$!3~#240'PLEASE.1<-.5DO(1010)NEXTDO.5<-.2DO,1SUB#1<-.3(3)PLEASEREADOUT,1

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

KonamiCode, 22 bytes

>>>(>)L(^)<<>((>))B(^)

This crashes with an InvalidMemoryAddress error, however I have also made a version that is 4 bytes longer but does not crash:

>>v(>)>(>)L(^)<<>((>))B(^)

Explanation for the no-crash version: >>: Inputs the string to echo

v(>): Writes a zero to memory directly after the inputted string. This is the part that is removed in the crashing program.

>(>): Sets the address pointer back to 0

L(^): A loop marker

<<: Prints the character at the current address

>((>)): Advances the memory pointer

B(^): A loop instruction. This will go back to the loop marker and repeat until we reach the 0 value in memory. Becuase the comparison buffer is set to 0 by default, this terminates the program.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 6 bytes

p gets

Attempt This Online!

\$\endgroup\$
1
  • \$\begingroup\$ This works only for single line and even then it adds quotes and escapes some characters. \$\endgroup\$
    – Jiří
    Jul 25, 2022 at 1:33
2
\$\begingroup\$

Fig, \$1\log_{256}(96)\approx\$ 0.823 bytes

x

Try it online!

Fig only implicitly inputs if there is a need, so we have to explicitly input here.

\$\endgroup\$
2
\$\begingroup\$

Logically, 137 bytes

@R:i:p:b_HIGH;NOT(i)(b)AND(i,b)(p)
@D:i:o:9d;COPY(i,9d)(9d,o)
@M:c,r,d,e,8i;R(h)(r)OR(r,c)(d)D(d)(c)READ(c)(e,8i)WRITE(c,8i)()HALT(e)()

Ungolfed

@RisingEdge
: in;
: pulse;
: bar_HIGH;
NOT (in)      (bar)
AND (in, bar) (pulse)

@Delay
: in;
: out;
: 9d;
COPY (in, 16d) (16d, out)

@Main
: clock, risingPulse, delayIn, eof;
RisingEdge      (H)                     (risingPulse)
OR              (risingPulse, clock)    (delayIn)
Delay           (delayIn)               (clock)
READ            (clock)                 (eof, 8i)
WRITE           (clock, 8i)             ()
HALT            (eof)                   ()

Starts by combing a simple rising pulse and a delay to act as a clock, which then reads and writes characters each clock cycle. when eof is reached, halt is pulled.

\$\endgroup\$
1
\$\begingroup\$

osascript, 14 bytes

on run a
a
end

Grabs all input from STDIN and returns it. This will not work for infinite input.

\$\endgroup\$
1
  • \$\begingroup\$ This prints a trailing newline - I'm not sure how to get rid of it, but I'll try a few things. \$\endgroup\$ Oct 30, 2015 at 16:45
1
\$\begingroup\$

AppleScript, 50 Bytes

(display dialog""default answer"")'s text returned

Opens a display dialog (equivalent to STDIN) and returns all the text input into the dialog pane out to the result pane. Will not support infinite input.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 22 bytes

putc$_ while$_=$<.getc

Should handle null bytes and infinite streams.

\$\endgroup\$
1
\$\begingroup\$

Curry, 16 bytes

main=interact id

Curry is a Haskell look-alike, so it shouldn't be surprising that a cat program looks... well, exactly the same in it :) It's lazy just like Haskell, so it handles infinite streams fine.

\$\endgroup\$
1
\$\begingroup\$

Burlesque, 1 byte

Q

STDIN is pushed on the stack as a string. Q pretty-prints this string, which amounts to Burlesque not printing it "raw", i.e. surrounded by quotes. It's implicitly printed at the end of the program.

(I can't test it right now, but this might handle infinite streams? It's implemented in Haskell, so...)

\$\endgroup\$
3
  • \$\begingroup\$ It handles infinite streams as well, yes. You can work on infinite streams i.e. try cat /dev/urandom | base64 | blsq --stdin "2coQ" and it will work just fine. However, not all built-ins are lazy. For example trying to do 2couN wont work because \[ is not lazy. Mapping however will work i.e. 2co)<-Q works fine. Also doing 2cop^ nor 2co^p will work. \$\endgroup\$
    – mroman
    Nov 13, 2015 at 17:25
  • \$\begingroup\$ Additionally you need to know that some built-ins don't work natively on strings but Explode them and then Concat them back. That's why f[ won't work on infinite strings because f[ will do an auto-concat afterwards. So if you want to filter an infinite string you need to convert it to a block. For example XX:><2co)\[)rib2 works on infinite strings but if you don't do the XX it won't :). \$\endgroup\$
    – mroman
    Nov 13, 2015 at 17:30
  • \$\begingroup\$ I think the reason is that \[ falls back to r[ and r[ produces a single value out of a block and thus can't possibly work on infinite lists. \$\endgroup\$
    – mroman
    Nov 13, 2015 at 17:32
1
\$\begingroup\$

Frege, 16 bytes

main=interact id

Another Haskell-like, another totally equivalent cat program.

\$\endgroup\$
1
\$\begingroup\$

Pascal, 64 bytes

var c:Char;begin while not eof do begin read(c);write(c)end end.

Needless to say, Pascal is not the ideal golfing language.

\$\endgroup\$
1
  • \$\begingroup\$ This can be abbreviated even further using buffer variables. begin while not EOF do begin write(input^);get(input)end end. Note, an ISO-compliant processor will print a space character for every newline. Maybe you should indicate which dialect you are using (because the omission of theprogram header is also non-standard). \$\endgroup\$ Nov 3, 2022 at 23:21
1
\$\begingroup\$

Oberon, 109 bytes

MODULE m;IMPORT Files,Out,In;VAR c:CHAR;BEGIN WHILE~Files.Eof(Files.stdin)DO In.Char(c);Out.Char(c)END END m.

Ouch...

\$\endgroup\$
1
\$\begingroup\$

Java, 118 bytes

class A{public static void main(String[]a)throws Exception{org.apache.commons.io.IOUtils.copy(System.in,System.out);}}

(Uses apache commons IO; I'm not actually sure what the behavior is with null bytes and with infinite text).

\$\endgroup\$
1
\$\begingroup\$

Kipple, 5 Bytes

Calling the program:

java -jar Kipple.jar -i [input] cat.k

Stored in a file called cat.k - replace this with the filename.

Code:

(i>o)

As this is on the wiki page, I will be posting this as a community wiki.

\$\endgroup\$
1
\$\begingroup\$

Simplex v.0.7, 2 bytes

g is perhaps my favorite command. It is short for {sL}, {sp}, and Ts.

bg 
b  ~~ take input and write input to strip
 g ~~ output contents of strip
\$\endgroup\$
1
\$\begingroup\$

Lua, 31 characters

::a::io.write(io.read(1))goto a

Will crash on EOF, as io.read(1) returns nil, which is an invalid argument to io.write. This needs Lua's goto statement, which is new since version 5.2.

\$\endgroup\$
1
  • \$\begingroup\$ The default for io.read() is the file descriptor 1, you could save 1 Byte by writing ::a::io.write(io.read())goto a. \$\endgroup\$
    – Katenkyo
    Mar 2, 2016 at 15:29
1
\$\begingroup\$

C#, 83 bytes

using c=System.Console;class A{static void Main(){for(;;)c.Write((char)c.Read());}}

Seems to be shorter than using static, which another (faulty) answer here was using.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It should terminate on end of stream, right? \$\endgroup\$
    – Zereges
    Oct 31, 2015 at 22:58
1
\$\begingroup\$

F#, 74 bytes

type C=System.Console
[<EntryPoint>]let rec(!)x=C.Write(char(C.Read()));!x

Conceptually, this is similar to my C# and VB.NET answers. F# is strict about the type of main: it has to be precisely string[] -> int. The neat part is its name doesn't matter: whatever you mark as <EntryPoint> will be the program's main function. I saved a byte by calling it (!), a prefix operator, allowing me to write !x instead of m x or something.

\$\endgroup\$
1
\$\begingroup\$

Rust, 68 bytes

use std::io;fn main(){io::copy(&mut io::stdin(),&mut io::stdout());}

io::copy copies the entire contents of a reader into a writer. This function (I think) needs to borrow and mutate both stdin and stdout to do this, which is what &mut is about.

\$\endgroup\$
1
\$\begingroup\$

Julia, 35 bytes

while 0<1write(read(STDIN,Char))end

Similar to Alex's answer, but crashes at EOF. I think that's valid.

\$\endgroup\$
3
  • \$\begingroup\$ Nevermind then. \$\endgroup\$
    – Dennis
    Oct 31, 2015 at 3:56
  • 1
    \$\begingroup\$ Considering how similar it is to the other Julia answer, I probably would have left this as a comment instead. \$\endgroup\$
    – Alex A.
    Oct 31, 2015 at 5:57
  • \$\begingroup\$ I came up with almost this very answer (I had Int8 instead of Char, I'm not sure if mine would have worked for high bytes) before looking at yours -- but they were very similar when I compared them indeed! I think it's worth having both around though (a crashy one and a non-crashy one). \$\endgroup\$
    – Lynn
    Oct 31, 2015 at 10:51
1
\$\begingroup\$

Nim, 53 bytes

while not stdin.endOfFile:stdout.write stdin.readChar

Not much to say here. I need sleep now. :)

\$\endgroup\$
1
\$\begingroup\$

FlogScript, 1 byte

"

I lied. Here's one more, before anyone else gets to it. Doesn't handle infinite streams.

\$\endgroup\$
1
3 4
5
6 7
11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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