Bat is a Modern Drop-in Replacement for Cat Command on Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

The bat is a modern command-line program written in the Rust programming language, and I can confidently say it’s a great drop-in replacement for our beloved cat command.

If you’re wondering why I’m hyping this so much and what it has to offer you that makes it worth replacing the cat with, then do check out its features below.

Features of Bat

Bat really has some cool features to offer, which are mentioned below.

  • Supports syntax highlighting for a large number of programming and markup languages.
  • Detect and display Git changes.
  • Pipe output to its own pager (e.g., “less“) if the output is too large for one screen.
  • Display non-printable characters such as spaces, tabs, line breaks, etc.
  • Offer multiple themes that can be easily configured using flags or environments.
  • A user-friendly command-line interface that, of course, inherits all the functionality of the “cat” command.

Now let’s see how you can install the bat on your favorite Linux distribution.

How to Install Bat on Linux

The “bat” installation is quite easy if you’re using any Linux distribution; to its latest release, you can easily install it from their default package manager.

Below, I’ve provided the commands for the most popular Linux distribution (including Windows and macOS) that you can use right away to install it on your existing system.

$ sudo apt install bat                                                                      #On Debian, Ubuntu, Mint, Pop!_OS, etc
$ dnf install bat                                                                                #On RedHat, Fedora, AlmaLinux, etc
$ pacman -S bat                                                                              #On Arch, Manjaro, EndeavourOS, etc
$ apk add bat                                                                                   #On Alpine Linux
$ emerge sys-apps/bat                                                                  #On Funtoo and Gentoo Linux
$ xbps-install -S bat                                                                        #On Void Linux
$ pkg install bat                                                                                #On Termux and FreeBSD
$ pkg_add bat                                                                                   #On OpenBSD
$ nix-env -i bat                                                                                 #On Nix
$ zypper install bat                                                                          #On openSUSE
$ brew install bat                                                                             #On Linux or macOS using Brew
$ port install bat                                                                               #On macOS using port
$ winget install sharkdp.bat                                                           #On Windows using Winget
$ choco install bat                                                                           #On Windows using Choco
$ scoop install bat                                                                           #On Windows using Scoop

Once the installation is complete, you can use the “bat” or “batcat” command.

How to Use Bat on Linux

To view the file content, you can use the “bat” or “batcat” command. Now, if you’re wondering why I’m referring to two commands, remember that in some Linux distributions, there is an existing “bat” package. To avoid confusion on those systems, the command name will be “batcat” instead.

So, to read the content of any file, you can use the following command:

$ bat {filename or /path/to/the/file}

#OR

$ batcat {filename or /path/to/the/file}

Below, I’m viewing the “~/.bashrc” file using bat, and you can see how beautifully it colored the syntax, numbered the lines, and presented the file in a unique, modern way.

viewing bashrc file using bat

The following is a picture of a Python file viewed using bat.

viewing python file using bat

By default, bat uses “less” to paginate the output (in simple terms, you can view the file by scrolling). However, you can also force bat to print the output to the command-line, just like a “cat” command does, using the “--paging=never” flag.

$ bat --paging=never ~/.bashrc

#OR

$ batcat --paging=never ~/.bashrc

Output:

viewing bashrc file using bat without paging

To force “bat” to display non-printable characters like spaces, tabs, line breaks, etc., making them easier to track, you can use the “--show-all” flag alongside “bat” to display and highlight them.

$ bat --show-all ~/.bashrc

#OR

$ batcat --show-all ~/.bashrc

Output:

viewing the bashrc file with non printable characters using bat

To disable decorations like header and line numbering in the sidebar, you can use the “-p” flag.

$ bat -p ~/.bashrc

#OR

$ batcat -p ~/.bashrc

Output:

disabling decorators in bat output

Bat is also integrated with Git. So, if you have an existing file continuously monitored for changes by Git, you can make slight changes to it and then view the files using bat to see Git annotations on the modified lines.

viewing git changes of modified file using bat

To view a specific range (or portion) of lines from the file, you can use the “-r” flag with the start and end line numbers that you desire to show in the output.

#The following command will only show lines from 25 to 35.
$ bat -r 25:35 ~/.bashrc
$ batcat -r 25:35 ~/.bashrc

#The following command will only print lines from 1 to 20.
$ bat -r :20 ~/.bashrc
$ batcat -r :20 ~/.bashrc

#The following command will show lines from 20 to EOF.
$ bat -r 20: ~/.bashrc
$ batcat -r 20: ~/.bashrc

Bat comes with a bunch of themes for syntax highlighting that you can easily list using the following command:

$ bat --list-themes

#OR

$ batcat --list-themes

Output:

viewing the themes for bat

To apply any theme, for example, I really enjoy viewing my files using the “Dracula” theme. So, to view my file with this theme using the “bat” command, I can use the ‘--theme="Dracula"‘ flag each time to view my file in this theme.

$ bat --theme="Dracula" ~/.bashrc

#OR

$ batcat --theme="Dracula" ~/.bashrc

To permanently apply theme changes, export the “BAT_THEMEenvironment variable with the theme name at the end of your shell configuration file (e.g., “~/.bashrc” for Bash).

export BAT_THEME="Dracula"

That’s it. The “Dracula” theme will now appear whenever you view the file using a bat.

Final Word

Here comes the end of the article. I hope you truly enjoyed reading it and discovered an amazing new tool. If you’d like to dig deeper, you can explore its help or manual page, or simply reach out to us in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.