Run ShellCheck Inside Docker Container on Linux

Run ShellCheck Inside Docker Container on Linux

ShellCheck is a command line tool that automatically analyzes shell scripts to identify syntax errors, potential bugs, and style issues. It provides helpful suggestions to improve script readability. Running ShellCheck inside a Docker container with all dependencies preinstalled ensures consistent results across all team members, eliminates environment-specific issues, and simplifies maintenance by standardizing the tool version and configuration.

Let's say we have the following Bash script named test.sh:

#!/bin/bash

test="Hello world"
echo $test

To analyze the shell script using ShellCheck from a Docker container, run the following command:

docker run -it --rm -v ./:/code pipelinecomponents/shellcheck shellcheck test.sh

Explanation of the command:

  • docker run - launches a new container.
  • -it - starts an interactive terminal session.
  • --rm - automatically removes the container after it finishes, keeping the system clean.
  • -v ./:/code - mounts the current directory (./) to /code inside the container, allowing ShellCheck to access the local files.
  • pipelinecomponents/shellcheck - specifies the Docker image to use.
  • shellcheck test.sh - runs ShellCheck inside the container on the test.sh script.

It analyzes the script and provides warnings, suggestions, and links to helpful documentation:

In test.sh line 4:
echo $test
     ^---^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
echo "$test"

For more information:
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...

Leave a Comment

Cancel reply

Your email address will not be published.