How to Debug Bash Script with “ShellCheck” Command?

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

While writing a bash script, you might run into a bug or a syntax error every now and then. To debug your script, the “ShellCheck” command can be a handful of ways of finding those errors. Upon executing your script with this command, you can constantly check your script for bugs and receive useful suggestions for improvements. In this article, you are going to learn how to debug the bash script with the “ShellCheck” command. Let’s start!

What is ShellCheck?

ShellCheck is a static analysis tool for shell scripts. It is designed to identify and provide suggestions for common issues, syntax errors, and potential pitfalls in shell scripts written in languages like Bash, Shell, and Dash. ShellCheck analyzes the script’s code and offers recommendations to improve its quality, readability, and adherence to best practices.

How to Install ShellCheck on Ubuntu?

To debug and validate a Bash script using ShellCheck, you should first ensure that you have ShellCheck installed on your system. The easiest way to install ShellCheck on your local machine is to use your package managers, such as apt, apt-get, or yum, according to your Linux distribution or Unix distribution. Run ShellCheck commands according to your distro. If you want to install ShellCheck in Ubuntu, see the following step:

To install ShellCheck on Ubuntu, type the following command in your terminal:

sudo apt install shellcheck

Installing shellcheck command through the terminalThe command provided has successfully installed ShellCheck on the system, as depicted in the image.

Debugging and Validating Bash Script with ShellCheck

To use the ShellCheck on your bash script, first, you need to write a script and then check whether it contains any error.

Down below, I am providing a sample code and debugging that code using the ShellCheck  command:

#!/bin/bash
echo "This is a script with an error"

# Error: Variable assignment should not have spaces around the equal sign
error_var = "Oops! There's an error."
echo $error_var
EXPLANATION

In this example, the error is the presence of spaces around the equal sign = in the variable assignment. The correct way to assign a value to a variable in Bash is without spaces around the equal sign. Using the bash command to execute the file will not give any detailed information regarding the error but the ShellCheck command is helpful regarding this type of syntax issue.

Now, I will run the whitespace.sh script with the conventional bash command and see what the output appears in the terminal. Write the following command in the terminal:

bash whitespace.sh

Executing bash file with bash commandBased on the error message displayed in the terminal, we understand that there is an error on line 6 of the code. However, the error message does not provide any information about the type of error or how to fix it. This is where the ShellCheck command comes in handy. By running the script with the ShellCheck command, you can obtain a detailed explanation of the error type and potential solutions to fix it.

To debug a shell script, use the ShellCheck command followed by the script’s name. See the code below:

shellcheck whitespace.sh

Debugging bash script with shellcheck commandFrom the image provided above, you can observe that running the ShellCheck command highlights the error line and suggests a solution to rectify the issue. In this case, the code has a problem with spaces before and after the equal sign in line 6. To fix it, simply remove the equal sign and the code will be ready to use without any issues.

Conclusion

To sum up, using the “ShellCheck” command is a great way to debug bash scripts effectively. ShellCheck is a great static analysis tool that can help you find syntax errors, common mistakes, and areas where scripts could be improved. It provides clear, actionable feedback, simplifies the debugging process, improves script reliability, and follows best practices. This article has discussed the process of using ShellCheck to debug the bash script. If you have any questions or queries related to this article, feel free to comment below. Happy debugging!

People Also Ask

How to use ShellCheck in bash?

To utilize ShellCheck in bash, begin by installing it. Installing ShellCheck in bash is easy. You can install it through apt, yum, or the official website. After installing ShellCheck, you can run the shellcheck command, followed by the script’s name, as given below:

shellcheck script_name.sh

How do I check my shell?

To check which shell you are currently using, you can use the following command in the terminal:

echo $SHELL

This command echoes the value of the SHELL environment variable, which typically holds the path to the current shell executable. The output will display the path to your current shell, such as /bin/bash, /bin/zsh, or another shell executable.

Alternatively, you can use the ps command to display information about the current shell process. For example:

ps -p $$

The $$ represents the process ID of the current shell. The output will include information about the shell process, including its name.

By running either of these commands, you can determine the shell you are currently using in your terminal session.

How do I verify bash syntax?

To verify the syntax of a Bash script without executing it, you can use the bash shell itself along with the -n option. Here’s the command:

bash -n your_script.sh

Replace “your_script.sh” with the actual filename of your Bash script. The -n option instructs Bash to check the syntax without executing the script.

If there are no syntax errors, you will see no output. If there are errors, Bash will provide information about the specific errors in your script.

How do I check for errors in bash?

To check for errors and debug a Bash script, you can use the -x option with the bash command. This option enables debugging output, showing each command as it is executed. Here’s an example:

bash -x your_script.sh

Replace “your_script.sh” with the actual filename of your Bash script.

When you run the script with the -x option, the terminal will display each command along with its output. This can help you identify errors and understand how the script is being executed.

Related Articles


<< Go Back to Bash Debugging | Bash Error Handling and Debugging | Bash Scripting Tutorial

Rate this post
Mohammad Shah Miran

Hey, I'm Mohammad Shah Miran, previously worked as a VBA and Excel Content Developer at SOFTEKO, and for now working as a Linux Content Developer Executive in LinuxSimply Project. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). As a part of my job, i communicate with Linux operating system, without letting the GUI to intervene and try to pass it to our audience.

Leave a Comment