Echo nc netcat9/20/2023 ![]() ![]() Its implementation, arguments, and behavior are all a bit hacky. Using echo at all is actually a bad idea when trying to use it to send any possible string to the device! Why? Well, Schaller pointed out the following resource to me in the comments, and it has tons of really valuable information: Unix & Linux: Why is printf better than echo?.Ī few of the key reasons why NOT to use echo include: Don't use echo at all in any shell scripts if you want portable and expected behavior, and the ability to send any command to the device! Key takeaway: use printf or echo -n to NOT have a trailing newline character at the end of your prints.īUT, that's not the end of it! Here are two more points: 1. Printf 'measure:voltage? ch1' | timeout 0.2 nc 192.168.0.1 9999įrom man echo: -n do not output the trailing newline # also works, of course, as stated in the question and it looks like you can suppress the trailing newline from echo by using -n: # works!Įcho -n 'measure:voltage? ch1' | timeout 0.2 nc 192.168.0.1 9999 If I forcefully add it (a newline char, \n) to the end of the printf cmd, then it fails too-meaning the device will not respond to the command as expected: # fails: ![]() It looks like echo adds a trailing newline to the string, whereas printf does NOT, and this trailing newline character is interfering with the device's ability to parse the command. Ensure you're not accidentally sending a newline char ( \n) at the end of the command ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |