Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

This is a rather hypothetical question, so please don't ask me why I'd want to do this.

Assuming that I have a variable GIF that contains binary data, and assuming that I cannot use the pipe | operator, the following should be the correct way to use 'triple-less-than' operator:

openssl enc -base64 <<< $GIF

However, it appears to me that <<< is not binary-safe and therefore the binary data gets corrupt.

Is there a binary-safe equivalent?

share|improve this question
    
Please note that bash variables can't contains NUL chars. So your variable $GIF, for a start, is not holding full-binary even before starting. However, to the exception of the NUL character, it is holding all the others. (as a ref: vaab.blog.kal.fr/2014/05/03/… ) – vaab Apr 28 at 4:05
up vote 17 down vote accepted

The here string redirection (<<<) is a simple form of here document redirection (<<). Here string redirection is not "binary safe"; Bash will perform expansion on the here string. In addition, Bash will append a new-line to the end of the here string (issue the command xxd -p <<< "foo" and you'll get 666f6f0a in return).

Your only safe bet, excluding pipes, is I/O redirection.

Similar not binary safe question here. You can store encoded data and try this

COMMAND_WITH_BIN_INPUT <(uudecode <(echo "$uuEncodedData")) 

however this is not far from

echo "$uuEncodedData"|uudecode|COMMAND_WITH_BIN_INPUT

but without pipe metachar.

share|improve this answer
1  
First link no longer exists. Hence the importance of including as much information in the answer as possible, even if it appears too verbose at first, because the information linked to does not stay around forever. – Wesley Apr 28 '14 at 22:44
    
Alternative link for "here strings": linux.die.net/abs-guide/x15683.html – JJC May 15 '14 at 17:56
1  
Or, try od -a <<< "" and you'll see nl instead of the hex value. – jcalfee314 Jun 17 '14 at 14:46
    
@Wesley There's also archive.org. :) Original link: web.archive.org/web/20120126213653/http://tldp.org/LDP/abs/html/… – Jason C Jun 17 '14 at 15:05

Bash isn't binary safe in general, and will corrupt nulls and newlines in variables containing binary content during substitution.

So I think the answer is "no" but more fundamentally "not in a shell scripting language" because they all seem to have problems with binary.

I'd say however you plan to get the data into $GIF, you instead get it into a file, or use python as an alternative scripting language which will handle binary data without problems.

share|improve this answer
    
References or precision, or simple examples would have been more helpful than a wild guess and general feeling from your side. There are ways to get binary data parsed through bash. – vaab Apr 28 at 4:08
    
@vaab Yes, you can see the upvoted and accepted answer above. – Paul Apr 28 at 5:24

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.