Beware of Piping Echo into base64 on the Command Line
It will add a new line by default which will change your value, consider using printf instead or at least echo with the -n flag.
You can use base64
to either encode or decode values.
It’s common to pipe something into it for encoding and echo
will add a new
line by default which will produce a base64 encoded value that includes the new
line. That extra trailing new line is likely unwanted for you.
For example if you want to encode exactly hello world
, having hello world\n
is not the same. It will cause a decoding mismatch if you encoded something
quickly on the command line to use as a value for another program and your
program expects hello world
.
Encoding examples:
$ echo "hello world" | base64
aGVsbG8gd29ybGQK
$ echo -n "hello world" | base64
aGVsbG8gd29ybGQ=
$ printf "hello world" | base64
aGVsbG8gd29ybGQ=
The first base64 encoded value really have an extra \n
at the end of the
output where as the other 2 do not. That’s why the value is different near the
end.
The -n
flag with echo
will not include a trailing new line and printf
prints exactly what you tell it to, if you wanted a new line you’d have to add
it manually.
Decoding examples:
$ echo "aGVsbG8gd29ybGQK" | base64 -d
hello world
At first glance this could be seen as confusing. The first value is our originally encoded value with the trailing new line but it’s not too obvious from the output because you could totally come to the conclusion of:
“I encoded hello world
and got back hello world
, it’s right there!”.
It’s only after we decode the value without \n
where it’s more clear on
what’s happening:
$ echo "aGVsbG8gd29ybGQ=" | base64 -d
hello world%
When you use echo
, printf
and other tools that print output, having the
output end with %
marks that the line is missing a newline character. In your
terminal this character will likely be colored up differently so you can tell
it’s not a literal percent sign in the string.
The video below demonstrates the above in more detail:
# Demo Video
Timestamps
- 0:23 – Using base64 to encode and decode a string
- 1:25 – Using echo with the -n flag or printf to not add a trailing new line
- 2:22 – Getting a different base64 encoded value without the new line
- 2:53 – This was a fun one to debug recently
- 3:35 – Should you use echo -n or printf? It’s up to you
References
Has this ever bitten you in the past? Let me know below!