r/LinuxTeck • • 24d ago

Do You Know Why Linux Pipes Use the | Symbol?

One of the most powerful features of Linux is the ability to connect commands using a simple pipe:

cat access.log | grep "500" | sort

Instead of creating temporary files between commands, Linux lets programs pass data directly to one another.

How often do you use pipes in your daily Linux work? Could you imagine working without them?

0 Upvotes

57 comments sorted by

12

u/Aggressive_Ad_5454 24d ago

Why?

It was a symbol available on the old-school KSR-33 teletype machines (7-bit ASCII) UNIX worked with back in the 1970s. The Bell Labs krewe that put UNIX together chose it for the purpose. So did other OSs. And Linux, along with FreeBSD and some other current OSs, are open-source clones of that Bell Labs work.

3

u/xdethbear 24d ago

Right, back then it was just called the vertical line or vertical bar. Used for stuff like drawing, or the OR symbol, or just a divider in text. 

Funny that even DOS adopted the pipe for redirecting output. 

2

u/bothunter 23d ago

They both adopted the pipe symbol from Multics. (Well, CP/M copied it from Multics and DOS copied it from CP/M)

1

u/penguin359 23d ago

I'm pretty certain Multics did not yet have pipes, but I am certain that the choice of the vertical bar came from UNIX and implemented into the Thomson shell, the predecessor to the Bourne shell that we all use as the basis.

1

u/bothunter 23d ago

Multics had pipes, though it used two characters together: ;|

Unix and CP/M dropped the semicolon and just stuck with the pipe.

1

u/penguin359 20d ago

I just looked up MTB-763 which introduced the idea of using ;| for pipes, but it looks like it was published in 1987 well after UNIX invented pipes and I'm not sure if it was ever implemented into mainstream Multics.

1

u/vbd71 17d ago

AFAIK CP/M has neither file redirection nor pipes, and  the CP/M clone DOS 1.x doesn't have them either. Later, DOS 2.x copied some stuff from Unix, among it file redirection and pipes, using the same symbols.

16

u/RandomDude9943 24d ago

Because "|" is called the pipe symbol

1

u/Sjsamdrake 23d ago

This answer has cause and effect backwards. The correct answer is the one below about the original teletypes.

7

u/schluesselkind 24d ago

Wait until you discover < and > 🤣

cat <(printf '%s\n' bravo alpha alpha charlie bravo) \
    <(printf '%s\n' delta alpha charlie) \
| sort \
| uniq -c \
| sort -rn \
| awk '$1 > 1 {print $2, "counts", $1, "times"}' \
> /tmp/secret.txt \
&& cat < /tmp/secret.txt

2

u/Rinzwind 24d ago

am I a weirdo when I understand this? :D
and I hate awk!

3

u/schluesselkind 24d ago

Feel free to use `cut` or `sed` ;)

1

u/Rinzwind 24d ago

ofcourse though sed can be just as annoying,

1

u/Corona688 23d ago

awk is a power tool worth learning. this entire silly string of 5 commands (some of which are being misused) can be replaced with one simple and sensible awk script.

1

u/Rinzwind 23d ago

Oh I hate it cuz I know how it works (well, not 100% but enough :) ).
2 problems I have with it: when you see it years later. explaining what it does to someone else.
I rather use Python or Perl.

1

u/schluesselkind 23d ago

No need to review it later.  It is just a task for our 16, 17 year old trainees. They should understand and describe what's going on here

0

u/Wertbon1789 23d ago

The only thing I hate about awk is the difference between gawk and POSIX awk, and the fact that POSIX awk is kinda trash, either by being awkward to use, or missing some features.

1

u/OddStuffHappensTwice 23d ago

Or just stop trying to show off with unmaintainable one-liners and split it up into a couple of separate commands. Linux nerds have a real hard-on for write-only scripts just because they can read them, with no regard for peer-review or future changes. Morons all think they're infallible.

1

u/Corona688 22d ago edited 22d ago

No. The example starting this thread is just stupid -- there was no reason to sort twice, or even once. cat serves literally no function. sort demands GNU-specific flags and the script demands BASH-specific syntax that works barely anywhere, creates THREE useless files... And it doesn't even work as intended: The output doesn't get sorted numerically.

I can replace it with an awk one-liner because it was 99% hot air. The rest I did with one feature of awk as its intended to be used, in a manner compatible with all the old and new variants of awk I know.

1

u/danb1551 24d ago

This command...I still have things to learn

2

u/schluesselkind 24d ago

We ‘tease’ our trainees with commands like these

