{"id":12073,"date":"2021-11-18T05:45:55","date_gmt":"2021-11-18T05:45:55","guid":{"rendered":"https:\/\/linuxways.net\/?p=12073"},"modified":"2024-03-25T02:10:32","modified_gmt":"2024-03-25T02:10:32","slug":"bat-command-in-linux-alternative-to-cat-command","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/bat-command-in-linux-alternative-to-cat-command\/","title":{"rendered":"bat command in Linux &#8211; alternative to cat command"},"content":{"rendered":"<p>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.<\/p>\n<h2><a id=\"post-12073-_gm8y3iv4b6zc\"><\/a>Some of the features of bat commands are pointed below.<\/h2>\n<ul>\n<li><strong>Git integration: <\/strong>bat communicates with git to show modifications.<\/li>\n<li><strong>Syntax highlighting: <\/strong>bat supports syntax highlighting for a large number of programming and markup languages.<\/li>\n<li><strong>Automatic paging: <\/strong>bat is able to pipe its own output to less, if in case the output is too large for one screen.<\/li>\n<li>Command line interface is very user-friendly.<\/li>\n<\/ul>\n<p>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.<\/p>\n<h2><a id=\"post-12073-_fpi5bmj6ch7k\"><\/a>Installation of bat command on Ubuntu<\/h2>\n<p>Bat package can be installed from the package manager with the following command.<\/p>\n<pre>\r\n$ sudo apt install bat\r\n<\/pre>\n<p>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.<\/p>\n<pre>\r\n$ wget https:\/\/github.com\/sharkdp\/bat\/releases\/download\/v0.18.3\/bat-musl_0.18.3_amd64.deb\r\n\r\n$ sudo dpkg -i bat-musl_0.18.3_amd64.deb\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"173\" class=\"wp-image-12080\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-272.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-272.png 662w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-272-300x78.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/p>\n<h3><\/h3>\n<h2><a id=\"post-12073-_j7xme07opr6c\"><\/a>Use cases of bat command on Linux<\/h2>\n<p>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.<\/p>\n<h4><a id=\"post-12073-_yu2ogpqbouhq\"><\/a>To display contents of single file<\/h4>\n<pre>\r\n$ bat filename\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\n$ bat test.txt\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"135\" class=\"wp-image-12081\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-273.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-273.png 618w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-273-300x66.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/p>\n<h4><a id=\"post-12073-_gxligyagfp0z\"><\/a>To display contents of multiple files at once<\/h4>\n<pre>\r\n$ bat &lt;file1&gt; &lt;file2&gt;\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\n$ bat \/etc\/hosts test.txt\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"364\" class=\"wp-image-12082\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-274.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-274.png 731w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-274-300x149.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<h4><a id=\"post-12073-_tztmmzdbuo3d\"><\/a>To display all supported languages<\/h4>\n<pre>\r\n$ bat --list-languages\r\n<\/pre>\n<h4><\/h4>\n<h4><a id=\"post-12073-_mm7c0hsmteer\"><\/a>To append the contents of different files on a single file<\/h4>\n<pre>\r\n$ bat file1 file2 &gt; singlefile\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\n$ bat test.txt app.txt &gt; document.txt\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"169\" class=\"wp-image-12083\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-275.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-275.png 600w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-275-300x85.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/strong><\/p>\n<h4><a id=\"post-12073-_rglg9xrpfstn\"><\/a>To create new file<\/h4>\n<pre>\r\n$ bat &gt; filename\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\n$ bat &gt; hello.txt\r\n<\/pre>\n<h4><a id=\"post-12073-_ud8wxo89v7la\"><\/a>To print specific range of lines using \u201c&#8211;line-range\u201d switch<\/h4>\n<pre>\r\n$ bat --line-range range_from:range_to filename\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\n$ bat --line-range 2:4 \/etc\/hosts\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"719\" height=\"171\" class=\"wp-image-12084\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-276.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-276.png 719w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-276-300x71.png 300w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/strong><\/p>\n<h4><a id=\"post-12073-_qzol3mpq3ajo\"><\/a>To check the line numbers using \u201c-n\u201d flag<\/h4>\n<pre>\r\n$ bat -n filename\r\n<\/pre>\n<p>Example<\/p>\n<pre>\r\n$ bat -n zigzag.py\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"822\" height=\"309\" class=\"wp-image-12085\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-277.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-277.png 822w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-277-300x113.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-277-768x289.png 768w\" sizes=\"auto, (max-width: 822px) 100vw, 822px\" \/><\/p>\n<h4><a id=\"post-12073-_aqc9tkamxsfj\"><\/a>To set a language for syntax highlighting<\/h4>\n<pre>\r\n$ bat filename\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\n$ bat zigzag.py\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"823\" height=\"564\" class=\"wp-image-12086\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-278.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-278.png 823w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-278-300x206.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-278-768x526.png 768w\" sizes=\"auto, (max-width: 823px) 100vw, 823px\" \/><\/p>\n<h4><\/h4>\n<h4><a id=\"post-12073-_8mf4dh3eulwv\"><\/a>To check the bat command themes for syntax highlighting<\/h4>\n<pre>\r\n$ bat --list-themes\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"839\" height=\"583\" class=\"wp-image-12088\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-279.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-279.png 839w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-279-300x208.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-279-768x534.png 768w\" sizes=\"auto, (max-width: 839px) 100vw, 839px\" \/><\/p>\n<h4><a id=\"post-12073-_r3a4x46yyha7\"><\/a>To change the bat command theme for syntax highlighting<\/h4>\n<pre>\r\n$ bat --theme=ansi filename\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\n$ bat --theme=ansi zigzag.py\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"564\" class=\"wp-image-12089\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-280.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-280.png 808w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-280-300x209.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-280-768x536.png 768w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/p>\n<p>To make these changes of themes permanent, write \u201cexport BAT_THEME=\u201dansi\u201d on ~\/.bashrc for user specific or \/etc\/bash.bashrc for system wide.<\/p>\n<h2><a id=\"post-12073-_l6dp11wkcc6r\"><\/a>Conclusion<\/h2>\n<p>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 \u201cman bat\u201d for more details.Thank you!<\/p>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>","protected":false},"author":110,"featured_media":12087,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,5,168,2],"tags":[361],"class_list":["post-12073","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-mint","category-scripting","category-ubuntu","tag-linux-commands"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=12073"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12073\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/12087"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=12073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=12073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=12073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}