13

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Ketan
  • 9,226
  • 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 00:38
  • 1
    Agreed with most of what you said but still... tr just comes across as a single odd such command. I can't think of any other similar command that has this kind of behavior. – Ketan Oct 26 '16 at 00:42
  • Not sure as to the reason, but you could redirect standard input < - to get tr to act on a file, e.g. tr "this text" "to this text" < some_file.txt . – marshki Oct 26 '16 at 00:46
  • 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 https://unix.stackexchange.com/questions/6804/what-does-dd-stand-for or https://unix.stackexchange.com/questions/93773/why-is-sort-o-useful – strugee Oct 26 '16 at 02:35
  • 3
    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 06: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 06: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 01 '16 at 00:33

1 Answers1

16

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
user1133275
  • 5,574