×


bat command in Ubuntu 20.04 LTS Linux system – alternative to cat command

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.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux system commands queries.

In this context, we shall look into how to install and use bat command. 


Main features of bat commands:

  • Syntax highlighting: Bat supports syntax highlighting for a large number of 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 the output 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.


How to install bat command on Ubuntu Linux system ?

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:

$ 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


Examples of using bat command on Linux

1. To display contents of single file

$ bat filename

Example:

$ bat test.txt


3. To display contents of multiple files at once

$ bat <file1> <file2>

Example:

$ bat /etc/hosts test.txt


4. To display all supported languages:

$ bat --list-languages


5. To append the contents of different files on a single file:

$ bat file1 file2 > singlefile

Example:

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


6. To create new file:

$ bat > filename

Example:

$ bat > hello.txt


7. 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


8. To check the line numbers using "-n" flag:

$ bat -n filename

Example

$ bat -n zigzag.py


9. To set a language for syntax highlighting:

$ bat filename

Example:

$ bat zigzag.py


10. To check the bat command themes for syntax highlighting:

$ bat --list-themes


11. To change the bat command theme for syntax highlighting

$ bat --theme=ansi filename

Example:

$ bat --theme=ansi zigzag.py

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


[Need help in fixing Linux issues ? We can help you. ]


Conclusion

This article covers the best way of installing and using bat Command in Linux system. In fact, Bat is a drop-in replacement for the cat command, with some additional cool features such as syntax highlighting, git integration and automatic paging.