vendredi 27 février 2015

How does `ls` know whether it's piped or printed


ls normally prints like this:



1 1249 1653 23 33 4581 6202 6447 789 836 903 config.gz kpageflags sysrq-trigger
10 1253 1658 24 34 4582 6206 648 79 837 91 consoles loadavg thread-self
1003 1255 1662 251 347 4583 6207 649 791 84 923 cpuinfo locks timer_list
1004 1257 1667 252 35 4689 6209 6799 8 840 925 crypto meminfo timer_stats
1005 1261 167 26 357 48 6232 6986 80 841 998 devices misc uptime
1006 1263 1670 266 36 49 6242 6992 802 846 acpi diskstats modules version
1008 13 1677 27 37 5 6267 6993 803 848 asound dma mounts vmallocinfo


But when you pipe it eg to cat with ls /proc | cat, it prints it as if it got -1 (works the same also with grep and other commands):



1
10
1003
1004
1005
1006
1008
1010
1012
106
107
1073
108
109
1152
117
118
1247
1249
1253


My question is, how does it know it's being piped instead of printed? Or, how it happens?



Aucun commentaire:

Enregistrer un commentaire