Bash Formatter

Indent and format Bash shell scripts with consistent layout.

  • Runs locally in your browser
  • No upload required

Try it

Paste a Bash or POSIX shell script. Heredocs and quotes are preserved.

Formatted Bash
if [ -f x ]; then echo yes fi
Lines
3
if [ -f x ]; then
  echo yes
fi

Supported formats

Input
text, number
Output
text

Privacy

Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.

Processing
In your browser
Privacy level
Browser only

Re-indented 3 line(s) of shell script at 2-space depth.

How it works

  • Scanned each line outside of quotes, comments and ${...}/$(...) expansions.
  • Tracked block depth from if/then/fi, for/do/done, while, case/esac, functions and brace groups.
  • Emitted each command at a depth-based indentation.
  • Copied heredoc bodies, quoted strings and comments byte-for-byte.
  • Shell indentation is cosmetic: the script's behaviour is unchanged.
  • Parentheses (subshells) do not add an indent level; only braces do.
  • Output is 3 line(s); formatting it again changes nothing.

Methodology

  1. Scan each line with a quote-aware scanner that skips single/double quoted strings, comments, ${...} expansions and $(...) groups.
  2. Detect block keywords only in command position so an argument like `echo if` is never treated as a keyword.
  3. Emit each line at a depth derived from the accumulated block keywords, and copy heredoc bodies verbatim.

Usage

  1. Paste or type a Bash or POSIX shell script.
  2. Choose a 2- or 4-space indent.
  3. Run the formatter and copy the indented result.

Examples

These are examples, not current values: they use fixed inputs, while the interactive tool computes live results in your browser.

if block indented

jsonSelect to copy
{
  "text": "if [ -f x ]; then\necho yes\nfi",
  "indentSize": 2
}

for loop and function

jsonSelect to copy
{
  "text": "for i in 1 2 3; do\necho $i\ndone\nf() {\nreturn 0\n}",
  "indentSize": 2
}

heredoc preserved

jsonSelect to copy
{
  "text": "cat <<EOF\n  raw  text\nEOF",
  "indentSize": 4
}

Limitations

When to format a shell script

Consistent indentation makes control flow reviewable: you can see at a glance which commands sit inside an `if`, a loop or a function.

The formatter is intentionally conservative: it never rewrites command substitution or here-documents, so a script that works keeps working.

Frequently asked questions

Does formatting change how my script runs?
No. Shell ignores leading indentation, so only the layout changes. Heredoc bodies and quoted strings are copied exactly.
Why is my heredoc not indented?
Heredoc bodies are copied byte-for-byte so the delimiter stays valid and the content is never altered.
Is my script uploaded?
No. All processing happens in your browser and nothing is sent anywhere.

Sources

  1. POSIX.1-2017 Shell Command Language — IEEE/Open Group
  2. GNU Bash Reference Manual — GNU Project