from Hacker News

Show HN: Sol – A de-minifier for shell programs

by noperator on 9/16/24, 1:53 PM with 24 comments

I've built a tool called sol (like "soul") that helps you inspect and format complex shell one-liners. Features:

- Choose which transformations you want (break on pipe, args, redirect, whatever)

- "Peeks" into stringified commands (think xargs, parallel) and formats those, too

- Auto-breaks at a given width (e.g., 80 characters)

- Shows you non-standard aliases, functions, files, etc. that you might not have in your shell environment

- Breaks up long jq lines with jqfmt because—let's be honest—they're getting out of hand

As a security researcher and tool developer, I often encounter (or create) long pipelined Bash commands. While quick and powerful, they can be a nightmare to read or debug. I created sol to make it easier to understand and share these commands with others.

  • by kmarc on 9/18/24, 7:47 AM

    I usually do this by hand. Good to see a tool for it :-)

    Feature request, which I would love to have in all my automation scripts:

    Replace short flags with the long switches. Short flags are great when typing in a terminal but I don't want to figure out 2 years from now what the

        obscurecommand -v -f -n
    
    does, and I have to assume that it's NOT --version --file --dry-run, but --verbose, --force, and --dont-ask-before-deleting-everything

    I try to use long options in my script, therefore (especially in a team, where not everyone is familiar with every single command)

  • by snatchpiesinger on 9/18/24, 8:57 AM

    Cool! My personal preference is Knuth-style line-breaks on binary operators and pipes, which means breaking before the operator/pipe symbol.

      foo -a -b \
      | bar -c -d -e \
      | baz -e -f
    
    instead of

      foo -a -b | \
      bar -c -d -e | \
      baz -e -f
    
    This doesn't seem to be an option, but could be easy to implement.
  • by ComputerGuru on 9/18/24, 4:26 PM

    This is really cool; for a second I thought I could use it to stop manually maintaining both the minified and full-text versions of my “shell prefix” that makes it possible to run rust source code directly as if it were a shell script [0] where I’ve accidentally diverged between the two in the past, but then I revisited it and saw that the real value was in the comments and explanations more than just placing output in variables and breaking up command pipelines across shell lines.

    But the opposite might work, does anyone have a good minifier they could recommend (preferably one that does more than just whitespace mangling, eg also renames variables, chains executions, etc) that doesn’t introduce bash-isms into the resulting script?

    [0]: https://neosmart.net/blog/self-compiling-rust-code/

  • by pxc on 9/18/24, 7:37 PM

    This looks really handy! I should add this to the environment for some of my shell-centric projects at work.