Testing Bash Scripts With Bats in Linux


There are countless applications for the widely used and beneficial scripting language bash. Despite the fact that the language itself is widely used, testing it is not as common. This may result in expensive mistakes and reduced trust in the code.

In this article, we are going to understand how to test bash scripts with the Bats in the Linux.

Note − Linux commands are case-sensitive.

What is BATS?

Bash Automated Testing System, sometimes known as BATS, is a testing framework. Before a bash program is released, it may be verified using this automated testing procedure that it is operating properly and meeting the targets it was designed to meet. As we'll see in this post, BATS is used to run tests on bash scripts and then gives the results.

BATS gives programmers the option to use bash scripts and libraries and perform automated testing procedures that are comparable to those used by programmers of Python or Java.

How to Set Up BATS?

Assuming you wish to launch your project from a git repository, we should first install "git" (if you don't already have it installed) in the Terminal.

$ sudo apt update
$ sudo apt install git
$ git version


$ git version 2.34.1

To set up your git repository, create it or go to the project directory and run the commands below. After that, install bats and its libraries by cloning the required repositories as submodules.

$ git init
$ git submodule add https://github.com/bats-core/bats-core.git test/bats
$ git submodule add https://github.com/bats-core/bats-assert.git   test/test_helper/bats-files
$ git submodule add https://github.com/bats-core/bats-support.git test/test_helper/bats-support
$ git submodule add https://github.com/bats-core/bats-assert.git test/test_helper/bats-assert

Configuring Your Bash Script

Create a new bash file first with the following code −

$ nano point.sh
#!/usr/bin/env bash

choice="Web Browser $1 over $2"
echo "$choice";echo "$choice" > $3
exit 0

The output from the previous command shows that our bash scripts take three arguments and output the value of the "choice" variable in the terminal as well as creating a text file with the same output. Let's put our program into action to better understand.

script.txt   point.sh   firefox   Bing   log23.txt   google


Web Browser firefox over Bing

Begin Testing Bash Scripts

Simple tests are the easiest to do in order to become comfortable with BATS. In the test directory, let's test a new file called "tutorial.bats" with the following contents −

$ ./test/bats/bin/bats test/tutorial.bats


 ✗ can run our script
   (in test file test/tutorial.bats, line 2)
     './pointscript.sh' failed with status 127
   /home/etc/my_project/test/tutorial.bats: line 2: ./initproj.sh: No such file or directory
1 test, 1 failure

Since we don't have a file with that name, the test has obviously failed. Therefore, let's write a basic bash script in the /tst directory as follows −

$ mkdir tst/
$ echo '#!/usr/bin/bash' > initproj.sh
$ chmod a+x initproj.sh

Execute the command shown below to run our previous test from the root directory −

$ ./test/bats/bin/bats test/tutorial.bats
 ✓ can run our script
1 test, 0 failures


In this tutorial, we learned how to test bash scripts with various bash components. We learned how to test Bash scripts using the Bats library. We learned how to write our own assertions, used assertion tools given by the library, and created simple tests.

BATS offers the extensive testing features seen in other software development environments after the decision is taken to apply a testing discipline to one or more Bash scripts or libraries.

I hope you find these examples of the commands useful.