$ FILE="$(mktemp)"
$ printf "a\0\n" > "$FILE"
$ od -tx1z "$FILE"
0000000 61 00 0a >a..<
0000003
So far so good.
I wrapped the above into a bash script
#! /bin/bash
cmd=("$@")
FILE="$(mktemp)"
eval "${cmd[@]}" > "$FILE"
od -tx1z "$FILE"
but
$ script printf 'a\0\n'
0000000 61 30 6e >a0n<
0000003
Why does the output change \0
to literal string? How can I prevent that from happening?
Not very important in this post: my question comes from that I am trying to wrap some commands into a bash script, so to prevent a command expansion from removing NUL:
FILE="$(mktemp)"
printf "a\0\n" > "$FILE"
S="$(uuencode -m "$FILE" /dev/stdout)"
uudecode -o /dev/stdout <(printf "$S") | od -tx1
rm "$FILE"
printf '%s' "$S"
the other in your question is unsafe to the contents of$S
. – Nov 21 '18 at 00:09printf
is designed to have format and the string separated. When usingprintf
without an explicit format the string could be modified by any of the escapes that also afect the commandecho
. Read this and entry 2. in here for more details. – Nov 21 '18 at 03:20