# shfmt {#shfmt} ## In this document {#shfmt_toc} * @ref shfmt_overview * @ref shfmt_usage * @ref shfmt_installation * @ref shfmt_examples ## Overview {#shfmt_overview} The majority of tests (and scripts overall) in the SPDK repo are written in Bash (with a quite significant emphasis on "Bashism"), thus a style formatter, shfmt, was introduced to help keep the .sh code consistent across the entire repo. For more details on the tool itself, please see [shfmt](https://github.com/mvdan/sh). ## Usage {#shfmt_usage} On the CI pool, the shfmt is run against all the updated .sh files that have been committed but not merged yet. Additionally, shfmt will pick all .sh present in the staging area when run locally from our pre-commit hook (via check_format.sh). In case any style errors are detected, a patch with needed changes is going to be generated and either build (CI) or the commit will be aborted. Said patch can be then easily applied: ~~~{.sh} # Run from the root of the SPDK repo patch --merge -p0 if [[ -v foo ]] \ && [[ -v bar ]] \ && [[ -v foobar ]]; then echo "This is foo" fi # Currently, newlines are being escaped even if syntax-wise # they are not needed, thus watch for the following: if [[ -v foo && -v bar && -v foobar ]]; then echo "This is foo" fi #-> if [[ -v foo && -v \ bar && -v \ foobar ]]; then echo "This is foo" fi # This, unfortunately, also breaks the -bn behavior. # (see https://github.com/mvdan/sh/issues/565) for details. ###################################### case "$FOO" in BAR) echo "$FOO" ;; esac # switch_case_indent = true case "$FOO" in BAR) echo "$FOO" ;; esac ###################################### exec {foo}>bar :>foo exec {bar} bar : > foo exec {bar}< foo ###################################### # miscellaneous, enforced by shfmt (( no_spacing_at_the_beginning & ~and_no_spacing_at_the_end )) : $(( no_spacing_at_the_beginning & ~and_no_spacing_at_the_end )) # -> ((no_spacing_at_the_beginning & ~and_no_spacing_at_the_end)) : $((no_spacing_at_the_beginning & ~and_no_spacing_at_the_end)) ~~~