GoAdmio

reindent

Reformat a shell script in place with shfmt using 4-space indentation, or reformat every *.sh file in the current directory

On this page: Overview · Quick start · Syntax · Options · Output · Examples · Troubleshooting · Details · See also


Overview

/go/reindent keeps shell scripts in one consistent style. It runs shfmt, a shell script formatter, with these settings:

You can run it on one file, or on every *.sh file in the folder you are standing in.

[!WARNING] The file is overwritten, and no backup is made. shfmt can also change more than indentation, such as spacing and line layout. Preview the changes first with shfmt -d -i 4 <file>, or keep the script under version control.


Quick start

# Reindent one script
/go/reindent /go/postmaxsize

# Reindent every .sh file in the current folder
cd /root/scripts
/go/reindent all

Syntax

/go/reindent <file>
/go/reindent all
Form Behavior
/go/reindent <file> Reformats that file if it exists and is a regular file.
/go/reindent all Reformats *.sh in the current directory only.

Only the first argument is used. If it is neither all nor an existing file, the command does nothing and prints nothing.


Options

There are no --option flags. The only arguments are:

Argument Required Default Notes
<file> One of the two — A path to a script. A relative path is taken from the current directory. Paths with spaces don't work.
all One of the two — Matches *.sh in the current directory. It does not include subfolders or files without a .sh extension.

Output

Outcome What you get
File reformatted Nothing. shfmt -w is silent on success.
Syntax error in the script The shfmt error message. That file is left unchanged.
shfmt is not installed shfmt: command not found.
all with no .sh files An error from shfmt about the literal name *.sh.
The path doesn't exist Nothing at all.

Notes on the output:


Examples

# Reindent one command
/go/reindent /go/quota
# Reindent all .sh files in a folder
cd /root/scripts
/go/reindent all
# Preview the changes without writing anything
shfmt -d -i 4 /go/quota

Troubleshooting

Symptom Likely cause Fix
shfmt: command not found shfmt isn't installed. Install shfmt, then run the command again.
all reports an error about *.sh There are no .sh files in the current directory. Change to the right folder first.
all didn't touch the GoAdmio commands The commands in /go have no .sh extension, so *.sh doesn't match them. Pass each file by path, for example /go/reindent /go/create.
Nothing happened and no message was shown The file path doesn't exist, or isn't a regular file. Check the path.
A script with spaces in its path is skipped or fails The path is not quoted when it is passed to shfmt. Rename the file, or run shfmt -w -i 4 "<path>" yourself.
permission denied when writing The file is owned by root, and shfmt doesn't run with sudo. Run sudo /go/reindent <file>.
A syntax error is printed and the file is unchanged The script isn't valid shell. Fix the syntax, then run it again.
The file changed more than indentation shfmt also normalizes spacing and layout. Review with shfmt -d -i 4 <file>, or restore the file from version control.

Details

What happens on each run

flowchart TD
    A["reindent arg"] --> B["Log the call"]
    B --> C{"Argument is all?"}
    C -- "Yes" --> D["shfmt -w -i 4 *.sh in the current folder"]
    C -- "No" --> E{"File exists?"}
    E -- "Yes" --> F["shfmt -w -i 4 file"]
    E -- "No" --> G["Do nothing, no message"]

The shfmt flags

Flag Meaning
-w Write the formatted code back to the file instead of printing it.
-i 4 Indent with four spaces.

shfmt decides which shell dialect to use from the file's .sh extension or its #! line. Scripts with a valid #!/bin/bash line are handled even without an extension.

The current directory matters

The all mode uses a wildcard, which is expanded in the folder you run the command from, not in /go. The single-file mode also uses a relative path from where you are.


See also

Command Description
package Install all base packages required by GoAdmio
update Update GoAdmio to the latest version