Encoding / Decoding base64 Strings in the Terminal

Jan 22, 2021 min read

Sometimes you need to deal with base64 strings, here are a couple commands that make it easy to encode/decode those strings using the terminal. I deal with various types of authentication strings a lot, which are often base64 encoded, and often need to quickly test and verify the base64 values I’m using.

In your terminal you have access to the base64 program from the coreutils package installed on your machine by default. Using a simple echo statement we can encode/decode a base64 string. Here’s an example:

Encoding

echo -n < string to interpret > | base64
echo -n testuser:testpass | base64

** Note: adding the -n flag to the echo statement prevents echo from inserting a new line statement by default, with possible unintended consequences (Thanks Tomasz 🎉)

Decoding

Decoding a base64 string just requires us to use the --decode flag.

echo -n < string to interpret > | base64 --decode
echo -n dGVzdHVzZXI6dGVzdHBhc3MK | base64 --decode 

Result: testuser:testpass