Pretty-print for shell script

前端 未结 4 2139
小蘑菇
小蘑菇 2020-12-31 17:35

I\'m looking for something similiar to indent but for (bash) scripts. Console only, no colorizing, etc.

Do you know of one ?

4条回答
  •  借酒劲吻你
    2020-12-31 18:29

    shfmt works very well.

    You can format bash scripts and also check the formatting from pre-commit hooks.

    # reformat
    shfmt -l -w script.sh
    
    # check if the formatting is OK
    shfmt -d script.sh
    
    # works on the whole directory as well
    shfmt -l -w .
    

    The only option absent is that it does not reformat according to line length (yet).

    Since it is written in go you can just download the binary for most platforms, e.g. for Travis (.travis.yml):

    install:
      - curl -LsS -o ~/shfmt https://github.com/mvdan/sh/releases/download/v3.1.2/shfmt_v3.1.2_linux_amd64
      - chmod +x ~/shfmt
    script:
      - ~/shfmt -d .
    

    There is also a cross compiled js version on npm and a lot of editor plugins (see related projects)

提交回复
热议问题