In this article we will cover Base64 in its entirety: we will learn what Base64 is and what it is used for. We will also learn about the characters of this method, the concepts of encoding and decoding. We’ll even show you the algorithm, not just in theory, but through examples: you’ll be able to encode and decode Base64 manually and in JavaScript.
, ,

Base64 Encoding And Decoding in Bash on Linux & macOS

This article delves into Base64, Shell, Bash, Linux & macOS. It defines Base64, how it works, and how it can be used as a binary-to-text encoding method. In addition, the article defines a shell as a command-line interface application and examines Bash, a prominent Unix-like shell. It explains and provides examples of how to use the base64 command in Bash to conduct Base64 encoding and decoding.

What is Base64?

Base64 is a binary-to-text encoding system that uses an ASCII string to represent binary data. It is often used to encode binary data, such as images, files, or binary data, into a text-based format that may be safely transferred via text-based protocols, such as email, or included in URLs without causing problems owing to special characters. Base64 encodes data by dividing it into 6-bit chunks and expressing each piece as a different character from a set of 64. Binary data can thus be expressed using only printable ASCII characters.

What is Shell?

A shell is a command-line interface (CLI) program that provides users with an environment to interact with an operating system. It interprets user commands and passes them to the operating system for execution. The term “shell” is more general and can refer to any CLI, whether it’s for Unix-like systems or Windows. Shells can provide features like scripting capabilities, variables, and the ability to automate tasks through command sequences.

What is Bash?

Bash, an abbreviation for “Bourne Again SHell,” is a popular Unix-like shell and command-line interface. Many Linux distributions and macOS systems use it as their default shell. Bash has a robust feature set that includes variable manipulation, loops, conditions, and comprehensive command-line history. It also supports scripting, which allows users to use shell scripts to build complex tasks and automate procedures.

Base64 Encoding in Bash on Linux & macOS

Bash has built-in command-line facilities for Base64 encoding and decoding. In Bash, the base64 command is used to encode and decode Base64 data. The basic grammar for encoding and decoding is as follows.

The following syntax can be used to encode binary data into Base64 format:

base64 [options] [input-file]

For example, to encode a file named “data.txt” and output the encoded data to the terminal:

base64 data.txt

Base64 Decoding in Bash on Linux & macOS

To decode Base64-encoded data back to its original binary form, use the following syntax:

base64 -d [options] [input-file]

For example, to decode a Base64-encoded file named “encoded.txt” and output the decoded data to the terminal:

base64 -d encoded.txt

Base64 Options in Bash

The base64 command also provides a number of options for dealing with input sources, formatting, and output destinations. As a result, it’s a useful tool for working with Base64 data from the command line in a Bash environment.

  • -d, --decode: This option is used to decode data from base64 format. When specified, the command will decode the provided base64-encoded input data.
  • -i, --ignore-garbage: When decoding with the -d option, this flag allows the command to ignore non-alphabet characters in the input. This can be helpful if the input data contains characters that are not part of the base64 encoding.
  • -w, --wrap=COLS: This option controls line wrapping for encoded output. It specifies the maximum number of characters per line for the encoded output. By default, lines are wrapped at 76 characters. Use 0 to disable line wrapping altogether.
  • --help: This option displays a help message that provides a brief overview of the available options for the base64 command, along with a brief description of their usage.
  • --version: This option outputs version information about the base64 command and then exits.

Base64 Examples in Bash

In this section we will now look at various examples of encoding and decoding Base64 files and text in Bash.

echo -n "B64Encode.com" | base64

This Bash Shell command takes the string “B64Encode.com” as input, and pipes it to the base64 command using the | symbol. The base64 command then encodes the input string in base64 format and outputs the result. The -n flag passed to the echo command prevents a newline character from being appended to the output.

base64 file.txt

This command encodes the content of the file.txt file in base64 format

echo "QjY0RW5jb2RlLmNvbQ==" | base64 -d

The input is a base64 encoded string, “SGVsbG8sIFdvcmxkIQ==”, which is piped to the base64 command using the | symbol. The -d flag specifies that the input should be decoded. The base64 command then decodes the input string from base64 format and outputs the result.

base64 -d encoded.txt > decoded.txt

This command decodes the content of the encoded.txt file from base64 format and saves the result to a new file named decoded.txt. The -d flag passed to the base64 command specifies that the input should be decoded. The > symbol redirects the output of the command to the specified file.