pre-commit-config-shellcheck for syntax and semantic checks automation
Tags: python, shell, precommit, automation
Published: 2022-09-21 10:10:52

---
title: pre-commit-config-shellcheck for syntax and semantic checks automation
published: true
description: pre-commit-config-shellcheck is a tool by Anadea developers for checking entry points in the pre-commit config with ShellCheck.
tags: python, shell, precommit, automation
# cover_image: https://anadea.info/uploads/image_attachment/image/3004/nemo.jpg
# Use a ratio of 100:42 for best results.
---

## The reasoning
Second-checking of an already written code may be an exhausting and unreliable task for most of the programmers involved in [custom software development](https://anadea.info/services/custom-software-development). While refactoring the code a few days ago, we found some mistakes in shell code in the pre-commit config file. These errors have been there for a long time without any warnings. That's why we thought if these code checks either were more reliable or proceeded automatically. Thereafter, [pre-commit-config-shellcheck](https://github.com/Anadea/pre-commit-config-shellcheck) was created for this very purpose.

## What’s the difference
This tool will automatically detect and help you to resolve mistakes in your project’s _.pre-commit-config.yaml_ file. All the syntax and most semantic checks are done for you. Also, you will receive bug fixing recommendations. This is done by automatic check of all the shell code entries in the config file with the other tool—_Shellcheck_.

## How it works
### Shellcheck
[Shellcheck](https://github.com/koalaman/shellcheck) is a shell script static analysis tool. The main purpose of this tool is to find popular syntax mistakes as well as to point out corner cases that may cause scripts to fail under future circumstances while giving suggestions for fixing those.

### File checking
All the checks of this tool are done on the _.pre-commit-config.yaml_ file, but this file can’t be directly analyzed by Shellcheck due to the structure of the file, which includes not only shell code but other configuration points. We found the workaround for this in creating a list of temporary files with found shell entry points and checking them separately.

### Subprocesses
The main way of using Shellcheck is through a terminal, and this tool doesn’t have an adaptor for different programming languages, so we use the Python subprocess module in order to perform checks on each temporary file. Then the output from subprocesses is collected and the tool forms a single pre-commit-config-shellcheck output with the list of warnings and their locations.

Example of usage and output:
```
; pre_commit_config_shellcheck.py .pre-commit-config.yaml                                                                                                                                                                               
                                                                                                                                                                                                                                        
In entry "seed-isort-config" on line 9:                                                                                                                                                                                                 
var = 42                                                                                                                                                                                                                                
    ^-- SC2283 (error): Remove spaces around = to assign (or use [ ] to compare, or quote '=' if literal).                                                                                                                              
                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                        
In entry "seed-isort-config" on line 10:                                                                                                                                                                                                
echo '$var'                                                                                                                                                                                                                             
     ^----^ SC2016 (info): Expressions don't expand in single quotes, use double quotes for that.                                                                                                                                       
                                                                                                                                                                                                                                        
For more information:                                                                                                                                                                                                                   
  https://www.shellcheck.net/wiki/SC2283 -- Remove spaces around = to assign ...                                                                                                                                                        
  https://www.shellcheck.net/wiki/SC2016 -- Expressions don't expand in singl...                                                                                                                                                        
                                                                                                                                                                                                                                        
In entry "removestar" on line 19:                                                                                                                                                                                                       
removestar -i ${NAME}                                                                                                                                                                                                                   
              ^-----^ SC2086 (info): Double quote to prevent globbing and word splitting.                                                                                                                                               
                                                                                                                                                                                                                                        
Did you mean:                                                                                                                                                                                                                           
removestar -i "${NAME}"                                                                                                                                                                                                                 
                                                                                                                                                                                                                                        
For more information:                                                                                                                                                                                                                   
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...    
```
### Easy to use
For the sake of keeping this tool simple for users, there are a few ways to use pre-commit-config-shellcheck:

#### As a command-line tool
```
$ pip install pre-commit-config-shellcheck
$ pre_commit_config_shellcheck.py .pre-commit-config.yaml
```
#### As the [pre-commit](https://pre-commit.com/) hook
```
# .pre-commit-config.yaml
- repo: "https://github.com/Anadea/pre-commit-config-shellcheck"
  rev: "0.3.4"
  hooks:
    - id: "pre-commit-config-shellcheck"
```
#### And also as a [GitHub action](https://github.com/features/actions/)
```
- name: "pre-commit-config-shellcheck"
  uses: "action/pre-commit-config-shellcheck@0.3.4"
  id: "pre-commit-config-shellcheck"
  with:
    config: ".pre-commit-config.yaml"
```
## Conclusion
Pre-commit-config-shellcheck was successfully created, tested and deployed for third-party usage according to the licensing. This tool, additional information and licensing can be found on [GitHub](https://github.com/Anadea/pre-commit-config-shellcheck).