I have successfully written following function:
function print0(){
stdin=$(cat);
echo "$stdin" | awk 'BEGIN {ORS="\000";}; { print $0}';
}
which works as a -print0
argument in find
command, but basically for any command that passes it's output to this function. It is useful with xargs -0
. Then I realized that also opposite of this function would be useful too. I have tried following:
function read0(){
stdin=$(cat);
echo "$stdin" | awk 'BEGIN {RS="\000"; ORS="\n";}; {print $0}';
# EQUIVALENTS:
# echo "$stdin" | perl -nle '@a=join("\n", split(/\000/, $_)); print "@a"'
# echo "$stdin" | perl -nle '$\="\n"; @a=split(/\000/, $_); foreach (@a){print $_;}'
}
But it does not works, the interesting is that when I tried just commands (awk or perl) it worked like a charm:
# WORKING
ls | print0 | awk 'BEGIN {RS="\000"; ORS="\n";}; {print $0}'
ls | print0 | perl -nle '@a=join("\n", split(/\000/, $_)); print "@a"'
ls | print0 | perl -nle '$\="\n"; @a=split(/\000/, $_); foreach (@a){print $_;}'
# DOES NOT WORKING
ls | print0 | read0
What I am doing wrong? I am assuming that something is wrong with dealing null characters via following command: stdin=$(cat);
EDIT: Thank you all, the conclusion is that bash variables cannot hold null value. PS: mentioned command was just as example I know converting nulls to newlines and vice versa has not rational reason.