Linux – Send data as bytes to pipes in bash

Send data as bytes to pipes in bash… here is a solution to the problem.

Send data as bytes to pipes in bash

I’ve recently spent some time trying to use bash to write some numbers as bytes to the pipe (e.g. 10 as 0xA, not 0x310x30). Unfortunately, I was unable to do this and had to rewrite the script to Python. Is there any way to send data as bytes instead of characters in a bash script?

Solution

As Chepner says, you can output any value, for example: $’\012′. So, a short script:

convert() {
    printf \\$(printf '%03o' $1)
}
convert 122 | od -bc

The print on the right will convert the decimal parameter to octal numbers, and the second print will print the octet number above… For example, decimal 65 is converted to bytes ‘A’.

Related Problems and Solutions