Want to learn about base64 encode and decode method? Here in this tutorial, we explain the base64 encode and decode steps using bash shell scripting with various examples.

The base64 encoding method used to transmit data over any communication medium by converting the binary data to a text format. This method primarily used for email encryption process. The Base64 method in general is a binary to text encoding scheme which represents an 8-byte binary data to ASCII string format. This has several advantages while transmitting or channeling data among various medium – specially those reliably support text content. Hence, it is widely used in World Wide Web. Probably the most use case of this encoding scheme is using it for email attachments.
As per the Base64 representation table, the binary data can be converted to 64 different ASCII character – which are easy to transmit and printable. This encoding method uses letter A to Z, a to Z, 0 to 9 and + and /. A total of 64 ASCII characters to represent binary from 000000 to 111111. Each non-final Base64 digit represents exactly 6 bits of data.

Bash base64 encode and decode
Syntax
Before you learn about the examples, here is the basic syntax.
base64 [OPTIONs] [INFILE] [OUTFILE]
Option: You can provide any of the options or combine them as explained below.
INFILE: Input can be picked up from standard input (like command line) or from files.
OUTFILE: You can redirect the output to the standard output like the terminal or to a file.
Arguments | Descriptions |
-e or –encode | This option is used to encode any data from standard input or from any file. It is the default option. |
-d or –decode | This option is used to decode any encoded data from standard input or from any file. |
-n or –noerrcheck | By default, base64 checks error while decoding any data. You can use –n or –noerrcheck option to ignore checking at the time of decoding. |
-i, –ignore-garbage | This option is used to ignore non-alphabet character while decoding. |
-u or –help | This option is used to get information about the usage of this command. |
Example 1 – A basic encode
In Linux, base64 package is installed by default. Hence, you can use it from the command line easily. To simply encode a string or text, you can pass it through the command line via piping and get the encoded text. In this example, the string debugpoint.com is encoded to base64.
echo "debugpoint.com" | base64

The result is the base64 encoded string.
Explanation
The encoding method uses several steps to convert the input. The input characters are converted to 8-bit binary value. The entire set of binary string split into 6 bit binary values which is converted to decimal. Each of the decimal value is translated to the base64 character via the base64 index table.
In the above example, the first character “d” is converted to binary 01100100. The first 6 bits is 011001 which is 25 in decimal. The 25 refers to the Z in the base64 index table. And this goes on for the entire stream of text. See the example blow.
Example 2 – A basic decode
To decode the string, simply pass the encoded value to the base64 with option –decode. And it will give you the exact input string.

Example 3 – Encode a Text file
The same command can be used to encode a text file and redirect the output to another text file. Here’s how.
base64 example3.txt > example3-encoded.txt

Example 4 – Decode a Text File
And to decode a text file that was encoded using base64, simply use the –decode or -d switch and pass on the text file name.
base64 -d example3-encoded.txt
Example 5 – Encode a custom input from user
Using bash shell programming, you can take input from the user via the terminal and encode it. But for that you need to write a simple shell script and execute it after giving executable permission.
Here’s a simple example which takes input from user and display the encoded string.
#!/bin/bash #Sample program to take input, encode to base64 and display on terminal #Example by www.debugpoint.com echo "Enter text for encoding to base64:" read input_text output_text=`echo -n $input_text | base64` echo "The Base64 Encoded text is: $output_text"

Example 6 – A Simple Authentication using base64
Using the above encode and decode method, you can implement a simple authentication system. You can ask user to enter a password or a secret code. Then store the secret code to a file or compare on the fly. If the stored encoded string matches with user input encoded text, then the user is authenticated. Although it is very simple way of checking an authentication, but sometime useful for simple business cases.
#!/bin/bash #Sample program to take input, encode to base64 and display on terminal #Example by www.debugpoint.com echo "Type your password" read pwd1 decoded_text=`echo 'U2lsZW5jZSBpcyBnb2xkZW4h' | base64 --decode` if [[ $pwd1 == $decoded_text ]] then echo "You are a valid user." else echo "You are NOT a valid user." fi

Conclusion
I hope you get to learn the basics of Base64 encode and decode with these examples. Also learn a bit of its inner working. Let me know in the comment box below, if this helps you or need additional tutorial on this topic.
We bring the latest tech, software news and stuff that matters. Stay in touch via Telegram, Twitter, YouTube, and Facebook and never miss an update!
