11

I looked up a Unix book, the man and the wikipedia page for tr but could not find a reason why it was designed/implemented in such way that it does not read from file but strictly only from the standard input. For instance, tools such as wc, grep, sed, and awk all will happily read input from file if provided or from standard input. Was/Is there a compelling reason to design tr this way?

  • 2
    It was not coded that way, and life goes on... I suspect it must be due being another time with machines with severe memory restrictions, and tr is old. Unix utilities also were designed by several people, and well there are several inconsistencies and duplication of functionality across utilities. – Rui F Ribeiro Oct 26 '16 at 0:38
  • 3
    just to point out: this question has been flagged as primarily opinion-based but IMHO it shouldn't be closed; we've had questions similar to this on why things are implemented in whatever way that have very valid answers. see for example unix.stackexchange.com/questions/6804/what-does-dd-stand-for or unix.stackexchange.com/questions/93773/why-is-sort-o-useful – strugee Oct 26 '16 at 2:35
  • 2
    It is now out of the review queue, but still sitting perilously on 4 close votes - if it does get the last one, please ping me here so I can vote to reopen. – Michael Homer Oct 26 '16 at 6:22
  • 3
    @strugee I disagree. Yes, us speculating sure are just opinion... but since there may still be people out there who actually know - or remember reading/hearing a first-hand account - it is a question it may be possible to answer. 1970 isn't that long ago. – Baard Kopperud Oct 26 '16 at 6:30
  • 6
    Unix inherited tr from Multics as a derivative of PL/1 translate built-in function, which in turn was a generalization of a TR command in System/360 architecture (see IBM System-360 Green Card). Worth noting that QNX implementation could actually read from a file via -r – don_crissti Nov 1 '16 at 0:33
13
+100

The UNIX philosophy advocates for "small, sharp tools", so the answer is that reading from a file would be bloat contrary to the UNIX philosophy. As to why wc, grep, sed, awk, etc do read from files, the answer is that they all have features that require more than one input or input selection or otherwise require direct access to the files. As tr is not commonly used for those reasons you are left with one of the following forms to meet your needs;

tr ... < file
tr ... < file > file2
tr ... < file | sponge file
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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