Today I learned: How to encode/decode base64 string from the command line

Usually in order to encode/decode a piece of string I would hunt for an online tool to do so. Today, I have seen a colleague in need to decode some base64 token, do it right from her command line.

Here are the magic commands:

Encode a string to base64 from the command line

echo -n 'string-to-be-encoded' | base64

In the above command the -n flag ensures you do not miss a hidden character like line breaks...

This command outputs:

c3RyaW5nLXRvLWJlLWVuY29kZWQ=

Decode a base64 string from the command line

echo -n 'c3RyaW5nLXRvLWJlLWRlY29kZWQ=' | base64 --decode

This command outputs:

string-to-be-decoded%

Everything in front of the last % character is the original string.

Reference

Find out how to do these operations using files rather than strings within the article: How to base64 encode and decode from command-line