Of course. This is a classic and fun request.
Here is an obfuscated "Hello, World!" script. It's designed to be confusing to read at a glance but uses standard Bash features to achieve its goal.
The Obfuscated Script
Copy and paste this into a file named mystery.sh, make it executable with chmod +x mystery.sh, and then run it with ./mystery.sh.
#!/bin/bash
# You wanted obfuscated? You got it.
_=48656c6c6f
__=576f726c64
___="\x$(printf %x 33)"
____=()
while [ ${#_} -gt 0 ]; do
____+=("\x${_:0:2}")
_=${_:2}
done
____+=("\x2c\x20")
while [ ${#__} -gt 0 ]; do
____+=("\x${__:0:2}")
__=${__:2}
done
____+=($___)
printf "%b" "${____[@]}"
printf "\n"
How It Works (The De-obfuscation)
Even though it looks like nonsense, every line has a purpose. Let's break it down.
-
_=48656c6c6f
- This variable
_ is assigned a long string of numbers. These are actually hexadecimal character codes concatenated together.
48 = 'H'
65 = 'e'
6c = 'l'
6c = 'l'
6f = 'o'
-
__=576f726c64
- Similarly, this variable
__ holds the hex codes for "World".
57 = 'W'
6f = 'o'
72 = 'r'
6c = 'l'
64 = 'd'
-
___="\x$(printf %x 33)"
- This is a bit of a trick.
printf %x 33 converts the decimal number 33 to its hexadecimal representation, which is 21.
- The command then becomes
___="\x21". This is the hex code for an exclamation mark !.
-
____=()
- This initializes an empty array named
____. We will build our final message in this array.
-
while [ ${#_} -gt 0 ]; do ... done
- This loop runs as long as the string in variable
_ has a length greater than zero.
____+=("\x${_:0:2}"): This is the core of the loop.
${_:0:2}: This is Bash's "substring expansion". It takes the first 2 characters from the string _. On the first run, this is 48.
\x${...}: It prepends \x to it, creating \x48.
____+=(...): This appends the resulting string (\x48) as a new element to our array ____.
_=${_:2}: This reassigns _ to be a substring of itself, starting from the 3rd character. This effectively "chops off" the first two characters we just processed.
- This loop continues until
_ is empty, having populated our array with \x48, \x65, \x6c, \x6c, \x6f.
-
____+=("\x2c\x20")
- This directly appends the string
\x2c\x20 to the array. These are the hex codes for a comma (,) and a space ( ).
-
while [ ${#__} -gt 0 ]; do ... done
- This is the exact same logic as the first loop, but it operates on the
__ variable, adding the hex codes for "World" to our array.
-
____+=($___)
- This appends the content of the
___ variable (\x21) to the end of the array.
-
printf "%b" "${____[@]}"
- This is the final print command.
"${____[@]}": This expands the array ____ into a list of all its elements.
printf "%b": This is the key. %b tells printf to interpret backslashed escape sequences (like our \x48) in the corresponding argument. It processes the entire array, converting each hex code element into its actual character.
-
printf "\n"
- This simply prints a newline character to make the output clean.
So, the script painstakingly builds an array of hex codes (\x48 \x65 \x6c \x6c \x6f \x2c\x20 \x57 \x6f \x72 \x6c \x64 \x21) and then uses printf "%b" to translate them all at once into Hello, World!