Shell Script Formatter

Formatters

How to use the Shell Script Formatter

Format your shell script in three steps:

1

Paste or upload your shell script

Paste a bash, sh, or zsh script into the input panel or upload a .sh, .bash, or .zsh file. Scripts with or without shebangs are both accepted.

2

Choose shell type and indent size

Select bash, sh, or zsh mode. Choose 2 or 4 spaces for indentation. Bash and zsh scripts commonly use 2 spaces; 4 spaces is also widely used.

3

Format, copy, or download

Click 'Format Shell' to apply shfmt-style indentation and shell syntax highlighting. Copy the result or download as the appropriate shell script extension.


When to use this tool

Use the shell formatter when a shell script needs clean, readable indentation before sharing or committing:

  • Formatting bash scripts from documentation, GitHub Actions workflows, or CI/CD examples that lost indentation
  • Reformatting shell scripts that have grown organically with inconsistent indentation across team members
  • Cleaning up AI-generated bash scripts before including them in a DevOps or infrastructure-as-code repository
  • Preparing shell script examples for technical blog posts, runbooks, or developer documentation
  • Tidying deployment, build, or backup scripts before code review in a team repository
  • Formatting bash interview solutions or DevOps challenge scripts for readable submission

Frequently asked questions

Q:What is shfmt and how does this formatter compare?
shfmt (shell formatter) is a widely used command-line tool by mvdan that formats shell scripts according to POSIX sh, bash, mksh, and bats conventions. It handles complex shell constructs like heredocs, process substitutions, and compound commands with full POSIX AST parsing. This browser formatter applies shfmt's indentation logic for the most common shell constructs and is ideal for quick formatting. For strict shfmt compliance in a CI/CD pipeline, install shfmt natively and run it as part of your shell script linting step.
Q:Does it format both POSIX sh and bash-specific syntax?
Yes — the formatter handles both POSIX sh constructs (if/fi, for/do/done, case/esac, while/do/done) and bash-specific syntax (function declarations with the function keyword, [[ ]] test expressions, process substitution, brace expansion, and arrays). Select the correct shell mode (bash/sh/zsh) to ensure the most appropriate syntax highlighting for your script.
Q:How is the shebang line (#!) handled?
The shebang line (#!/bin/bash, #!/usr/bin/env bash, #!/bin/sh) is always placed at column zero regardless of the surrounding context, following the Unix convention that shebangs must appear at the very first character of a file. It is also highlighted distinctly in the syntax output to visually separate it from regular comments.
Q:Does it handle heredocs correctly?
Heredoc declarations (<<EOF, <<'EOF', <<-EOF, <<~'EOF') are recognised and preserved. The formatter keeps the heredoc delimiter and its content unchanged, as reformatting heredoc content would risk altering multi-line string values embedded in the script.
Q:Can it format GitHub Actions workflow scripts embedded in YAML?
This formatter is designed for standalone .sh, .bash, and .zsh script files. For shell commands embedded in GitHub Actions YAML (run: | blocks), extract the shell commands, format them here, then paste them back into the YAML. The formatter does not process YAML syntax.
Q:Should I use shellcheck alongside this formatter?
Yes — this formatter handles style and indentation; ShellCheck (shellcheck.net) handles correctness, detecting undefined variables, quoting issues, common bash pitfalls, and POSIX compatibility problems. They are complementary tools. Format your script here for consistent style, then run ShellCheck to catch logical errors and anti-patterns before deploying.