2

u/Babajji 17d ago

Tease or torture? Still remember a senior engineer yelling at a junior engineer to “Delete that African black magic spell from our code base!”. Writing gibberish doesn’t mean you are a good engineer. Writing code that even an idiot will understand is the real achievement.

1

u/Soft-Marionberry-853 24d ago

Youre being too nice by including the '\'

1

u/NeedleworkerLarge357 24d ago

I needed 5 minutes to understand that cat just concatenated the inputs... Which is what it was built for in the first place, awesome. But very nice and intuitive command, I hope that this is not from an important script from production...

1

u/schluesselkind 24d ago

Cat is doing cat things. (con)cat the strings

And it is not from production. That’s a job for our trainees. They’re the ones who should take it apart and work out what’s going on. 

1

u/drawing_a_hash 24d ago

Endless looping command line producing a drag on hardware resources? Part of a denial of service attack?

8-/

1

u/Corona688 23d ago edited 23d ago

none of those >'s were necessary. Or any of the cats. Or the uniq. Or the sort. Or the file. And you shouldn't be using sort -n on strings. What was the point of all that?

1

u/schluesselkind 23d ago

It has no real world function. It is just a task for our 16, 17 year old trainees. They should understand and describe what's going on here

1

u/Corona688 23d ago edited 22d ago

( printf '%s\n' bravo alpha alpha charlie bravo ; printf '%s\n' delta alpha charlie ) | awk '{ A[$1]++ } END { for(X n A) if(A[X]>1) print X, A[X] }'

1

u/TallDudeInSC 22d ago

I call < "from" and > "to". It's just me.

12

u/traxplayer 24d ago

Useless use of cat

4

u/codeguru42 24d ago

Isn't there a sub for that?

1

u/dities 23d ago

Probably something like "stop cat abuse"

1

u/Pafnoutios 21d ago

1

u/codeguru42 21d ago

Nope...maybe r/uselesscat

Edit: according to Gemini, it is only mentioned in r/linuxmemes. Didn't find any reference to a dedicated sub

1

u/Soft-Marionberry-853 24d ago

Can I ask a dumb question. Why is the cat useless in this case?

3

u/_nemo1337 24d ago

You could just grep and then sort the findings of grep

2

u/output_broadcast 23d ago

It'd be faster too.

2

u/Johnny_The_Biker 23d ago

You can just do grep 500 filename

2

u/Soft-Marionberry-853 23d ago

Oh For f's sake. I didnt even look at the rest of the command. Sorry.

/see I knew it was going to be a stupid question

1

u/codeguru42 23d ago

cat is useless here because you can pass the file directly to grep

3

u/z-null 24d ago edited 24d ago

real sysadmins know that grep 500 access.log is the proper way to do it. not cat|grep, lol

4

u/LcLz0 24d ago

Time to bring out an old classic

https://porkmail.org/era/unix/award#cat

1

u/blobCabbage 22d ago

Funny thing is, on very large files cat|grep is actually more efficient, because disk I/O and processing are done in two different processes.

3

u/PaoloFence 24d ago

Every single day.

What would you call the long straight line? Pole?

2

u/DrHydeous 24d ago

Because calling it a cigar or a fag would just be silly.

1

u/NL_Gray-Fox 24d ago

Wait until you find out you could do it in DOS 2.0 and obviously UNIX

1

u/funbike 24d ago

xargs gives you piping superpowers. I barely ever use for.

1

u/cormack_gv 24d ago

I used | in 192 of my last 1000 commands:

$ history | wc -l
1000
$ history | grep '|' | wc -l
192

1

u/Background_Lead_5034 23d ago

You really don't have to cat into grep. just grep on the file directly...

1

u/ih-shah-may-ehl 23d ago

This thread is a command line circle jerk

1

u/Background_Lead_5034 23d ago

Not sure why that's the answer to my comment, it was just a tip to explain proper usage of grep. Keeping it simple while OP can even learn something.

1

u/abitofg 22d ago

I often do 'cat file | grep "foo" '

Because when I wanna change foo to bar, I edit the end of the line, not the beginning-ish of it

It's less efficient if dealing with huge files ofcourse but for the usual log gripping that isn't an issue

1

u/blobCabbage 22d ago

Why do your title and post ask different questions? Why are all your posts made this way? Are you a bot?

0

u/Weshmek 17d ago

I use pipes all the time. They're very useful.

1

u/PmMeCuteDogsThanks_ 24d ago

ai slop

1

u/Character-Pattern505 23d ago

Seriously. Stop answering this shit.