8

If I base64 encode a string which consists of seven characters e.g. abcdefg with the website https://www.base64encode.org/ the result is YWJjZGVmZw==. The trailing "==" characters are padding because the number of input characters cannot be divided by 7.

I've to reproduce this result in bash. So I've tried the following command:

echo "abcdefg" | base64

However, the result is different now:

YWJjZGVmZwo=

I'm using Ubuntu where base64 (GNU coreutils) 8.25 is installed.

I would be glad if someone could give me a hint.

2 Answers 2

13

I've just noticed that the reason for the described behaviour is the newline which echo writes at the end. So the correct command is the following which suppress the newline

echo -n "abcdefg" | base64

Then the output is like I expect it:

YWJjZGVmZw==
1
  • Better use printf
    – CervEd
    Commented Dec 7, 2023 at 8:36
0

It is also tricky how a here-string will produce unexpected output. It is probably missing the null character \0.

$ base64 <<<"abcdefg"
YWJjZGVmZwo=
$ printf 'abcdefg' | base64
YWJjZGVmZw==

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.