Bash base64 decode and encode on Linux

Linux commonly uses base64 to encode and decode data. This method of encoding provides a reliable way for data to be transmitted and stored. The encoding process will convert binary data to ASCII characters, making it usable by a variety of services (such as OpenSSL) that require readable ASCII character transmission as opposed to binary. Afterwards, the data can be decoded back to binary data. In this tutorial, you will see how to use the base64 command to decode and encode data on a Linux system.

In this tutorial you will learn:

  • How to encode and decode data with base64 in terminal
  • How to encode and decode base64 data from file
Bash base64 decode and encode on Linux
Bash base64 decode and encode on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software base64
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

base64 Encode and Decode Example Commands




The base64 utility should be installed by default on all major Linux distributions. Let’s see some examples on how we can use the base64 Linux command to encode and decode data from the command line.

  1. Using the base64 command with no further options will encode terminal input or the contents of a file to base64 format. Let’s look at a simple example of converting a text string via standard input from the echo command.
    $ echo "Welcome to linuxconfig.org" | base64
    V2VsY29tZSB0byBsaW51eGNvbmZpZy5vcmcK
    

    The output (second line) is our base64 encoded data.

  2. Now we can take the encoded data we received above, and use the -d or --decode option to decode it back to its original format.
    $ echo V2VsY29tZSB0byBsaW51eGNvbmZpZy5vcmcK | base64 -d
    Welcome to linuxconfig.org
    
  3. We can also encode an input file into the base64 format by using the following command syntax:
    $ base64 input-file.txt 
    VGhpcyBpcyBteSBpbnB1dCBmaWxlIQo=
    

    Or to output the encoded data into its own file:

    $ base64 input-file.txt > output-file.txt
    
  4. Next, let’s use the -d or --decode option to decode a file from base64 back to its original format:
    $ base64 -d output-file.txt
    This is my input file!
    

    The decoded contents are shown as output in our terminal.

NOTE
Although at first glance it can seem like base64 is obscuring our text strings just like encryption would do, base64 is NOT encryption and should not be used as a substitute for protecting sensitive information. This data can be easily decoded and base64 is not meant to make the original data indecipherable.

Closing Thoughts




In this tutorial, we saw how to use the base64 command to encode and decode data on a Linux system. As some programs and system services require base64 encoding for their input, it is handy for a system administrator to be able to quickly encode a binary data to base64 to use as input. The same command can also be used to convert the encoded data back to human readable format.