shellcheck-py
shellcheck-py is a Python wrapper designed to provide a pip-installable `shellcheck` binary. It internally downloads the pre-built `shellcheck` static analysis tool for shell scripts, making it available in your environment without needing system-level package managers. The current version is 0.11.0.1. The library's release cadence appears to be driven by updates to the underlying `shellcheck` tool and Python compatibility.
Warnings
- gotcha The `shellcheck-py` package installs the `shellcheck` binary to your environment's PATH, rather than providing Python functions for direct linting. Interaction with the linter is typically done by executing the `shellcheck` command via `subprocess` or as a pre-commit hook.
- breaking Older Python versions (e.g., 3.10/3.11) may encounter installation failures due to `shellcheck-py` using the deprecated `setup.py install` method, especially if the `wheel` package is not installed. `pip` versions 23.1 and above are enforcing stricter build standards (PEP 517).
- gotcha While `shellcheck-py` aims to make `shellcheck` easily installable, it still relies on platform-specific pre-built binaries. Issues related to binary compatibility with highly specific or unusual system architectures might occur, though less common for mainstream OSes.
Install
-
pip install shellcheck-py
Imports
- shellcheck binary
import subprocess subprocess.run(["shellcheck", "myscript.sh"])
Quickstart
import subprocess
import os
# Create a dummy shell script for demonstration
script_content = '''#!/bin/bash
# SC2034: foo appears unused. Verify use (or export if used externally).
foo="bar"
echo $foo
'''
with open("test_script.sh", "w") as f:
f.write(script_content)
os.chmod("test_script.sh", 0o755) # Make it executable
print("Running shellcheck on test_script.sh:")
try:
# Run shellcheck, capturing output. -f json for machine-readable output
result = subprocess.run(
['shellcheck', '-f', 'json', 'test_script.sh'],
capture_output=True,
text=True,
check=False # Don't raise an exception for non-zero exit codes (warnings/errors)
)
print("Standard Output:")
print(result.stdout)
if result.stderr:
print("Standard Error:")
print(result.stderr)
print(f"Exit Code: {result.returncode}")
except FileNotFoundError:
print("Error: 'shellcheck' command not found. Ensure shellcheck-py is installed correctly.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Clean up the dummy script
if os.path.exists("test_script.sh"):
os.remove("test_script.sh")