Encode or Decode Base64 String using Mac Terminal Command


To base64 encode or decode a string in macOS Terminal console you can make use of base64 command.

Examples:

1. Encode to Base64 String:

mac ~ % base64 <<< "Hello World - This is my String"

SGVsbG8gV29ybGQgLSBUaGlzIGlzIG15IFN0cmluZwo=

You can also concatenate two commands like an echo to get the encoded string,

mac ~ % echo "Hello World" | base64

SGVsbG8gV29ybGQK


2. Decode Base64 String to Text:

If you have a base64 encoded string and you want to decode it to plain string, you can make use of -D option,

mac ~ % base64 -D <<< "SGVsbG8gV29ybGQgLSBUaGlzIGlzIG15IFN0cmluZwo="

Hello World - This is my String


3. Encode a file:

If you want to encode a file, you can do that by using -i option

mac Desktop % base64 -i myFile.txt 

SGVsbG8gdGhlcmUhClRoaXMgaXMgbXkgZmlsZSBpIHdhbnQgdG8gZW5jb2RlLg==
Terminal Base64 Command - Files
Terminal Base64 Command - Files


4. Write encoded/decoded string to a file:

Now let's see how we can read a string from a file and write to another file, to write a file we can make use of -o option,

The best way to know more about this command is by checking the manual,

% base64 -i myFile.txt -o myOutput.txt


Some related links:

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.

Follow Us: GitHub | Facebook | Twitter | Instagram