News for hackers. Stuff that matters.
Re:An Illustrated Guide to Useful Command Line Tools by wodenokoto on Saturday October 26, 2019 @09:17AM (#21363467)
Can someone explain the misuse of cat, which bat solves?
Re:An Illustrated Guide to Useful Command Line Tools by pdkl95 on Saturday October 26, 2019 @10:00AM (#21363705)
Simply using cat to dump a single file to STDOUT is technically a misuse because cat is theoretically intended to concatenate files. This 'misuse' is 'solved' by bat because bat seems to be primarily intended for its syntax highlighting and git-related features; according to bat's docs, cat-style concatenation of files is intended for drop-in compatibility with traditional cat.

That said, the idea that using cat to show a single file is "misusing" cat is prescriptivist rules-lawyering. There is no technical reason against using cat for non-concatenation purposes. The descriptivist interpretation of cat says using the tool for non-concatenation purposes is fine, since that's how people are actually using it.

However, note that cat is often misused as a substitute for STDIN redirection:

    # this creates an extra process that wastes
    # time copying STDIN to STDOUT
    cat "${inputfile}" | do_stuff

    # just connect inputfile directly to STDIN
    do_stuff <"${inputfile}"

    # or if you want to keep the input
    # at the start of the pipeline
    <"${inputfile}" do_stuff
Re:An Illustrated Guide to Useful Command Line Tools by arundelo on Saturday October 26, 2019 @09:33AM (#21363557)
I guess the author was thinking of how cat's original purpose is to concatenate multiple files, not show just one file. (But I certainly don't think using cat in the latter way is a misuse.)

There's also a commonly noted "unnecessary use of cat" where people do this:

  cat file.txt | grep foo
instead of this:

  <file.txt grep.foo
but that's not relevant to bat (which can be used unnecessarily in the same way).
Re:An Illustrated Guide to Useful Command Line Tools by weaksauce on Saturday October 26, 2019 @11:12AM (#21364122)
Or ‘grep foo file.txt’
Re:An Illustrated Guide to Useful Command Line Tools by Zhyl on Saturday October 26, 2019 @03:40PM (#21365729)
What's the name of the bash feature with the '<' before the filename? I want to read the docs on it but I don't even know what to search for.
Re:An Illustrated Guide to Useful Command Line Tools by detaro on Saturday October 26, 2019 @03:46PM (#21365763)
Re:An Illustrated Guide to Useful Command Line Tools by eurg on Saturday October 26, 2019 @11:41AM (#21364314)
Catting a file will your terminal make interpret escape codes in the file.

For instance:

    $ echo -e "\033]0;${USER} is an unfriendly person\007" > test-file.txt
Then, many days later:

    $ cat test-file.txt
will change your terminal title.

With less, you _can_ interpret escape codes, but usually you don't, and I consider this the correct default.

Re:An Illustrated Guide to Useful Command Line Tools by enriquto on Saturday October 26, 2019 @12:04PM (#21364463)
in your example it is not cat who "interprets" the escapes, but echo
Re:An Illustrated Guide to Useful Command Line Tools by eurg on Saturday October 26, 2019 @02:16PM (#21365279)
Neither one interprets the sequence, but cat will print the file unfiltered to stdout, and stdout is processed by your terminal, and then the terminal will interpret it. less filters before printing to the terminal.
Re:An Illustrated Guide to Useful Command Line Tools by enriquto on Saturday October 26, 2019 @04:50PM (#21366094)
I meant that "echo -e" transforms the four characters "\033" into a single byte, for example. Thus, the sequence is "interpreted" by echo. Then, the cat program just copies the bytes without looking at them. If you want "cat" to escape these bytes so that they are not seen by the terminal you can use the "-v" option.
Re:An Illustrated Guide to Useful Command Line Tools by kortilla on Saturday October 26, 2019 @10:54PM (#21367627)
The sequence that translates to an escape code is interpreted by echo. The escape code is passed unfiltered by cat for the terminal to interpret.
Re:An Illustrated Guide to Useful Command Line Tools by ISL on Saturday October 26, 2019 @12:00PM (#21364430)
Tried it -- didn't work. Single-quoting didn't work either. Terminal is urxvt.

What is clear is that

  $ cat test-file.txt
does not print the initially-echoed text, but it is escaped and up to nefarious tasks.
Re:An Illustrated Guide to Useful Command Line Tools by eurg on Saturday October 26, 2019 @02:15PM (#21365277)
The escape sequence is terminal specific. urxvt is likely different from xterm in this regard.
Re:An Illustrated Guide to Useful Command Line Tools by tazard on Saturday October 26, 2019 @11:59AM (#21364425)
That's a surprising and interesting bit if knowledge, thanks
Re:An Illustrated Guide to Useful Command Line Tools by joosters on Saturday October 26, 2019 @12:26PM (#21364584)
'less -R' is useful because it will honour colour control codes while escaping other ones (like title changes, terminal bell, etc)
Re:An Illustrated Guide to Useful Command Line Tools by jhbadger on Saturday October 26, 2019 @09:28AM (#21363528)
They are referring to using cat to print out a file to the terminal. I'm not sure that's a "misuse", but personally I've never done it, using "more" or "less" instead. "bat" really is a replacement for more/less, only with additional features such as syntax highlighting in source code.
Re:An Illustrated Guide to Useful Command Line Tools by johnisgood on Saturday October 26, 2019 @09:30AM (#21363539)
I use:

    cless() {
      pygmentize -O style=<style> "$1" 2>/dev/null | less
    }
I have programs that depend on pygmentize so I am not sure I will use bat anytime soon when I already have pygmentize and less, or at least not for it having syntax highlighting.
Re:An Illustrated Guide to Useful Command Line Tools by pcr910303 on Saturday October 26, 2019 @09:36AM (#21363576)
That use is the exact reason why LESSPIPE exists, check out man less & https://www-zeuthen.desy.de/~friebel/unix/lesspipe.html
Re:An Illustrated Guide to Useful Command Line Tools by johnisgood on Saturday October 26, 2019 @09:40AM (#21363594)
Maybe, but I already had pygmentize, I already had less, and this small function took me less than 10 seconds to type in and start using immediately (and I use a particular style that pygmentize provides so that is a plus). :P In any case, thanks for sharing.
Re:An Illustrated Guide to Useful Command Line Tools by pcr910303 on Saturday October 26, 2019 @09:59AM (#21363698)
Oh, I forgot to mention that LESSPIPE is a mechanism to automatically pipe the contents of a file when opening in less, and that the linked LESSPIPE script has an option to use pigmentize for syntax highlighting.

It’s a tool for enhancing less & pygmentize, not replacing them.

Well, just providing info for everyone. I’m not trying to force you using that script :-)

Re:An Illustrated Guide to Useful Command Line Tools by johnisgood on Saturday October 26, 2019 @10:13AM (#21363771)
> Well, just providing info for everyone. I’m not trying to force you using that script :-)

Yeah of course, sorry, I admit my comment did seem defensive! Thank you for sharing, really. :)

Re:An Illustrated Guide to Useful Command Line Tools by saagarjha on Saturday October 26, 2019 @09:37AM (#21363580)
I just use nano -v.
Re:An Illustrated Guide to Useful Command Line Tools by saagarjha on Saturday October 26, 2019 @09:26AM (#21363517)
Using cat to catenate a file to standard input isn't misuse, but it looks like bat performs syntax highlighting.
Re:An Illustrated Guide to Useful Command Line Tools by yesenadam on Saturday October 26, 2019 @09:57AM (#21363687)
Google "UUOC" - it's famous.

Edit: Sorry downvoter, thought I was being helpful - no-one mentioned the acronym yet. There's a lot online about it, more than I'm qualified to explain. Entertaining reading too. I remember reading about the UUOC Awards years ago..

Re:An Illustrated Guide to Useful Command Line Tools by spoiler on Saturday October 26, 2019 @09:25AM (#21363509)
I guess they just meant it as in "reading code," when in fact bat is better tool just for reading code.