1

Various minifier scripts exist for shell code, (e.g. bash-minifier), but how about the reverse?

  1. Are there any shell-centric utils or scripts to automatically turn a one-liner like this:

    echo foo;echo bar;echo "baz;bing";echo 'buz;bong'
    

    ...into this:

    echo foo
    echo bar
    echo "baz;bing"
    echo 'buz;bong'
    
  2. Or turn minimalist logic like this:

    true && echo foo
    

    ...into this:

    if true ; then
        echo foo
    fi
    
2

Minification is not generally a reversible operation, as information could be lost in the process, e.g. consider human-readable variable names, comments, logical constructs, which can be written in multitude of different ways e.t.c.

But there are various tools, which can pretty-print or beautify your code, which should solve #1 for you.

One example is: https://github.com/mvdan/sh

A shell parser, formatter and interpreter (POSIX/Bash/mksh)

Running your one-liner, through it, produces the following result:

%shfmt <<<"echo foo;echo bar;echo \"baz;bing\";echo 'buz;bong'"

echo foo
echo bar
echo "baz;bing"
echo 'buz;bong'
2
  • Progress... but shfmt doesn't seem to unroll a for loop: shfmt <<< "for f in foo bar; do echo $f ; done" returns for f in foo bar; do echo bar; done where the semicolons aren't changed into carriage returns.
    – agc
    Jun 9 '17 at 22:28
  • 1
    @agc It does re-format the loops in fact, but only if they have > 1 statements in the body block, e.g. shfmt -s <<< "for f in foo bar; do echo $f; echo $f ; done"
    – zeppelin
    Jun 10 '17 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.