Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

eg:

[root@box ~]echo hello | base64
aGVsbG8K
[root@box ~]base64 -d
aGVsbG8K[enter][ctrl+d]
hello
base64: invalid input

As opposed to the clunkier:

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

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
3  
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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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