Fundamentals 8 min read

Master Base64 Encoding and Decoding on the Command Line

This guide explains what Base64 encoding is, why it’s needed for transmitting binary data as text, and provides step‑by‑step command‑line examples for encoding and decoding strings, files, and user input with practical Bash scripts.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Base64 Encoding and Decoding on the Command Line

Base64 is a method for converting binary data into ASCII characters, which is useful when transmitting data through text‑only systems that cannot handle raw binary.

What Base64 Is Used For

Data compression

Data hiding

Transmitting data in an alternative format

Base64 encoded data may only contain letters, digits, and the = padding character. For example, c2FtcGxlCg== is valid, while HV3.Zh2J== is not.

Base64 Command Syntax

The basic syntax for the GNU base64 utility is:

$ base64 [OPTION] [FILE]

Common Options

Decode a file or string: -d or --decode Show help information: --help Ignore non‑alphabet characters when decoding: -i, --ignore-garbage Show version information:

--version

Encoding a String

To encode the text “Welcome to Linux”: $ echo "Welcome to Linux" | base64 The command prints the Base64 representation to standard output. You can also redirect the output to a file:

$ echo "Welcome to Linux" | base64 > encodedfile.txt

View the saved file with:

$ cat encodedfile.txt

Decoding a String

To decode the Base64 string “V2VsY29tZSB0byBMaW51eAo=”:

$ echo "V2VsY29tZSB0byBMaW51eAo=" | base64 --decode

The original text is printed to standard output:

You can also redirect the decoded output to a file:

$ echo "V2VsY29tZSB0byBMaW51eAo=" | base64 --decode > decodedfile.txt

Encoding a Text File

Encode the contents of testfile.txt:

$ base64 testfile.txt

Redirect the output to a file if needed:

$ base64 -d encodedfile.txt > decodedfile.txt

Encoding User Input with a Script

The following Bash script reads data from the user, encodes it with Base64, and prints the result:

#!/bin/bash
# Prompt for input
echo "Provide Some data to encode"
# Read input into variable
read data
# Encode and store in variable
encod_data=`echo -n $data | base64`
# Print encoded output
echo "Encoded text is : $encod_data"

Verifying a User Key

This script decodes a predefined Base64 key and compares it with user input to validate the key:

#!/bin/bash
# Prompt for key
echo "Enter your key"
read key
# Decode predefined key
orig_key=`echo 'QWJjMTIzCg==' | base64 --decode`
# Compare
if [ $key == $orig_key ]; then
  echo "You have entered a valid key"
else
  echo "The key you have entered is not valid"
fi

Note that encoding is not encryption; encoded data can be easily decoded, so Base64 should not be used to protect sensitive information.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxshell scriptdecodingBase64
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.