Bash newbie; using this idiom to generate repeats of a string:
echo $(head -c $numrepeats /dev/zero | tr '\0' 'S')
I decided I wanted to replace each null byte with more than one character (eg. 'MyString' instead of just 'S'), so I tried the following with sed
echo $(head -c $numrepeats /dev/zero | sed 's/\0/MyString/g' )
But I just get an empty output. I realized I have to do
echo $(head -c $numrepeats /dev/zero | sed 's/\x0/MyString/g' )
or
echo $(head -c $numrepeats /dev/zero | sed 's/\x00/MyString/g' )
instead, but I don't understand why. What is the difference between the characters that tr
and sed
match? Is it because sed
is matching against a regex?
Edit
Interesting discovery that \0
in the replacement
portion of the 's/regexp/replacement'
sed
command actually behaves the same as &
. Still doesn't explain why \0
in regexp
doesn't match the nullbyte though (as it does in tr
and most other regex implementations)