Pretty-print for shell script

I am looking for something similar to indent , but for scripts (bash). Only the console, without coloring, etc.

Do you know anyone?

+7
scripting bash shell indentation
source share
4 answers

Vim may have bash indents. But do not reformat them before the indentation.
Back up the bash script, open it with vim, enter gg=GZZ and the indent will be fixed. (Note for the impatient: this overwrites the file, so be sure to make this backup!)

Although there are some errors with << (expecting EOF as the first character in the string), for example.

EDIT: ZZ not ZQ

+5
source share

A bit late for the party, but it seems shfmt can help you.

+2
source share

In Bash, I do this:

 reindent() { source <(echo "Zibri () {";cat "$1"; echo "}") declare -f Zibri|head --lines=-1|tail --lines=+3 | sed -e "s/^\s\s\s\s//" } 

this excludes comments and repeats the text of the bash way script.

If your script has HEREDOCS, they were destroyed by sed in the previous function.

So use:

 reindent() { source <(echo "Zibri () {";cat "$1"; echo "}") declare -f Zibri|head --lines=-1|tail --lines=+3" } 

But all your scripts will be indented with 4 spaces.

Or you can do:

 reindent () { rstr=$(mktemp -u "XXXXXXXXXX"); source <(echo "Zibri () {";cat "$1"|sed -e "s/^\s\s\s\s/$rstr/"; echo "}"); echo '#!/bin/bash'; declare -f Zibri | head --lines=-1 | tail --lines=+3 | sed -e "s/^\s\s\s\s//;s/$rstr/ /" } 

who also takes care of the heirs.

+1
source share

Found it http://www.linux-kheops.com/doc/perl/perl-aubert/fmt.script .

Very nice, only one thing I pulled out is [...] โ†’ replacing the test.

-one
source share

All Articles