Code formatter / beautifier for bash (in command line)?
Asked Answered
G

4

30

Looking for a command line code formatter that can be used for bash code. It must be configurable and preferably usable from command line.

I have a big project in bash, which I need to use Q in mind for. So far I am happy with a program written in python by Paul Lutus (a remake of his previous version in Ruby).

See http://arachnoid.com/python/beautify_bash_program.html (also cloned here https://github.com/ewiger/beautify_bash).

but I would like to learn any serious alternative to this tool if it exists. Requirements: it should provide robust enough performance and behavior of treating/parsing rather complicated code.

PS I believe full parsing of bash code is generally complicated because there exists no official language grammar (but please correct me if I am wrong about it).

Grani answered 21/8, 2012 at 0:11 Comment(0)
I
18

you can script vim to do: "gg=G" that means "indent all the file"

Italianism answered 23/11, 2012 at 0:30 Comment(3)
Yes, it does seem to be robust enough, so it qualifies.Grani
@YauhenYakimovich Is their to automatically format a file through the bash interpreter?Lorenzalorenzana
I just tried this, and the resulting indentation was incorrect. According to vim's internal documentation, it will filter the text through "the internal formatting function |C-indenting| and |'lisp'|". (I haven't set equalprg or indentexpr.) The file has a .sh extension if that matters.Moorfowl
V
19

You could give shfmt a try. It implements its own shell parser including Bash support, so it's more robust than plaintext-based tools.

And both the parser and printer are available as Go packages, so it should be easy to write a 20-line Go program to manipulate or play with shell code.

Please note that I'm the author, so the advice may be a bit biased :)

Vaasta answered 11/3, 2018 at 19:33 Comment(2)
Please add a note, that you are the author of shfmt.Clotildecloture
@DanielMartí I've CCed you here #60154033Rockaway
I
18

you can script vim to do: "gg=G" that means "indent all the file"

Italianism answered 23/11, 2012 at 0:30 Comment(3)
Yes, it does seem to be robust enough, so it qualifies.Grani
@YauhenYakimovich Is their to automatically format a file through the bash interpreter?Lorenzalorenzana
I just tried this, and the resulting indentation was incorrect. According to vim's internal documentation, it will filter the text through "the internal formatting function |C-indenting| and |'lisp'|". (I haven't set equalprg or indentexpr.) The file has a .sh extension if that matters.Moorfowl
C
5

I discovered that the type builtin will print functions in a formatted manner.

#/usr/bin/env bash

source <(cat <(echo 'wrapper() {') - <(echo '}'));
type wrapper | tail -n +4 | head -n -1 | sed 's/^    //g'

https://github.com/bas080/flush

Comfit answered 28/5, 2020 at 10:36 Comment(5)
This could be a nice solution but it removes all the comments and empty lines in the shell script.Cromer
" it removes all the comments and empty lines" is a feature for me... :-DVenose
It can be simplified to: . <( echo wrapper\(\){ ; cat ; echo } ) ; type wrapper | sed '1,/{/d; $d' # I wouldn't change the indentation as that could break quoted multiline stringsSalesgirl
@MartinKealey, that's neat. No more head and tail. To fix the indentation I added ... | sed '1,/{/d; $d;s/^ //g'.Comfit
I mainly dislike the path hijacking issue that using env instead of the shell directly gives, but with that fixed this works well. (I mainly use it to get "one-liners" readable, so the lack of comments and blank lines doesn't bother me)Mayworm
S
1

On the contrary the shell does have a rigorous grammar. It is described both in English in the ISO standard and documentation for Bash and other shells, and in formal terms in the shell.y file in the Bash source tree.

What makes it "hard" is that where one normally thinks of, say, a quoted string as single lexical token, In the shell every meta character is a separate lexical token, so the meaning of a character can change depending on its grammatical context.

So the parsing tokens do not align with the "shell words" that a user thinks of, and a simple quoted string is at least 3 tokens.

The implementations typically take some shortcuts involving using multiple lexical analysers chosen by whether the grammar is inside quotes, inside numeric context, or outside both.

Salesgirl answered 19/5, 2021 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.