The bat command works just like the cat command does on Linux. The difference is that the bat command is more modern, boasting a more readable design and features like Git integration and syntax highlighting.
In this brief guide, we compare the bat command to its predecessor, the cat command, and walk you through installing and using the command on your machine.
Prerequisites
You will need a Linode account, and a compute instance to follow this tutorial. Ensure you select a limited Linode user account and set the hostname and time zone. It’s best practice to harden SSH access.
Also, bear in mind that this guide is written for non-root users. The commands that require root access have the prefix “sudo.”
Comparing the Bat and Cat Commands
The Linux cat command is one of the most basic commands and often one that Linux users learn early on. The command comes included in virtually every distro and is typically used to view a file’s contents from the command line.
The bat command is just as easy to use on the command line as the cat command. Like the cat command, the bat command also displays a file’s content. However, it has a more modern look and several added features.
The feature that attracts most users to this command is the syntax highlighting feature. This feature, coupled with the easier-to-read formatting, graphical non-printing characters, and pagination, makes the bat command a better choice over the cat command for reading files containing code.
You can integrate the command with most command-line tools, including Git. When a file is tracked on Git, you can use the bat command to see annotations highlighting a file’s modified lines. Furthermore, you can access the old versions of the files with the command under version control.
How to Install the bat Command on Linux
Most Linux distros, including Fedora, Debian, and Ubuntu, offer the bat command on the package manager. You can run the following command to install bat on Debian and Ubuntu:
sudo apt install bat |
Note that on these distros, you must use the “batcat” command to use the bat command due to a conflict with an existing package called bacula-console-qt.
But you can link the “batcat” command to a “bat” command on your machine by running the following:
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat |
Ensure that the bacula-console-qt package is not installed on your machine before running the above commands.
If you have the package installed and often use it, you will need to stick to using the “batcat” command.
On the Fedora distro, run the following to install the bat command:\
sudo dnf install bat |
CentOS and AlmaLinux are other popular distros, and if you’re running either of these, you will need to take a few additional steps to install the bat command on your machine.
Install tar on your machine if you don’t have it already installed by running the following:
sudo yum install tar |
You will need tar to extract the bat package.
Here are the steps to downloading and installing the latest bat command release and installing it:
- Determine your machine’s CPU architecture by running:
uname -a |
2. Let’s say your CPU architecture is x86_64. You must find the latest version of the bat package for your CPU’s architecture on its release page. Find the .tar.gz package having the text “unknown-linux-musl” right before the file extension. Copy the URL of the appropriate package.
3. Download the package by running the following command:
curl -o bat.zip -L https://github.com/sharkdp/bat/releases/download/v0.18.2/bat-v0.18.2-x86_64-unknown-linux-musl.tar.gz |
4. Next, extract the bat command’s package with this command:
tar -xvzf bat.zip |
5. Now, move the extracted files to /usr/local/ like so:
sudo mv bat-v0.18.2-x86_64-unknown-linux-musl /usr/local/bat |
Finally, add an alias for the bat command to your .bashrc file. The file is located in your home directory in most distros. To create the alias, add the following text at the end of the file:
alias bat=”/usr/local/bat/bat” |
To verify that the bat command is installed on your machine, run the bat command with the –version flag like so:
bat –version |
If the output indicates the version you installed, the command has been installed on your machine.
How to Use the bat Command on Linux
The bat command works similarly to the cat command – it expects you to enter a path to the file you want to view after the command.
Let’s explore the bat command’s features with the Flask-RESTful project on GitHub. If you don’t have Git installed, run:
sudo apt install git |
If you’re using Fedora, replace “apt” with “dnf,” and if you’re on CentOS or AlmaLinux, replace “apt” with “yum.”
Next, you must clone the repository like so:
git clone https://github.com/flask-restful/flask-restful.git |
Now, to see the Python files in the repository, you can use the bat command like so:
bat flask-restful/examples/todo.py |
Interestingly, the bat command uses the “less” setting by default to paginate its output. However, you can instruct the command to print to the terminal directly like the cat command. All you have to do is set the –paging flag to “never,” like so:
bat –paging=never flask-restful/examples/todo.py |
The bat command supplies the option to show non-printing characters like the cat command. With this option, you can easily track spaces, tabs, etc. Additionally, the bat command allows you to use special characters to represent non-printing characters. Here’s how you do this:
bat –show-all flask-restful/examples/todo.py |
Git Integration
You can use the bat command with Git without much hassle. To see it in action, open the todo.py file we have been using with any text editor and modify it. Close it and open it again, this time using the bat command.
You will notice that the bat command offers some Git annotations in the lines you have modified. What’s more interesting is that you can use the bat command with Git to view even older versions of the files in any Git repo.
Customizing Syntax Highlighting in the bat Command
The bat command offers a lot of flexibility with the syntax. You can add support for a programming language, pick a custom color palette, and use various customization options.
Interestingly, the bat command comes with several themes for syntax highlighting built-in. To see a list of the available themes and what they look like, you can run the following command:
bat –list-themes |
You will see a list of themes and samples of what the code looks like. There are several ways to apply a theme you find interesting. You can:
- Run the bat command with the –theme flag and set your desired theme. Note that you must use this flag every time you run the bat command, so the output appears in your preferred theme. Here’s what the flag looks like in action when used this way:
bat –theme=”Solarized (dark)” ~/flask-restful/examples/todo.py |
2. Set the “BAT_THEME” environment variable to the theme you’re interested in. Doing this will set the theme for the shell session. When the session is over, the theme will reset to the default. Here’s how you set the BAT_THEME variable:
export BAT_THEME=”Solarized (dark)” |
3. You can modify the BAT_THEME variable in your .bashrc file to set a theme for all your shell sessions. After navigating to the file, run:
export BAT_THEME=”Solarized (dark)” |