How to Use the ‘bat’ Command in Linux

Get to know the 'bat' command in Linux for a visually appealing way to interact with text files.

The bat command in Linux, short for “Basic Attention Token,” offers a more visually appealing way to view file contents compared to the traditional cat command. It provides syntax highlighting, line numbering, and a host of other features that make reading files a breeze. Similar to commands like less and more, the bat command enhances the user’s ability to interact with text files.

Primarily used by system admins, developers, and data analysts, the bat command proves handy for those who frequently work with text files in the Linux environment. It’s not only used for viewing files but also for concatenating and displaying them. For a more streamlined workflow, the bat command can be used alongside tools like grep to search within files or awk for text processing.

How to Install the bat Command

bat is not included by default in most Linux distributions, so you’ll need to install it. Here’s how you can install and uninstall bat on some popular Linux distributions:

Debian/Ubuntu:

Install:

sudo apt update
sudo apt install bat

Uninstall:

sudo apt remove bat

Fedora:

Install:

sudo dnf install bat

Uninstall:

sudo dnf remove bat

Arch Linux:

Install:

sudo pacman -S bat

Uninstall:

sudo pacman -R bat

CentOS:

For CentOS, bat might not be available in the default repositories. You can use the cargo package manager (Rust’s package manager) to install it.

Install:

First, install Rust and Cargo:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Then, install bat using Cargo:

cargo install bat

Uninstall:

cargo uninstall bat

After installing, you might need to ensure that the installation path for Cargo’s binaries (usually $HOME/.cargo/bin) is in your PATH to access the bat command.

How to Use bat

1. Viewing a File with Line Numbers

Syntax: bat

Explanation: Displays the content of a file with line numbers.

Example: bat example.txt

Output:

1 │ This is line 1
2 │ This is line 2
3 │ This is line 3

The output displays the content of example.txt with line numbers.

2. Syntax Highlighting for a Specific Language

Syntax: bat --language

Explanation: Forces syntax highlighting for a specific language.

Example: bat --language=python example.py

Output:

1 │ def main():
2 │     print("Hello, World!")

The output shows the content of example.py with Python syntax highlighting.

3. Displaying Only Specific Lines

Syntax: bat --line-range

Explanation: Displays only specific lines or line ranges of a file.

Example: bat --line-range=2:4 example.txt

Output:

2 │ This is line 2
3 │ This is line 3
4 │ This is line 4

The output shows lines 2 to 4 of example.txt.

4. Using a Specific Theme

Syntax: bat --theme

Explanation: Displays the content of a file using a specific theme.

Example: bat --theme=TwoDark example.txt

Output:

1 │ This is line 1
2 │ This is line 2

The output shows the content of example.txt using the TwoDark theme.

5. Integrating with Git

Syntax: bat --diff

Explanation: Displays the file differences with Git integration.

Example: bat --diff example.txt

Output:

1 │-This is the old line
2 │+This is the new line

The output shows the differences in example.txt compared to the version in Git, with removed lines marked with - and added lines marked with +.

6. Concatenating Multiple Files

Syntax: bat

Explanation: Displays the content of multiple files concatenated.

Example: bat file1.txt file2.txt

Output:

file1.txt

1 │ Content of file1
2 │ More content of file1

file2.txt

1 │ Content of file2

The output shows the concatenated content of file1.txt and file2.txt.

7. Displaying File with a Specific Tab Width

Syntax: bat --tabs

Explanation: Displays the content of a file with a specific tab width.

Example: bat --tabs=4 example.txt

Output:

1 │    This line starts with a tab
2 │    So does this one

The output shows the content of example.txt with tabs expanded to 4 spaces.

8. Showing Non-Printable Characters

Syntax: bat --show-all

Explanation: Displays all non-printable characters in the file.

Example: bat --show-all example.txt

Output:

1 │ This is line 1$
2 │ This is line 2$

The output shows the content of example.txt with non-printable characters (e.g., end-of-line $).

9. Suppressing Line Numbers

Syntax: bat --plain

Explanation: Displays the content of a file without line numbers or decorations.

Example: bat --plain example.txt

Output:

This is line 1
This is line 2

The output shows the plain content of example.txt without any line numbers or decorations.

10. Previewing Markdown Files

Syntax: bat --pager

Explanation: Previews a Markdown file with syntax highlighting and pagination.

Example: bat --pager="less -R" example.md

Output:

1 │ # Heading
2 │ - List item 1
3 │ - List item 2

The output previews the content of example.md with Markdown syntax highlighting and pagination using less.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail