CentOS Mint Scripting Ubuntu

bat command in Linux – alternative to cat command

bat command in Linux

In linux, you must be familiar with cat commands. It is used often to print and concatenate files. Bat command is a clone of cat command which comes with many attractive and useful features. Some of the features are syntax highlighting, automatic paging, git integration and so on, that enhances the experience of programmers too.

Some of the features of bat commands are pointed below.

  • Git integration: bat communicates with git to show modifications.
  • Syntax highlighting: bat supports syntax highlighting for a large number of programming and markup languages.
  • Automatic paging: bat is able to pipe its own output to less, if in case the output is too large for one screen.
  • Command line interface is very user-friendly.

In this article, we will install and show the uses and features of bat command. Here, we are going to install it on Ubuntu 20.04 LTS server.

Installation of bat command on Ubuntu

Bat package can be installed from the package manager with the following command.

$ sudo apt install bat

But it can install any version of bat package so to make sure the bat package is the latest one, we are downloading it from the Github release page. You can simply search the page or run the following command as shown below.

$ wget https://github.com/sharkdp/bat/releases/download/v0.18.3/bat-musl_0.18.3_amd64.deb

$ sudo dpkg -i bat-musl_0.18.3_amd64.deb

Use cases of bat command on Linux

As the installation of the bat package is completed, we can simply use it with the bat command. Some of the examples are shown below.

To display contents of single file

$ bat filename

Example:

$ bat test.txt

Output:

To display contents of multiple files at once

$ bat <file1> <file2>

Example:

$ bat /etc/hosts test.txt

Output:

To display all supported languages

$ bat --list-languages

To append the contents of different files on a single file

$ bat file1 file2 > singlefile

Example:

$ bat test.txt app.txt > document.txt

Output:

To create new file

$ bat > filename

Example:

$ bat > hello.txt

To print specific range of lines using “–line-range” switch

$ bat --line-range range_from:range_to filename

Example:

$ bat --line-range 2:4 /etc/hosts

Output:

To check the line numbers using “-n” flag

$ bat -n filename

Example

$ bat -n zigzag.py

Output:

To set a language for syntax highlighting

$ bat filename

Example:

$ bat zigzag.py

Output:

To check the bat command themes for syntax highlighting

$ bat --list-themes

Output:

To change the bat command theme for syntax highlighting

$ bat --theme=ansi filename

Example:

$ bat --theme=ansi zigzag.py

Output:

To make these changes of themes permanent, write “export BAT_THEME=”ansi” on ~/.bashrc for user specific or /etc/bash.bashrc for system wide.

Conclusion

Bat is a clone of cat command which comes with many features as shown in the above examples. You can try using other themes too as per your preference. Explore the bat command with “man bat” for more details.Thank you!

Similar Posts