Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a little command that I use to copy/paste files around that involves pasting the base64-encoded tar archive into stdin, but the base64 utility keeps giving me and error message about my input because I have to type a line break before issuing an EOF to stop the input.


[root@box ~]echo hello | base64
[root@box ~]base64 -d
base64: invalid input

As opposed to the clunkier:

[root@box ~]echo -n 'aGVsbG8K' | base64 -d

Which doesn't really work well in the context of the command I've written.

So is there any way to put in an EOF without that extra line break? I mean this still technically works, but I don't want errors indicated when there isn't an actual error, and I don't want to suppress stderr in case there is an actual error.

share|improve this question
Why do you have to press Enter before Ctrl-D? In my testing it worked with pressing Ctrl-D twice. – Daniel Beck Aug 14 '13 at 19:03
You can also use base64 -d <<< aGVsbG8K – choroba Aug 14 '13 at 19:07
@DanielBeck I feel so dumb for not knowing that. – Sammitch Aug 14 '13 at 19:07
@choroba I still got 'invalid input' from that – Sammitch Aug 14 '13 at 19:09
What OS, what base64 --version? – choroba Aug 14 '13 at 19:10

I think you should find a different base64 utility, such as the GNU version, which tolerates newline characters.

Failing that, assuming that the input is exactly one line long (since otherwise it would have an illegal newline in it), you could do the following:

echo -n "$(head -n1)" | base64 -d

or if you don't like echo -n (although it seems to work on your system, that's not guaranteed):

printf %s "$(head -n1)" | base64 -d

There is some ambiguity about whether newlines are legal in base64 encoding; the relevant RFC says that they are only legal if the application using the base64 encoding is documented as allowing them. That's not much help for a general purpose base64 codec.

But AFAIK, practically all applications which use base64 do explicitly allow newlines (and some require them by setting a maximum line length), so it seems odd to me that the base64 utility wouldn't have some mechanism to allow them.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .