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_catshould print an infinite stream ofys. 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 fulfill 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 code-golf rules apply, including the loopholes that are forbidden by default.
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>
341 Answers 341
BrainChild ASM, 11 bytes
asm {
reada
inca
jnza oup
halt
oup:deca
writea
jmpb
}
Hex-dump of bytecode
0 1 2 3 4 5 6 7 8 9 A B C D E F
00000000: 27 15 0D 00 00 00 02 01 17 29 0C | '........).
Naïve solution, checks if the input is a EOF, if it isn't, outputs and repeats. Loops by jumping to the b register, which defaults to 0.
Positionally, (削除) 102 (削除ここまで) (削除) 76 (削除ここまで) \71ドル\log_{256}(3)\approx\$ 14 bytes
Actually it's 14.07 bytes, but the leaderboard doesn't take decimals
> ^
@ ^
> . : v
v < ;<
\ s /
\$\log_{256}3\$ is because each character is either a space, a newline, or non-whitespace.
Re:direction -A, 1 byte(Codepage 437)
◄
The -A option allows Re:direction to interpret the input as the list of its character codes, rather than trying to parse numbers from it.
Explanation
In Re:direction, an arrow on a line by itself is a halt command. Arrows also change the command queue, but leftwards and upwards arrows don't have an effect on how the command queue is interpreted as I/O.
The input specifies the initial command queue, and the output is taken from the final command queue. So if we don't change it at all before halting (or change it only by adding leftwards or upwards arrows), the output will be the same as the input.
Re:direction also supports a more efficient character encoding than codepage 437, but the program wouldn't be any shorter.
Bitwise Cyclic Tag, 1 bit, 0* bytes
*It's actually 0.125 bytes but the leaderboard doesn't take decimals
0
This will delete bits from the data tape (taking the starting data configuration as input) in order until there are none remaining to delete, at which point the program exits gracefully.
I'm on a phone now, but when I get the chance, I'll write an interpreter and post a TIO link.
-
\$\begingroup\$ the
usercommand would turn into a string if i remove the}for some reason \$\endgroup\$Gleb– Gleb2024年10月24日 22:12:01 +00:00Commented Oct 24, 2024 at 22:12
257-wrap brainfuck, 13 bytes
Designed by @l4m2. Uses 256 as EOF and merges , into .; trying to write EOF value results in reading a byte.
Modified from the original post; reading the specifications carefully:
-.+[-.[-]-.+]
SAKO, 80 bytes
CALKOWITE:*A,B
BLOK(9):A
1)A(=B)=0
CZYTAJWIERSZ:A
DRUKUJWIERSZ:A
SKOCZDO1
KONIEC
There's not much to explain.
- Create an array of integers
- Set all elements to zero
- Read into it our input
- Print it. Go to point 2.
The limit of characters here is ten, but can be increased by changing the number in BLOK(9):A
AArch64 machine code (Linux), 32 bytes
Port of ARM Thumb-2 expect SYS_read and SYS_write are 63 and 64.
Exits with Bad system call.
Disassembly of section .text:
0000000000000000 <_start>:
0: 910003e1 mov x1, sp
4: d2800022 mov x2, #0x1 // #1
8: d28007e8 mov x8, #0x3f // #63
c: d100fd00 sub x0, x8, #0x3f
10: d4000001 svc #0x0
14: 9b007d08 mul x8, x8, x0
18: d2401908 eor x8, x8, #0x7f
1c: 17fffffc b c <_start+0xc>
Original source
.globl _start
_start:
mov x1,sp // Stores one-character input
mov x2,1 // Length of to input/output
mov x8,63 // SYS_read
// Main loop
1:
sub x0,x8,63 // 63-63=0 for fd_stdin; 64-63=1 for fd_stdout
// (OBTW 127-63=64)
svc 0 // system call
// read() sets 1 or 0 to x0 to indicate number of bytes read
// write() sets 1 to x0; I don't think other values would be set
mul x8,x8,x0 // sets x8 to either 64 or 63 or 0
eor x8,x8,127 // takes XOR: 63 to 64; 64 to 63; 0 to 127
b 1b
Obtw AArch64 system call number 127 is sched_rr_get_interval; its synopsis is:
int sched_rr_get_interval(pid_t pid, struct timespec *tp);
On my Termux it had resulted in Bad system call; hope some other users of AArch64 Linux let me know what would happen on your machine for this program.
History
Began with 36+1=37 bytes
-5 bytes by porting from Thumb-2 answer
True, 2 bytes
,.
Decided to make a better interpreter becuase the old one sucked.
Also this raises an EOFError to not cause index error becuase of an empty input list.
And if error termination is unallowed, then we have a problem considreing there is no way to do a try/except in the langauge
cat\$\endgroup\$shanswer usingcatwhich also contains a shorter solution usingdd.) \$\endgroup\$