Format
Rewrites your script with consistent indentation and spacing using shfmt. Blocks like if, for, while, and functions are indented, and spacing around ; and | is made even.
if [ -f x ];then
echo hi
fiAfterif [ -f x ]; then
echo hi
fiPaste a messy Bash or sh script and get it back with consistent indentation and spacing. The formatter is shfmt, running in your browser, so your script stays on your device.
Format uses shfmt, the standard shell script formatter. The options apply to Format.
Shell scripts are often written quickly and then edited by many people, so indentation and spacing drift. A formatter reads the whole script and prints it back with one consistent layout: every if, for, while, case, and function body indented the same way, and even spacing around semicolons, pipes, and redirects. This tool uses shfmt, the standard formatter for Bash and sh, and changes only the layout, never what the script does.
Rewrites your script with consistent indentation and spacing using shfmt. Blocks like if, for, while, and functions are indented, and spacing around ; and | is made even.
if [ -f x ];then
echo hi
fiAfterif [ -f x ]; then
echo hi
fiChoose tabs, 2 spaces, or 4 spaces for each level of nesting. shfmt uses tabs on its own, but 2 spaces is the most common choice for shell scripts, so it is the default here.
Tabs, 2 spaces, or 4 spacesAfterYour choice per levelIndents the branches of a case statement one level deeper than the case line. On by default.
case $1 in
a) echo a ;;
esacAftercase $1 in
a) echo a ;;
esacPuts a space between a redirect like > or >> and the file name.
echo hi >out.txtAfterecho hi > out.txtWhen a pipe or && command is split over several lines, moves the | or && to the start of the next line instead of the end of the current one.
cat file |
grep aAftercat file \
| grep aA formatter helps whenever a shell script is hard to read or needs to match a shared style. These are the cases that come up most:
These references explain Bash and the formatter behind this tool:
This tool uses shfmt, the standard formatter for shell scripts. To format scripts on your own machine:
| To | Use |
|---|---|
| Format a script in place | shfmt -w script.sh |
| Indent with 2 spaces instead of tabs | shfmt -i 2 -w script.sh |
| Check without changing files | shfmt -d script.sh shows the changes as a diff |
| Find bugs, not just layout | shellcheck script.sh |
shfmt only fixes layout. ShellCheck is a separate tool that warns about real bugs, like unquoted variables that break on spaces, so the two work well together. shfmt also reads indent settings from an .editorconfig file.
If you work across languages, these formatters run the same way, right in your browser: