3

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
    

1 Answer 1

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. Commented Jun 9, 2017 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" Commented Jun 10, 2017 at 11:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.