This commit is contained in:
Miloslav Ciz 2023-11-12 01:41:59 +01:00
parent ff131e2f76
commit 62a042b858
5 changed files with 18 additions and 13 deletions

View file

@ -1,17 +1,17 @@
# Unix Philosophy
Unix philosophy is one of the most important and essential approaches to programming which advocates great [minimalism](minimalism.md) and is best known by the saying that **a program should only do one thing and do it well**. Unix philosophy is a collective wisdom, a set of recommendations evolved during the development of one of the earliest [operating systems](os.md) called [Unix](unix.md), hence the name. Unix philosophy advocates simplicity, clarity, modularity, reusability and composition of larger programs out of smaller programs rather than designing huge monolithic programs as a whole. Unix philosophy, at least partially, lives on in many project and Unix-like operating systems such as [Linux](linux.md) (though Linux is more and more distancing from Unix), has been wholly adopted by groups such as [suckless](suckless.md) and [LRS](lrs.md) (us), and is even being expanded in such projects as [plan9](plan9.md).
Unix philosophy is one of the most important and essential approaches to [programming](programming.md) (and by extension all [technology](tech.md) design) which advocates great [minimalism](minimalism.md) and is best known by the saying that **a program should only do one thing and do it well**. Unix philosophy is a collective wisdom, a set of recommendations evolved during the development of one of the earliest [operating systems](os.md) called [Unix](unix.md), hence the name. Unix philosophy advocates simplicity, clarity, modularity, reusability and composition of larger programs out of smaller programs rather than designing huge monolithic programs as a whole. Unix philosophy, at least partially, lives on in many project and Unix-like operating systems such as [Linux](linux.md) (though Linux is more and more distancing from Unix), has been wholly adopted by groups such as [suckless](suckless.md) and [LRS](lrs.md) (us), and is even being expanded in such projects as [plan9](plan9.md).
In 1978 [Douglas McIlroy](mcilroy.md) has written a short overview of the Unix system (*UNIX Time-Sharing System*) in which he gives the main points of the system's style; this can be seen as a summary of the Unix philosophy (the following is paraphrased):
1. **Each program should do one thing and do it well**. Overcomplicating existing programs isn't good; for new functionality create a new program.
2. **Output of a program should be easy to interpret by another program**. In Unix programs are chained by so called [pipes](pipe.md) in which one program sends its output as an input to another, so a programmer should bear this in mind. [Interactive](interactive.md) programs should be avoided if possible.
2. **Output of a program should be easy to interpret by another program**. In Unix programs are chained by so called [pipes](pipe.md) in which one program sends its output as an input to another, so a programmer should bear this in mind. [Interactive](interactive.md) programs should be avoided if possible. Make your program a [filter](filter.md) if possible, as that exactly helps this case.
3. **Program so that you can test early, don't be afraid to throw away code and rewrite it from scratch**.
4. **Write and use tools**, even if they're [short-lived](throwaway_script.md), they're better than manual work. Unix-like systems are known for their high [scriptability](script.md).
This has later been condensed into: do one thing well, write programs to work together, make programs communicate via text streams, a universal interface.
**Example**: maybe the most common practical example that can be given is [piping](pipe.md) small [command line](cli.md) utility programs; in Unix there exist a number of small programs that do *only one thing but do it well*, for example the [`cat`](cat.md) program that only displays the content of a file, the [`grep`](grep.md) program that searches for patterns in text etc. In a command line we may use so called [pipes](pipe.md) to chain some of these simple programs into more complex processing pipelines. Let's say we want to for example automatically list all first and second level headings on given webpage and write them out alphabetically sorted. We can do it with a command such as this one:
**Simple example**: maybe the most common practical example that can be given is [piping](pipe.md) small [command line](cli.md) utility programs; in Unix there exist a number of small programs that do *only one thing but do it well*, for example the [`cat`](cat.md) program that only displays the content of a file, the [`grep`](grep.md) program that searches for patterns in text etc. In a command line we may use so called [pipes](pipe.md) to chain some of these simple programs into more complex processing pipelines. Let's say we want to for example automatically list all first and second level headings on given webpage and write them out alphabetically sorted. We can do it with a command such as this one:
```
curl "https://www.tastyfish.cz/lrs/main.html" | grep "<h[12]>.*</h[12]>" | sed "s/[^>]*> *\([^<]*\) *<.*/\1/g" | sort
@ -26,9 +26,9 @@ Welcome To The Less Retarded Wiki
What Is Less Retarded Software
```
In the command the pipes (`|`) chain multiple programs together so that the output of one becomes the input of the next. The first command, [`curl`](curl.md), downloads the [HTML](html.md) content of the webpage and passes it to the second command, [`grep`](grep.md), which filters the text and only prints lines with headings, this is passed to [`sed`](sed.md) that removes the HTML code and the result is passed to `sort` that sorts the lines alphabetically -- as this is the last command, the result is then printed. This is fast, powerful and very flexible way of processing data for anyone who knows the Unix tools. Notice the relative simplicity of each command and how each works with text -- the universal communication interface.
In the command the pipes (`|`) chain multiple programs together so that the output of one becomes the input of the next. The first command, [`curl`](curl.md), downloads the [HTML](html.md) content of the webpage and passes it to the second command, [`grep`](grep.md), which filters the text and only prints lines with headings, this is passed to [`sed`](sed.md) that removes the HTML code and the result is passed to `sort` that sorts the lines alphabetically -- as this is the last command, the result is then printed out. This is fast, powerful and very flexible way of processing data for anyone who knows the Unix tools. Notice the relative simplicity of each command and how each one works as a **[text](text.md) [filter](filter.md)**; text is a universal communication interface and behaving as a filter makes intercommunication easy and efficient. A filter simply takes an input stream of data and outputs another stream of data; it ideally works on-the-go (without having to load whole input in order to produce the output), which has great many advantages, for example requiring only a small amount of memory (which may become significant when we are running many programs at once in the pipeline) and decreasing [latency](latency.md) (the next pipe stage may start processing the data before the previous stage finishes). When you're writing a program, such as for example a [compression](compression.md) tool, make it work like this.
Compare this to the opposite [Window philosophy](windows_philosophy.md) in which combining programs into collaborating units is not intended or even purposefully prevented, and therefore very difficult, slow and impractical to do -- such programs are designed for manually performing some predefined actions, e.g. painting pictures with a mouse, but aren't made to collaborate or be automatized, they can rarely be used in unintended, inventive ways needed for [hacking](hacking.md).
Compare this to the opposite [Window philosophy](windows_philosophy.md) in which combining programs into collaborating units is not intended, is possibly even purposefully prevented and therefore very difficult, slow and impractical to do -- such programs are designed for manually performing some predefined actions, mostly using [GUI](gui.md), e.g. painting pictures with a mouse, but aren't made to collaborate or be automatized, they can rarely be used in unintended, inventive ways needed for powerful [hacking](hacking.md). Getting back to the example of a compression tool, on Windows such a program would be a large GUI program that requires a user to open up a file dialog, manually select a file to compress, which would then probably go on to load the whole file into memory, perform compression there, and the write the data back to some other file. Need to use the program on a computer without graphical display? Automatize it to work with other programs? Run it from a script? Run it 10000 at the same time with 10000 other similar programs? Bad luck, Windows philosophy doesn't allow this.
**Watch out! Do not misunderstand Unix philosophy.** There are many extremely dangerous cases of misunderstanding Unix philosophy by [modern](modern.md) wannabe programmers. One example is the hilarious myth about "[React](react.md) following Unix philosophy" ([LMAO this](http://img.stanleylieber.com/src/20872/img/small.1527773532.png)), supposedly the "devs" think that having billion of dependencies or focusing on doing one huge thing ([GUI](gui.md)) somehow implies Unix philosophy -- **nothing based on [JavaScript](js.md) can ever follow Unix philosophy!** Unix philosophy can NOT be built on top of non-unix philosophy technology, and focusing on a very broad goal does not mean doing one thing.
@ -36,8 +36,10 @@ Compare this to the opposite [Window philosophy](windows_philosophy.md) in which
## See Also
- [LRS](lrs.md)
- [Unix](unix.md)
- [minimalism](minimalism.md)
- [suckless](suckless.md)
- [KISS](kiss.md)
- [Windows philosophy](windows_philosophy.md)
- [Windows philosophy](windows_philosophy.md)
- [hacking](hacking.md)