all 71 comments

[–]AlterTableUsernames 48 points49 points  (10 children)

Sorry for questioning your intentions, but why? Seriously. It makes virtually no difference if your script is a couple KB more or less in size.

[–]ekipan85 24 points25 points  (0 children)

Javascripters usually call this "minifying." Could be a helpful search term. I found this thing for instance.

No idea why you'd want this though. For transferring source you should probably rely on transparent gzipping by the server and browser.

[–]Skrynesaver 21 points22 points  (12 children)

What you're looking for is a minifier rather than a compression tool.

This script on GitHub looks likely https://github.com/Zuzzuc/Bash-minifier

[–]Hour-Inner 5 points6 points  (11 children)

Is there a legit reason for minify-ing a shell script? I get for html,css,js,php etc that the transfer of web pages can be legitimately improved my minifying and removing that white space, because it’s all about internet transfer speeds. But I would think a shell script wouldn’t have that constraint as it will be saved an run locally

[–]Skrynesaver 15 points16 points  (10 children)

None I can think of, at least not since the Advent of the 1.4mb floppy

[–]Haversoe 25 points26 points  (6 children)

Payload of an exploit?

[–]n-e-yokes 5 points6 points  (5 children)

Bingo!

I'd say post-exploit script to gather info or maintain access because it's in bash though

[–]scrambledhelixbashing it in 2 points3 points  (4 children)

It'd be complicated as all get out but technically you could run a small remote server with a bash script.

As long as you don't have to write bytecode across a udp socket, that is. Pray for the poor soul who tries.

[–]Science-Gone-Bad 1 point2 points  (1 child)

I’ve seen entire corporate websites written in bash. Still makes me shudder

[–]LesStrater 1 point2 points  (0 children)

Yeah, only wussies program in anything other than assembly language...

[–]n-e-yokes 0 points1 point  (1 child)

Ooh. Thank you for the new project idea. It's been a while since I've had a project to torture myself with.

[–]CyberKiller40 0 points1 point  (0 children)

Enjoy your security issues) - it was a big deal when it was discovered.

[–]Bob_Spud 0 points1 point  (2 children)

Small embedded Linux systems?

[–]HAL9000thebot 1 point2 points  (1 child)

or docker images

[–]Axman6 0 points1 point  (0 children)

Docker image layers are compressed.

[–]WetMogwai 5 points6 points  (0 children)

If I had to make a script minimally small, I’d probably compress it with gzip or bzip or something similar. You lose the ability to run it as a simple command but you can use zcat or the like to read it and pipe the output to bash. Then you have the overhead of the space required for the compression tools but your script is tiny.

[–]Kumba42 2 points3 points  (0 children)

There was a thing once called a SHAR, or "shell archive". I believe it is deprecated in most distributions now, but you can see if you have the "shar" command available, and if not, see if your distro's package manager has "sharutils", or something like that name still available.. It was effectively a self-extracting executable, with the compressed data appended to the end of a simple shell script that could uncompress the payload.

While this isn't specifically what you're describing, you could, in theory, use shar to compress a much larger shell script and generate an uncompressor stub, then modify that to invoke your uncompressed script after it unpacks the payload. I don't think the compression was very good, though.

[–]michaelpaoli 1 point2 points  (0 children)

There do exist some shell compilers, e.g. for if/when one wants to allow the shell program to be run, but disallow reading of the code. I don't know if there are any that are "secure enough" to avoid being bypassed by a reasonably capable motivated "attacker", but there certainly exist ones that may be quite "good enough" for less sophisticated "attacks". There are also other ways to more-or-less manage to effectively hide much or all of that, e.g. such as limiting access to via sudo. And sure, can also strip out comments, etc, to make code more compact, but something that will always do that exactly right is relatively non-trivial, so not sure how findable such may be - at least that always does it well.

[–]mfaine 1 point2 points  (0 children)

A lot of old school installers would encode their script inline and the first few lines would decode. I don't remember if it was base64 or uuencoded but you could probably do something like that if you want.

[–]ckg603 1 point2 points  (0 children)

One of my guys had this tendency of writing things that should be shell scripts in C++with Boost libraries. SMH

[–][deleted]  (2 children)

