binary/hex output

i want to output something like
2f 00 00 00

but i can't seem to escape characters like i'm used to in some programming languages, so with this:

echo "/\0\0\0" >> outputfile

i actually get
2f 5c 30 5c 30 5c 30 0a

ie the \0 isn't giving me the 00 i need, and in addition it has got an extra couple of bytes on the end - 0a (return carriage?).

obviously i'm going about this the wrong way - any suggestions? thanks.

/home/jmcnama> printf "%c%c%c" 0 0 0 | od -c
0000000   0   0   0
0000003

does generate three ascii nul characters...

Try without the quotes.

echo 2f \0\0 \0\0 \0\0
2f 00 00 00

hm, without the quotes it still seems to just give the character values

2f 00 00 00

writes a file like this

32 66 20 30 30 20 30 30 20 30 30 0A

(32 for the '2' character, 66 for 'f', 20 for ' ' etc., and still with a return carriage at the end)

likewise with "%c" 0, this gives actual hex byte value of 30 (for an ascii '0' character)

any ideas how to get round this?

How about echo -n /... | tr . '\000'

... or use Perl (or awk, or ...)

perl -e 'printf "/\0\0\0"'

didn't know about -n, great thanks. what's the tr for? '.' seems to be 2e rather than 00

the tr bit seems to do the trick but i dont get why.. thanks though

tr translates (replaces) all occurrences of one character with another. I arbitrarily chose dot to translate from; the idea is merely to use a character which doesn't need to survive past tr so you need to pick one which isn't present in your input. If you have ISO-8859-1 something like the section sign � used to be a popoular pick because you very rarely see it in any real-world data, but of course now with UTF-8 that no longer works.

great, thanks a lot