base64 Command: Encoding and Decoding Basics
What you'll be able to do
- Encode and decode strings and files with `base64`
- Tell encoding, encryption, and hashing apart
- Avoid the `echo` newline trap and the `>` overwrite
Prerequisites (read these first)
What You'll Learn
- How to encode and decode strings and files with
base64 - How to tell encoding, encryption, and hashing apart
- How to avoid the newline trap that
echoadds - Common options like
-w(wrap) and-d(decode)
Who this is for: Linux beginners who saw a base64 string in an API token or email attachment and wondered "what is this?"
Words used here (this is the part people mix up)
- Encoding: changing how data is written. No key is involved. Anyone can turn it back.
base64is this. - Encryption: making data readable only to whoever holds the key. Without the key, the contents stay hidden. Tools like
openssldo this. - Hashing: producing a short value from the contents. It cannot be reversed. It answers "are these the same?" Tools like
sha256sumdo this.
All three produce a strange-looking string. What differs is whether you can reverse it and whether a key is required. base64 here is the kind anyone can reverse without a key.
Intro: Lina's Mystery String
SGVsbG8gV29ybGQ= in a config file. What is it? An encrypted password?base64. But one important thing up front: it is not encryption.base64 -d and anyone gets the original back. So it can't protect secrets. Today let's look at the basics of base64 and why it exists.Quick Summary
base64represents binary data with only 64 ASCII characters (A-Za-z0-9+/). The trailing=is padding that pads the length- Encode with
base64, decode withbase64 -d - It is not encryption. Anyone can decode it. Never use it to protect secrets
1. What Is base64?
Conclusion: base64 converts binary data into 64 ASCII characters. It is packaging for transport, not encryption.
base64 represents any data using just 64 characters. So it's not "encryption," it's packaging for transport. The contents aren't hidden, but the form is safe to move around.Where base64 is used
- Email attachments (MIME)
- HTTP Basic authentication headers
- Binary embedded in config files / JSON (images, certificates)
data:URLs (embedding images directly in HTML)
2. Encoding a String
Conclusion: Pipe with
echo -nintobase64. Without-n, the trailing newline gets encoded too.
Hello World.base64 with |.$ echo -n "Hello World" | base64
SGVsbG8gV29ybGQ=
Don't forget the -n in echo -n
By default echo adds a trailing newline. Without -n, that one newline character (\n) gets encoded too. The resulting string changes.
-n to echo?$ echo "Hello World" | base64
SGVsbG8gV29ybGQK
= to K. So an invisible newline caused it.echo -n or printf.SGVsbG8gV29ybGQ= vs SGVsbG8gV29ybGQK
The ending differs: = (no newline) vs K (includes the \n). When base64-encoding a token or password, this newline contamination is a classic bug, so always use echo -n or printf.
# printf adds no newline, so it avoids the missing -n problem $ printf '%s' "Hello World" | base64
3. Decoding
Conclusion: Use
base64 -d(or--decode). Anyone can run it, so it offers no secrecy.
SGVsbG8gV29ybGQ= back. Just add -d.$ echo "SGVsbG8gV29ybGQ=" | base64 -d
Hello World
-d and --decode are the same
Both base64 -d and base64 --decode work. The short -d is more common.
openssl.4. Encoding and Decoding Files
Conclusion: Pass a filename to encode; redirect the
-doutput to a file to restore it.
# Create a practice file $ echo "practice" > sample.txt # Encode a file and save as .b64 $ base64 sample.txt > sample.txt.b64 # Decode the .b64 back into a restored file $ base64 -d sample.txt.b64 > restored.txt
Binary like images or certificates goes through exactly the same steps.
Where files land, and the overwrite risk
- New files are created in the directoryA container that organizes files. Same idea as a "folder" on Windows or macOS. you are in right now. Check it with
pwd >is shellAn interactive program that reads the commands you type and runs them. redirection. If that name already exists, it wipes the contents and overwrites- Nothing asks for confirmation. Unlike a GUI, the overwritten content does not go to a trash folder
- If overwriting worries you, run
ls restored.txtfirst to see whether the name is taken base64does not run in this site's virtual terminal. Run it in your own terminal- Practice inside the empty directory you create in "7. Mini Exercise". Nothing you already had can break there
diff or sha256sum. If they match, the restore was perfect.# Verify the original and restored files are identical $ diff sample.txt restored.txt && echo "OK: identical"
OK: identical
Size grows by about 1.33x
base64 turns 3 bytes into 4 characters. The encoded output is about 4/3 (roughly 33% larger) than the original. Watch out when base64-encoding large files where storage is tight.
5. Controlling Line Wrap with -w
Conclusion:
base64wraps at 76 characters by default. Use-w 0for a single line.
base64 inserts a newline every 76 characters (wrapping) by default. That matches the MIME spec for email.-w 0. -w is "wrap," and 0 means no wrapping.# No wrapping (single line) $ base64 -w 0 image.png > oneline.b64 # Wrap every 40 characters $ echo -n "Hello World, this is a longer text" | base64 -w 40
macOS base64 has no -w
-w is a GNU coreutils (standard Linux) option. The macOS (BSD) base64 has no -w and controls wrapping differently. This article assumes Linux (GNU coreutils).
6. Common Pitfalls
Conclusion: "mistaking it for encryption," "echo newline contamination," and "invalid input on decode" are the three big stumbling blocks.
Pitfall 1: Thinking base64 is encryption
This is the most dangerous one. base64 can be decoded by anyone. Putting a secret in base64 and then committing it to GitHub or logging it is a never-ending source of incidents. Always encrypt data you need to hide.
Pitfall 2: echo newline contamination
As we saw in section 2, forgetting echo -n encodes the newline too. When a base64-encoded token fails authentication, suspect this first.
Pitfall 3: invalid input on decode
If a space or stray character sneaks in during copy-paste, you get base64: invalid input. base64 uses exactly 64 characters (A-Z a-z 0-9 + /), and anything else is an error.
Use -i (--ignore-garbage) to skip characters outside those 64 and decode anyway.
# Ignore stray newlines or spaces while decoding $ base64 -d -i messy.b64
Safe templates (copy-paste)
# Encode a string (no newline added) printf '%s' "text" | base64 # Encode on one line (no wrapping) base64 -w 0 file.bin # Decode echo "SGVsbG8=" | base64 -d
7. Mini Exercise: Try It Yourself
Conclusion: Three tasks (encode, round-trip, newline difference) help cement how base64 behaves.
Make an empty practice directory and work inside it. That way you never overwrite a file you already had.
# Prepare a practice directory and move into it $ mkdir -p ~/base64-practice && cd ~/base64-practice
Exercise 1: Encode your own name with base64, without a trailing newline.
Show Hint 1 (Direction)
Pipe the output of a command that prints text. Pick the form that adds no newline.
Show Hint 2 (Command name)
Use printf '%s' or echo -n, then pipe into base64.
Show Answer
$ printf '%s' "Lina" | base64
TGluYQ==
printf '%s' and echo -n both keep the trailing newline out.
Exercise 2: Decode the Exercise 1 output and confirm the original comes back.
Show Hint 1 (Direction)
Use the same command with one option that reverses the direction.
Show Hint 2 (Command name)
Use base64 -d.
Show Answer
$ echo "TGluYQ==" | base64 -d
Lina
If your original name appears as-is, the round trip succeeded.
Exercise 3: Encode both echo -n "test" and echo "test", then explain in one line why the results differ.
Show Hint 1 (Direction)
You cannot see it, but one of them sends one extra character at the end.
Show Hint 2 (Command name)
Run echo -n "test" | base64 and echo "test" | base64, then compare the output.
Show Answer
$ echo -n "test" | base64 $ echo "test" | base64
dGVzdA== dGVzdAo=
echo adds a trailing newline (\n) by default. echo "test" encodes test\n (5 bytes) while echo -n "test" encodes test (4 bytes). Different input, different base64 string.
Looking Back
Conclusion: base64 is not a way to hide data; it is a wrapper for moving it safely.
base64 isn't a way to hide something — it's a way to wrap it for transport.Three-Line Recap
Conclusion: base64 is a keyless conversion — neither encryption nor hashing.
base64is encoding, a change of writing form. Anyone reverses it withbase64 -d- Encryption needs a key. Hashing cannot be reversed. base64 is neither of those
- Use
echo -norprintffor strings, and mind the>overwrite when writing files