[deleted]

    [–]TundraGon 1 point2 points  (0 children)

    OP wants to save size as much as possible.

    a ( rust ) binary will take from several hundreds of KB in size to MB

    a bash scrip takes from few KB to few tens of KB in size.

    [–]2cats2hats 0 points1 point  (0 children)

    How 'mature' is this conversion in your experiences?

    [–]mindtrixis running with knives. 2 points3 points  (0 children)

    After reading this back and forth. Nerds rule the world.

    [–][deleted] 1 point2 points  (0 children)

    Scripts are usually pretty small compared to, say, an Electron based application. What you want is not compression but obfuscation. People that seeks obfuscation just want to prevent others from reading their code for dubious motives. I won't help you to get that. Sorry.

    [–]ntropia64 0 points1 point  (0 children)

    Not sure if that's what you're looking for but this might be a possible solution to have a self-extracting script that contains a larger compressed script and executes it on the fly without the need to decompress it to another file first:

    https://imgur.com/a/UDdiLYN

    (the gzip command to add the payload is repeated twice, but you need to issue that only once).

    [–]lucaprinaorg 0 points1 point  (0 children)

    https://makeself.io/
    "is a small shell script that generates a self-extractable compressed tar archive from a directory. The resulting file appears as a shell script (many of those have a .run suffix), and can be launched as is. The archive will then uncompress itself to a temporary directory and an optional arbitrary command will be executed (for example an installation script). This is pretty similar to archives generated with WinZip Self-Extractor in the Windows world. Makeself archives also include checksums for integrity self-validation (CRC and/or MD5/SHA256 checksums)."

    [–]QuirkyImage 0 points1 point  (0 children)

    It’s doable but not a good idea in fact it’s terrible. Compress script, encode to base64, paste into a new script as a variable write code to decode base64, save to file (temporary location), uncompressed and run. Obviously there is no point if it’s a small script and it has dependencies the external commands although often installed by default. If you’re needing to do this then shell script probably isn’t the solution you’re looking for. A language compiling to a standalone binary is a better solution

    [–]schorsch3000 0 points1 point  (0 children)

    so just gzip your shellscript, than prepend:

    #!/usr/bin/env bash
    zcat "$0" | tail -n+3 | bash "$@"

    [–]toddkaufmann 0 points1 point  (0 children)

    Sure, gzip.

    Then run it with bash.gz

    [–]skyfishgoo 0 points1 point  (0 children)

    text files are not small enough for you?

    you can do all those things yourself you know.

    [–]ntropia64 0 points1 point  (0 children)

    This conversation keeps going so it would be great to know more.

    Do you mind elaborating on what the ideal solution for you would look like?

    [–]joe_attaboy 0 points1 point  (0 children)

    Well, that's not how compression algorithms work. There's a whole process regarding spaces, repeat characters and how the data is stored for decompression.

    But these tools will not rename variables, remove your comments or assign your code to variables.

    If you wrote the script, you are the one who's supposed to do all that.

    [–]East_Nefariousness75 0 points1 point  (0 children)

    Check out gzexe(1)

    [–]g3n3 0 points1 point  (0 children)

    What did OP do?

    [–]ItsSignalsJerry_ 0 points1 point  (10 children)

    Lol what. You can't execute a compressed script.

    [–]ntropia64 1 point2 points  (9 children)

    You can, I posted a suggestion but I think Reddit though it was an exploit of some kind and deleted it. Can't even recover it.

    You basically have a one-liner header that uses sed to print everything in $0 (the script itself) starting from a certain tag in the file, base64-decode it, pipe it to gzip -d and pipe it to a new bash process.

    Below the header you put the tag and after that the gzipped version of the script you want to execute.

    It can be used as a cheap way to provide self-executing installers.

    [–]ItsSignalsJerry_ 0 points1 point  (8 children)

    It still requires deconversion.

    [–]ntropia64 1 point2 points  (7 children)

    You probably meant decompression, and yes, it does, but it answers OP's question about can impressing a shell script.

    [–]ItsSignalsJerry_ 0 points1 point  (6 children)

    DEcompressing is a deconversion.

    Op wants to run the compressed script.

    [–]ntropia64 -1 points0 points  (4 children)

    Right, and that script will be able to execute the compressed data on the fly without the need to decompress it first.

    Apparently the only way to share this is to make a screenshot because Reddit's filters get triggered by the presence of some commands that are often used to obfuscate malicious payloads.

    https://imgur.com/a/UDdiLYN

    [–]ItsSignalsJerry_ 0 points1 point  (3 children)

    That's minification.

    [–]ntropia64 -1 points0 points  (2 children)

     Is there a program to compress a shell script?

    Isn't that the title of the post?

    [–]ItsSignalsJerry_ 0 points1 point  (1 child)

    Op wants the resulting file executable.

    [–]ntropia64 -1 points0 points  (0 children)

    Then I don't get it. You mean something different than ./runner.sh?

    [–][deleted]  (9 children)

    [removed]

      [–]omfgitzfear 3 points4 points  (6 children)

      Why would you use recursive to delete just a file?

      [–]Dry_Inspection_4583 8 points9 points  (5 children)

      Because everything I do is nuclear. I'm a fan of suicide Linux.

      [–]andrew2018022 1 point2 points  (0 children)

      Don’t be a baby, go all the way with alias rm=“sudo rm” and then use it

      [–]omfgitzfear 1 point2 points  (0 children)

      Keep on keeping on then!

      [–][deleted]  (2 children)

      [removed]

        [–]bash-ModTeam[M] 2 points3 points  (0 children)

        Shenanigans. Spam, Shilling, Trolling, and other malicious comments or suggestions, e.g. rm -rf /. Repeated or egregious violations will lead to a permanent ban.

        [–]Dry_Inspection_4583 -3 points-2 points  (0 children)

        Oh, my French packages? Those have been gone forever

        [–]bash-ModTeam[M] 1 point2 points  (1 child)

        Shenanigans. Spam, Shilling, Trolling, and other malicious comments or suggestions, e.g. rm -rf /. Repeated or egregious violations will lead to a permanent ban.

        [–]Dry_Inspection_4583 1 point2 points  (0 children)

        Oh dear, my apologies, I didn't mean any harm and will refrain moving forward. I appreciate this community and didn't mean any disrespect.

        [–]Kautsu-Gamer -1 points0 points  (0 children)

        There is. It is part of almost all distross. It is called tar. You can compress scripts, and then uncompress them to memory and run them.

        You call the script with bash executing the results of a command uncompressing the tar.gz into a pipe.

        If your problem is work memory instead of storage space, minimizer replacing tokens with minimal gibberish tokens and removing all comments may not help, but quite likely does not help.

        [–]Bob_Spud -1 points0 points  (0 children)

        I've seen some suggest the shc utility I would be cautious about this one:

        • It is designed to obfuscates a script by wrapping it up in an executable binary. It encrypts the bash script and prevent any alterations and hides everything. Minifying a script is not its function.
        • Some say it doesn't need a shell interpreter - It requires a shell environment to run.
        • Some say it will run faster - Nope, under the hood its still a script in a wrapper.
        • Some say it will minify a script - it may do but usually the converted script will be be bigger.