Sample Header Ad - 728x90

How do I make Bash not drop NUL bytes on input from command substitution?

6 votes
2 answers
18999 views
I have this Linux shell command: echo $(python3 -c 'print("Test"+"\0"+"M"*18)') | nc -u [IP] [PORT] My intention is to pipe the output of the print statement to the netcat command. The netcat command creates a socket to some service that essentially returns an stdout of the string passed in. The problem here is that when I try to run this command, I get this message: -bash: warning: command substitution: ignored null byte in input; and my null byte \0 gets ignored. But I don't want the null byte to be ignored. How do I tell the system to NOT ignored my null byte and take in the input exactly as I've specified. I have done some Google searches but honestly speaking they haven't helped much. Also, any link to some great article is much appreciated. -------------------------------------------------------------------------- **EDIT** Using printf worked. Ordinarily passing
-c 'print("Test"+"\0"+"M"*18)'
also worked. Valued @cas explanation. I guess I might be sticking to printf given it's faster (though speed isn't particularly a concern in my case). Thanks to all those who contributed :-).
Asked by Joker (199 rep)
Dec 24, 2021, 09:41 PM
Last activity: Jul 6, 2024, 05:35 PM