BASH Programming

Base64 Encode and Decode From Command Line

Encoding is the process used to convert data in a format required for effective transmission or storage. In contrast, decoding is opposite to the encoding method which converts the encoded data back to its original format. Base64 is the encoding process where the binary data is converted into ASCII. Base64 encoding is mostly required to avoid the transmission problems that occur when binary data is transmitted to text-based systems which cannot handle the binary data properly. As a result, the information is lost or corrupted during transmission.

Some of the uses of encoding are:

  • Data compression
  • Data hiding
  • Transmission of data in another format

For encoding data, Base64 uses only alphabet, number and = symbol. For instance, c2FtcGxlCg== is a valid encoded data while b?HV3.Zh2J== is not a valid encoded data.

In this Linux Hint tutorial, we will explain how to use the base64 command to encode and decode the data in a string or a file. We have performed the commands on Ubuntu 20.04 Focal Fossa system. However, you can also run the same commands on other Linux distributions.

Base64 Syntax

Here is the syntax for encoding using Base64:

base64 [OPTION] [FILE]

Options

Some of the command-line options that can be used with base64 command are:

-d or --decode

Use this option to decode a file or a string.

--help

Use this option to display help regarding the usage of base64.

-i, --ignore-garbage

Use this option while decoding to ignore non-alphabet characters

--version

Use this option to display version information

Encoding String

You can easily encode a string using the base64 command. For instance, to encode a sample text “Welcome to Linux” to base64, the command would be:

echo “Welcome to Linux” | base64
Terminal Output:
linuxhint@hp34:~$ echo "Welcome to Linux" | base64
V2VsY29tZSB0byBMaW51eAo=

This command will encode the text in the string using base64 and print the encoded text to standard output as shown in the Terminal Output above.

You can also save the encoded output to a file rather than printing to standard output using the redirection operator (>). The following command will encode the text and save the output to a file named “encodedfile.txt:

echo “Welcome to Linux” | base64 > encodedfile.txt

To view the encoded file, you can use the cat command:

cat encodedfile.txt
Terminal Output:
linuxhint@hp34:~$ cat encodedfile.txt
4oCcV2VsY29tZSB0byBMaW51eOKAnQo=

Decoding String

You can also decode the base64 encoded text using the –decode or -d option. For instance to decode base64 encoded text “V2VsY29tZSB0byBMaW51eAo=”, the command would be:

echo  4oCcV2VsY29tZSB0byBMaW51eOKAnQo= | base64 --decode

This command will decode the base64 encoded text and print the original text on the standard output as shown in the following Terminal Output.

Terminal Output:
linuxhint@hp34:~$ echo  4oCcV2VsY29tZSB0byBMaW51eOKAnQo= | base64 --decode
“Welcome to Linux”

Encoding Text File

The base64 command can also be used to encode a text file. For this example lets create a text file with some content first using this command on the terminal, or use any textfile.

echo """I Love Linux
I Love Linux
I Love Linux
LinuxHint is my homepage"
"" > testfile.txt

To encode this or any text file named “testfile.txt”, the command would be:

base64 testfile.txt

This command will encode the specified text file and print its encoded form on the standard output as shown in the following Terminal Output:

linuxhint@hp34:~$ base64 testfile.txt
SSBMb3ZlIExpbnV4CkkgTG92ZSBMaW51eApJIExvdmUgTGludXgKTGludXhIaW50IGlzIG15IGhv
bWVwYWdlCg==

You can also save the encoded output to a file rather than printing to standard output using the redirection operator (>). The following command will convert the text in the file using base64 and save the output to another file named “encodedfile.txt”. The command does not print anything to the screen.

base64 testfile.txt > encodedfile.txt

To view the encoded file, you can use the cat command:

cat encodedfile.txt
Terminal Output:
linuxhint@hp34:~$ base64 testfile.txt > encodedfile.txt
linuxhint@hp34:~$ cat encodedfile.txt
SSBMb3ZlIExpbnV4CkkgTG92ZSBMaW51eApJIExvdmUgTGludXgKTGludXhIaW50IGlzIG15IGhv
bWVwYWdlCg==

Decoding Text File

To decode an encoded text file, use the –decode or -d option. For instance to decode base64 encoded text file “encodedfile.txt”, the command would be:

base64 -d encodedfile.txt

This command will decode the base64 encoded text file and print the original text on the standard output as shown in the following Terminal Output:

Terminal Output:
linuxhint@hp34:~$ base64 -d encodedfile.txt
I Love Linux
I Love Linux
I Love Linux
LinuxHint is my homepage
linuxhint@hp34:~$

Encoding User Input

Using the base64 encoding, we can encode any user-provided data. For this purpose, we will need to create a script that will take user input, encode it using base64 encoding, and print the encoded data on standard output. Create a script “test.sh” with the following code:

#!/bin/bash
# Print message to ask for input
echo "Provide Some data to encode"
# Save the input to a variable named “data”
read data
# Encode using base64 encoding and save the output to a variable “encod_data”
encod_data=`echo -n $data | base64`
# Print encoded output
echo "Encoded text is : $encod_data"

Run the script as follows:

chmod 755 test.sh; ./test.sh

After running the script, you will be asked to input the data that you want to encode. Type some data and press Enter, and you will receive the encoded output on the screen as shown below:

Terminal Output:
linuxhint@hp34:~$ chmod 755 test.sh; ./test.sh
Provide Some data to encode
I love linux
Encoded text is : SSBsb3ZlIGxpbnV4

This encoded text can be sent over the internet or to another program and then decoded later using a simple command. For this command we assume the receiving program, decode.sh, has put the encoded data into a variable in BASH called RECEIVED_ENCODING.

#!/bin/bash
RECEIVED_ENCODING=SSBsb3ZlIGxpbnV4
RECEIVED_STRING=`echo  $RECEIVED_ENCODING | base64 --decode`
echo $RECEIVED_STRING

Here you can see the results of the receiving program in the Terminal Output:

linuxhint@hp34:~$ chmod 755 decode.sh ; ./decode.sh
I love linux

Conclusion

This is how you can use the base64 to encode and decode a string or a file from the command line. The results can be printed on the standard output, saved in a variable or a file, or passed over the network to another program. However, remember that encoding is not similar to encryption, and one can easily reveal the encoded data, so it is not recommended to use encoding for the transmission of sensitive data unless its also encrypted.

About the author

Linux Wolfman

Linux Wolfman is interested in Operating Systems, File Systems, Databases and Analytics and always watching for new technologies and trends. Reach me by tweeting to @linuxhint and ask for the Wolfman.