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.
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:
--versionEncoding 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.txtView the saved file with:
$ cat encodedfile.txtDecoding a String
To decode the Base64 string “V2VsY29tZSB0byBMaW51eAo=”:
$ echo "V2VsY29tZSB0byBMaW51eAo=" | base64 --decodeThe original text is printed to standard output:
You can also redirect the decoded output to a file:
$ echo "V2VsY29tZSB0byBMaW51eAo=" | base64 --decode > decodedfile.txtEncoding a Text File
Encode the contents of testfile.txt:
$ base64 testfile.txtRedirect the output to a file if needed:
$ base64 -d encodedfile.txt > decodedfile.txtEncoding 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"
fiNote that encoding is not encryption; encoded data can be easily decoded, so Base64 should not be used to protect sensitive information.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
