Bash Formatter
Indent and format Bash shell scripts with consistent layout.
- Runs locally in your browser
- No upload required
Try it
Supported formats
Privacy
Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.
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
- Scan each line with a quote-aware scanner that skips single/double quoted strings, comments, ${...} expansions and $(...) groups.
- Detect block keywords only in command position so an argument like `echo if` is never treated as a keyword.
- Emit each line at a depth derived from the accumulated block keywords, and copy heredoc bodies verbatim.
Usage
- Paste or type a Bash or POSIX shell script.
- Choose a 2- or 4-space indent.
- 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
{
"text": "if [ -f x ]; then\necho yes\nfi",
"indentSize": 2
}for loop and function
{
"text": "for i in 1 2 3; do\necho $i\ndone\nf() {\nreturn 0\n}",
"indentSize": 2
}heredoc preserved
{
"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
- POSIX.1-2017 Shell Command Language — IEEE/Open Group
- GNU Bash Reference Manual — GNU Project
Send feedback
Tell us if this tool helped, report a problem, or suggest a tool. Only the message you type is sent — never the input you used in the tool.
Was this tool useful?
Prefer to write to us directly? See the contact page.