Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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).

like image 985
Yauhen Yakimovich Avatar asked Aug 21 '12 00:08

Yauhen Yakimovich


People also ask

How do I format a shell script?

There are two ways to format your code: Automatically format on save (requires enabling in Packages → Format Shell → Toggle Format on Save) Run the command Format Shell: Format to invoke shfmt manually.

How do I run Shfmt?

Run command go install and installation should start. Once installation is done, through terminal (GitBash or cmd) navigate to $HOME/go/bin (on Windows by default it's under C:\Users\your_username\go\bin) From here you can use the shfmt command like so shfmt -l -w yourBashScript.sh. Voila!

What is source code beautifier?

A program that improves the presentation of programming source code. Based on an analysis of the syntax, it indents lines appropriately and squashes extraneous blank spaces and lines in order to produce more readable code.


1 Answers

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

like image 64
drizzt Avatar answered Sep 19 '22 04:09

drizzt