Bash is technically an interpreted language, it's not compiled; compiling it is not possible.
Bash is the shell, or command language interpreter, for the GNU operating system.
However, it is possible to obfuscate it so prying eyes cannot look at your script.
Use openssl to encode the file.
I created a simple Zsh script file called
arraytest.sh that will execute in Zsh (it's what I had available at the moment). I'm not going to post the actual shell script, instead, I am going to post the base64 encoding.
On my computer, I run the following command on my script and get the following results:
% openssl base64 < arraytest.sh
That output needs to be saved to a file. Let's called it
myencodedscript. You can copy and paste it into a new file. You don't have to make it executable. Once you've copied that script into your clipboard, issue the command
% pbpaste > myencodedscript
Now, you should have a file called
myencodedscript. Next, to run it, you need to feed that encoding back through
openssl to decode it and then pipe it to your shell.
% cat myencodedscript | openssl base64 -d | zsh
It should run the the script and generate output from an associative array.
So, how can you make this self contained?
#Script Name: encoded_script.sh
echo "$code" | openssl base64 -d | zsh
Save that script as
encoded_script.sh, make it executable and then run it with the command
(This assumes a scenario like an admin needing to run a script discretely on 1 or more machines in his care.)
If you want to truly hide this from "prying eyes", use this single line command:
% openssl base64 < script.sh | ssh firstname.lastname@example.org ' openssl base64 -d | bash'
This will encode the script, pipe it through an SSH connection where it decodes the script and passes that to the shell interpreter.
No third party tools
Cross platform (tested from macOS host to FreeBSD remote)
No need for executable bit set (
Gatekeeper on macOS is rendered moot as it not an app that must be notarized or explicitly allowed by the user.
Obviously you need an account on the remote and while nice, but not required, SSH Keys enabled so you don't have to enter a password, especially if you're running this on a batch of machines.
Note: This was done in Zsh with an associative array. It may work in Bash but you'll need at least Bash version 4 to operate properly. Also there's nothing nefarious going on with the script. It will print 3 words and then, on a new line, the word "Done" to indicate the script is finished.
If you want to verify that the code is actually from me, create a file called
vi, paste the code (no quotes), be sure to press "I" for "Insert" before pasting, save it and issue the command and check the hash below
% shasum -a 256 myencodedscript
It will only work if you use the same filename. I tested this cross platform on macOS and on FreeBSD. If you don't get the same hash, let me know.