Cat without cat on the commandline (2020)

(jarv.org)

24 points | by zoidb 12 hours ago ago

23 comments

  • basemi 11 hours ago

    The main purpose of `cat` is not to print a single file to stdout but to conCATenate files to stdout [0]. Of course if I have a single file it's printed alone. Just here to say that `cat` is not a synonym of `print`, but nowadays it's "cat FILE" everywhere. Even in APIs... [1] (Compact and aligned text? XD)

    [0] https://www.man7.org/linux/man-pages/man1/cat.1.html [1] https://www.elastic.co/guide/en/elasticsearch/reference/curr...

    • saagarjha 11 hours ago

      Yes and what I want to do is concatenate 1 file to stdout

      • oneeyedpigeon 10 hours ago

        And you don't want to use a pager because you know the file is shorter than one screen?

        • scbrg 10 hours ago

          pagers are sometimes more of a burden than a help. If a file is relatively short, the terminal's scrollback may be sufficient to navigate the content. A pager may not cooperate well with the terminal's UI for scrolling, and may even mess up scrollback history. And, depending on configuration, may clear the screen when it's closed against the user's wishes.

          Yes, there may be flags you can throw on them to make them behave the way you want, but those flags need to be remembered. `cat` is certainly shorter to type and easier to remember than `less -X`

          • oneeyedpigeon 10 hours ago

            What about syntax highlighting? If you're only interested in viewing the file anyway, there's a decent chance that would improve your experience.

            I do accept that `cat file` is not a problem, but it's interesting to discuss alternatives anyway!

        • saagarjha 10 hours ago

          I have infinite scrollback and ⌘F

  • silisili 11 hours ago

    Shell scripting is one of those places where readability is king, and premature optimization is the devil.

    Yes, there are multiple ways to get by without cat. Are they as obvious to understand? If not, what are we doing?

    Shell scripting isn't meant for performance but composability. If you're fighting me over micro optimizations, I'm just going to rewrite it in a proper language and 100x its performance. But then it's lost its readability and it's no longer editable.

    Balance, like most things in life.

  • jwilk 11 hours ago

    Related: https://github.com/wader/catgolf (might not be completely serious)

  • ilyagr 11 hours ago

    Took me a bit to figure out a nice way to do this in the `fish` shell. I think it's `string collect < file.txt`.

  • ChrisArchitect 10 hours ago
  • vman81 11 hours ago

    If I didn't have manpages or the option to google I'd probably just use head or tail after doing 'wc -l' to determine line count.

    head filename -n `wc -l filename`

    • Calzifer 10 hours ago

      In most cases you could simply use a huge number of lines like

        head filename -n 1000000000
      
      or use the "inverse mode". Head and tail can print all but the first/last n lines (I just have trouble to remember without man page the syntax and which does what). So those are more "cat equivalents":

        head -n-0 filename
        tail -n+0 filename
      
      PS: for your wc example you could actually do just

        head -n $(wc -l filename)
      
      because `wc` will print the number of lines and the filename. (does not work with spaces)
      • 10 hours ago
        [deleted]
  • its-summertime 11 hours ago

    the read/echo loop will not handle nulls

    the command substitution will mangle ending newlines

  • optymizer 11 hours ago

    On what kind of Linux system do you get a shell but don't have access to `cat`?

    • scrapheap 11 hours ago

      A damaged one you're trying to recover :)

    • levidos 11 hours ago

      Hardened container images

  • 12 hours ago
    [deleted]
  • 10 hours ago
    [deleted]
  • 10 hours ago
    [deleted]
  • greatgib 11 hours ago

    [flagged]

    • pvg 10 hours ago

      This sort of meta is not really on topic, if you think there's abuse or some other problem you can mail hn@ycombinator.com and tell/ask them.

    • y0ned4 11 hours ago

      the same here XD