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:
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency