Bat is Like the cat Command in Linux, But Super-Charged and Written in Rust

Bat is a cat command clone with advanced syntax highlighting for a large number of programming and markup languages.

Despite the title of this article, we’ll not talk about cats and bats here but about the cat and bat commands in Linux.

As you know, the cat (short for concatenate) command is a utility in Linux. One of its most commonly known usages is to print the content of a file onto the standard output stream. But given more time spent in the command line, features like syntax highlighting are convenient.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

What’s bat Command

Bat is a drop-in replacement for the cat command, with additional cool features such as syntax highlighting, git integration, and automatic paging.

By default, bat pipes its output to a pager (e.g., less) if the output is too large for one screen.

However, if you would instead bat work like the cat command all the time (never page output), you can set --paging=never as an option, either on the command line or in your configuration file.

The bat command also allows you to search during output (if the output is longer than the screen height) using the / key binding, similarly to less searching.

Bat uses the syntect library for syntax highlighting. Syntect is a popular syntax highlighting library for Rust that uses Sublime Text syntax definitions.

It is possible to alias cat directly to bat in your shell configuration. To do this, add the following line to your ~/.bashrc file:

alias cat='bat --paging=never'Code language: JavaScript (javascript)

Then source the file:

source ~/.bashrc

Like cat command, bat works out-of-the-box on Linux. No extra configuration is needed. So, to use it, all you have to do is type:

bat filename

The bat command receives our strongest recommendation. It is a helpful utility that you’ll wonder how you managed without it.

Bat Highlights

  • Syntax highlighting: Bat supports syntax highlighting for many programming and markup languages.
  • Git integration: Bat communicates with git to show modifications with respect to the index.
  • Automatic paging: The command can pipe its own output to less if it is too large for one screen.
  • Display and highlight non-printable characters: You can use the -A option to show and highlight non-printable characters.
  • Including all of the cat command functionality.
The bat Linux Command Compared to cat

You see the difference. The cat command shows the file’s contents in plain text format, whereas the bat command shows the output with syntax highlighting and ordering row numbers in a neat tabular column format. 

Those interested in learning more about the bat command in Linux can visit the project’s GitHub page.

Installing the bat Command in Linux

We should utilize the system package manager. Let’s look at several well-known Linux distribution examples for installing the bat command.

If your Ubuntu / Debian installation is new enough, you can run:

sudo apt install bat

Arch Linux users can install the bat package from Arch’s official repo:

sudo pacman -S bat

To install the bat command on Fedora, type:

sudo dnf install bat

You can install bat with zypper on openSUSE:

sudo zypper install bat

Conclusion

This short article covered how to install and use the bat command on Linux. In my opinion, the bat command is an excellent modern tool for Linux users. So, give it a shot and let us know what you think.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%